大家好,我是Spring安全新手,
我的任务是通过匹配从x509客户端证书检索到的用户名,根据活动目录对用户进行身份验证。到目前为止,我所做的是启用ssl相互身份验证
上面的部分工作得很好,现在我有security.xml文件,我已经配置了与x509参考和活动目录配置相关的所有内容
<global-method-security secured-annotations="enabled" />
<http >
<intercept-url pattern="/**" access="ROLE_USER,ROLE_ANONYMOUS" requires- channel="https"/>
<intercept-url pattern="/UserLogin/*" access="ROLE_ADMIN,ROLE_USER" requires-channel="https"/>
<x509 subject-principal-regex="CN=(.*?)," user-service-ref="ldapUserService" />
</http>
<authentication-manager>
<authentication-provider user-service-ref="ldapUserService" />
</authentication-manager>
<bean:bean id="contextSource" class="org.springframework.security.ldap.DefaultSpringSecurityContextSource">
<bean:constructor-arg value="ldap://ActiveDirectoryURL:389/CN=example,DC=net"/>
<bean:property name="userDn" value="mkanaka@example.local"/>
<bean:property name="password" value="secuera1SMK"/>
</bean:bean>
<bean:bean name="ldapUserService" class="org.springframework.security.ldap.userdetails.LdapUserDetailsService">
<bean:constructor-arg ref="ldapUserSearch"/>
<bean:constructor-arg ref="ldapAuthoritiesPopulator"/>
</bean:bean>
<bean:bean name="ldapUserSearch" class="org.springframework.security.ldap.search.FilterBasedLdapUserSearch">
<bean:constructor-arg value=""/>
<bean:constructor-arg value="(&(sAMAccountName={0})(objectclass=Users))"/>
<bean:constructor-arg ref="contextSource" />
</bean:bean>
<bean:bean name="ldapAuthoritiesPopulator"
class="org.springframework.security.ldap.userdetails.DefaultLdapAuthoritiesPopulator">
<bean:constructor-arg ref="contextSource" />
<bean:constructor-arg value="" />
<bean:property name="groupSearchFilter" value="member={0}" />
<bean:property name="searchSubtree" value="true" />
</bean:bean>
现在我面临的问题是,当我试图检索.getPrincipal .getAuthentication SecurityContextHolder.getContext () () ();它的返回类型是字符串而不是userDetails(记录时使用的证书详细信息),getPrincipal()的字符串输出是anonymousUser,它给出的权限是ROLE_ANONYMOUS,但是当我调用getAuthentication.isAuthenticated()时,它返回true。我使用tomcat 7, Spring security 3.1
有什么问题,请在这方面帮助我
根据您的配置,从证书中提取的用户名将是"Mohankumar Kanaka", Spring Security将尝试使用该用户名进行身份验证。
对于您的LDAP配置,它将搜索具有与此匹配的sAMAccountName
属性的目录条目(它没有找到)。
您需要某种方法将证书中的名称映射到Active Directory条目。Spring Security不可能自动为您完成这些工作。理想情况下,证书中主题名称的一部分应该与AD用户名匹配,以便您可以轻松提取它。