《Spring Security3》第九章(LDAP)第三部分翻译(LDAP明确配置)
2013-04-20 15:43 阅读(216)


明确的LDAP bean配置

         在本节中,我们带领你学会明确配置以下两项功能所需要的bean集合,即连接外部的LDAP服务器和支持授权的LdapAuthenticationProvider。正如其它基于bean的配置,你可能不希望这样做,除非你发现security命名空间风格的配置不能支持你的业务或技术需求——在这种情况下,请继续阅读。

配置外部的LDAP服务器引用

         为了实现这个配置,我们假设在本地的10389端口上运行着LDAP服务器,与前面章节的对应<ldap-server>有相同的配置。需要的bean定义在dogstore-base.xml中提供,如下:

 

Xml代码  收藏代码
  1. <bean class="org.springframework.security.ldap.DefaultSpringSecurityContextSource"  
  2.  id="ldapServer">  
  3.   <constructor-arg value="ldap://localhost:10389/dc=jbcppets,dc=com"/>  
  4.   <property name="userDn" value="uid=admin,ou=system"/>  
  5.   <property name="password" value="secret"/>  
  6. </bean>  

 接下来,我们需要配置稍微复杂一些的LdapAuthenticationProvider。

 

配置LdapAuthenticationProvider

         如果你已经读过并理解本章中关于Spring Security LDAP如何工作的讲解,那这个bean配置就很容易理解了,尽管稍有些复杂。我们将会配置一个包含如下特性的LdapAuthenticationProvider:

l  用户的凭证进行绑定认证(不是密码对比);

l  在UserDetailsContextMapper中使用InetOrgPerson

让我们开始吧——首先我们声明LdapAuthenticationProvider:

 

Xml代码  收藏代码
  1. <bean class="org.springframework.security.ldap.authentication.LdapAuthenticationProvider" id="ldapAuthProvider">  
  2.   <constructor-arg ref="ldapBindAuthenticator"/>  
  3.   <constructor-arg ref="ldapAuthoritiesPopulator"/>  
  4.   <property name="userDetailsContextMapper" ref="ldapUserDetailsConte  
  5. xtMapper"/>  
  6. </bean>  

 

 接下来是BindAuthenticator以及支持的FilterBasedLdapUserSearch bean(用来在绑定前在LDAP目录中定位用户的DN):

 

Xml代码  收藏代码
  1. <bean class="org.springframework.security.ldap.authentication.BindAuthenticator" id="ldapBindAuthenticator">  
  2.   <constructor-arg ref="ldapServer"/>  
  3.   <property name="userSearch" ref="ldapSearchBean"/>  
  4. </bean>  
  5. <bean class="org.springframework.security.ldap.search.FilterBasedLdapUserSearch"   
  6. id="ldapSearchBean">  
  7.   <constructor-arg value=""/> <!-- user-search-base -->  
  8.   <constructor-arg value="(uid={0})"/> <!-- user-search-filter -->  
  9.   <constructor-arg ref="ldapServer"/>  
  10. </bean>  

 

          最后是LdapAuthoritiesPopulator和UserDetailsContextMapper,它们的角色我们在前文中已经介绍过:

 

Xml代码  收藏代码
  1. <bean class="org.springframework.security.ldap.userdetails.DefaultLdapAuthoritiesPopulator"  
  2.  id="ldapAuthoritiesPopulator">  
  3.   <constructor-arg ref="ldapServer"/>  
  4.   <constructor-arg value="ou=Groups"/>  
  5.   <property name="groupSearchFilter" value="(uniqueMember={0})"/>  
  6. </bean>  
  7. <bean class="org.springframework.security.ldap.userdetails.InetOrgPersonContextMapper"  
  8.  id="ldapUserDetailsContextMapper"/>  

 

        最后需要配置的元素是对我们LdapAuthenticationProvider的引用,我们将会在dogstore-security.xml中通过引用进行声明:

 

Xml代码  收藏代码
  1. <authentication-manager alias="authenticationManager">  
  2.   <authentication-provider ref="ldapAuthProvider"/>  
  3. </authentication-manager>  

 到此时为止,我们已经使用明确Spring bean的方法配置了LDAP认证。正如我们在第六章:高级配置和扩展第一次引入明确bean配置那样,在LDAP集成中使用这项技术能够在security命名空间配置没有暴露特定属性时,或者需要自定义的实现类以满足特定业务场景的地方很有用。接下来,我们将会介绍一个这样的场景即怎样通过LDAP连接到Microsoft Active Directory。

 

通过LDAP集成Microsoft Active Directory

         Microsoft Active Directory的一个便利功能是它不仅能够很好的与基于Microsoft Windows网络的结构集成,还能够配置成使用LDAP协议保罗Active Directory内容。如果你们是使用Windows的公司,很可能任何的LDAP集成都是基于你的Active Directory实例。

         取决于你的Microsoft Active Directory配置(以及目录管理员支持Spring Security LDAP的意愿),你可能遇到的困难不在认证和绑定的过程中而是匹配Active Directory中的信息到Spring Security系统中的用户GrantedAuthority上。

         JBCP Pets公司的Active Directory LDAP树的实例在LDAP浏览器中的截图如下:



 在这里你没有看到前面实例LDAP结构中的ou=Groups——这是因为Active Directory在用户条目本身上以属性的方式存储分组信息。内置的Spring Security(本书出版之时)并没有提供支持典型Active Directory LDAP树的LdapAuthoritiesPopulator。

         让我们使用刚刚了解到的明确配置bean的方式来织入一个简单的LdapAuthoritiesPopulator实现,它只是简单的为每个LDAP认证的用户授予ROLE_USER角色。我们将这个类称为com.packtpub.springsecurity.security.SimpleRoleGrantingLdapAuthoritiesPopulator

 

Java代码  收藏代码
  1. public class SimpleRoleGrantingLdapAuthoritiesPopulator implements  
  2.     LdapAuthoritiesPopulator {  
  3.   protected String role = "ROLE_USER";  
  4.   public Collection<GrantedAuthority> getGrantedAuthorities(  
  5.       DirContextOperations userData, String username) {  
  6.     GrantedAuthority ga = new GrantedAuthorityImpl(role);  
  7.     return Arrays.asList(ga);   
  8.   }  
  9.   public String getRole() {  
  10.     return role;  
  11.   }  
  12.   public void setRole(String role) {  
  13.     this.role = role;  
  14.   }  
  15. }  

 并不十分令人兴奋,但是要记住如果需要的话你可以使用传入的o.s.ldap.core.DirContextOperations对象进行很多类型的LDAP操作,如查询目录获取用户的信息以及使用这些信息填充返回的角色。

         假设我们从上一节中的bean配置开始,让我们看一下怎样修改配置以支持Active Directory结构。

 

Xml代码  收藏代码
  1.   <bean class="org.springframework.security.ldap.  
  2. DefaultSpringSecurityContextSource" id="ldapServer">  
  3.     <constructor-arg value="ldap://corp.jbcppets.com/dc=corp,dc=jbcppets,dc=com"/>  
  4. <property name="userDn"   
  5. value="CN=Administrator,CN=Users,DC=corp,DC=jbcppets,DC=com"/>  
  6.     <property name="password" value="admin123!"/>  
  7.   </bean>  
  8.   <bean class="org.springframework.security.ldap.search.FilterBasedLdapUserSearch"  
  9.     id="ldapSearchBean">  
  10. <constructor-arg value="CN=Users"/>  
  11. <constructor-arg value="(sAMAccountName={0})"/>  
  12. <constructor-arg ref="ldapServer"/>  
  13. </bean>  
  14. <bean class="com.packtpub.springsecurity.security.SimpleRoleGrantingLdapAuthoritiesPopulator" id="ldapAuthoritiesPopulator"/>  

 属性sAMAccountName是在Active Directory中与标准LDAP条目的uid属性对等的——其它的配置修改都是典型的Active Directory配置。

         尽管大多数的Active Directory LDAP集成都可能比本例复杂,但这会给你一个深入理解和探索Spring Security LDAP内部原理的起点,会使得支持更复杂的集成会简单一点。

         注意存在另外一种与Active Directory集成的方式——Kerberos认证,它是Spring Security扩展项目的一部分,并会在第十二章:Spring Security扩展中涉及到。你可能会愿意了解两种类型的集成并确定哪种更适合你的需求(我们将会在第十二章中,比较Active Directory的LDAP和Kerberos认证)。

委托角色查询给UserDetailsService

         最后一项明确配置bean支持的技术就是在UserDetailsService中根据用户名查找用户并在此处获取GrantedAuthority。配置很简单就像声明一个bean,含有一个对UserDetailsService引用(就像基于JDBC的或我们在前面的章节中用到过的):

 

Xml代码  收藏代码
  1. <bean class="org.springframework.security.ldap.authentication.UserDetailsServiceLdapAuthoritiesPopulator" id="ldapAuthoritiesPopulator">  
  2.    <constructor-arg ref="jdbcUserServiceCustom"/>  
  3.  </bean>  

 你可能会预见到的逻辑和管理问题是用户名和角色必须同时在LDAP中和UserDetailsService所有的存储中管理——在大型用户场景下这可能不是一个灵活的模式。

         这种场景的通常用在需要LDAP认证确保应用中的用户即为公司用户,但是应用本身想存储授权信息。这会导致潜在的应用相关数据在LDAP目录以外,这可能会是出于分开操作的考虑。

小结

         我们已经看到在接受请求时,可以依赖LDAP服务器提供认证和授权信息,以及丰富的用户个人信息。在本章中,我们学到了:

l  LDAP术语和概念,并理解了LDAP 目录怎样组织以支持Spring Security;

l  在Spring Security配置文件中配置单独的(嵌入式的)和外部的LDAP服务器;

l  基于LDAP存储认证和授权用户,以及随后的匹配Spring Security角色;

l  LDAP中不同的认证模式、密码存储和安全机制——以及在Spring Security中怎样处理;

l  匹配LDAP目录中的用户详细信息到UserDetails对象中,实现LDAP和使用Spring应用的属性交换;

l  LDAP的明确bean配置,以及这种方式的优劣。

 

 

转自:iteye

作者:lengyun3566

链接:http://lengyun3566.iteye.com/blog/1358321