http://hanqunfeng.iteye.com/blog/2089658

前面介绍过基于本地数据库验证的方式,参考http://hanqunfeng.iteye.com/blog/1155226,这里说一下如何修改为使用AD进行身份验证【只对用户名和密码进行验证,权限依旧存储在本地数据库中】。

 

将配置文件中的如下部分删除:

Xml代码   收藏代码
  1. <!-- 认证管理器,使用自定义的UserDetailsService,并对密码采用md5加密-->    
  2.   <authentication-manager>    
  3.       <authentication-provider user-service-ref="userService">    
  4.           <password-encoder hash="md5" />    
  5.       </authentication-provider>    
  6.   </authentication-manager>    
  7.   
  8.   <beans:bean id="userService" class="com.piaoyi.common.security.UserService" />  

 并添加如下内容:

Xml代码   收藏代码
  1. <!-- ldap contextSource ldap服务器 -->  
  2.     <!-- 继承于org.springframework.ldap.core.support.LdapContextSource -->  
  3.     <beans:bean id="contextSource"  
  4.         class="org.springframework.security.ldap.DefaultSpringSecurityContextSource">  
  5.         <beans:constructor-arg value="ldap://192.168.159.xxx:389" />  
  6.         <beans:property name="userDn"  
  7.             value="cn=admin,cn=Users,dc=piaoyi,dc=local" />  
  8.         <beans:property name="password" value="xxxxxxx" />  
  9.     </beans:bean>  
  10.   
  11.     <!-- ldap 认证代理 -->  
  12.     <beans:bean id="ldapAuthProvider"  
  13.         class="org.springframework.security.ldap.authentication.LdapAuthenticationProvider">  
  14.         <beans:constructor-arg ref="ldapBindAuthenticator" />  
  15.         <beans:constructor-arg ref="ldapAuthoritiesPopulator" />  
  16.     </beans:bean>  
  17.   
  18.     <!-- 用户认证:凭证绑定 -->  
  19.     <beans:bean id="ldapBindAuthenticator"  
  20.         class="org.springframework.security.ldap.authentication.BindAuthenticator">  
  21.         <beans:constructor-arg ref="contextSource" />  
  22.         <beans:property name="userSearch" ref="userSearch" />  
  23.     </beans:bean>  
  24.   
  25.     <!-- 用户查询规则 -->  
  26.     <beans:bean id="userSearch"  
  27.         class="org.springframework.security.ldap.search.FilterBasedLdapUserSearch">  
  28.         <beans:constructor-arg index="0"  
  29.             value="cn=Users,dc=piaoyi,dc=local" />  
  30.         <beans:constructor-arg index="1"  
  31.             value="(sAMAccountName={0})" />  
  32.         <beans:constructor-arg index="2" ref="contextSource" />  
  33.     </beans:bean>  
  34.       
  35.     <!-- 角色控制,这里授予所有AD验证通过的用户HODLE角色 -->  
  36.     <beans:bean  
  37.         class="com.netqin.common.security.SimpleRoleGrantingLdapAuthoritiesPopulator"  
  38.         id="ldapAuthoritiesPopulator" />  
  39.   
  40.   
  41.     <!-- 认证管理器,如果使用基于cookie的<remember-me/>,需要声明LdapUserDetailsService -->  
  42.     <authentication-manager>  
  43.         <authentication-provider ref="ldapAuthProvider" />  
  44.     </authentication-manager>  
  45.   
  46.         <!-- ldapUserDetailsService 用于<remember-me user-service-ref="ldapUserDetailsService"/> -->  
  47.         <beans:bean id="ldapUserDetailsService" class="org.springframework.security.ldap.userdetails.LdapUserDetailsService">  
  48.         <beans:constructor-arg index="0" ref="userSearch" />  
  49.         <beans:constructor-arg index="1" ref="ldapAuthoritiesPopulator" />  
  50.     </beans:bean>  

 这里仅使用到一个自定义类,它只有一个作用,就是授予所有用户HODLE角色,而真正的权限验证还是交给投票器处理

SimpleRoleGrantingLdapAuthoritiesPopulator

Java代码   收藏代码
  1. package com.netqin.common.security;  
  2. import java.util.Arrays;  
  3. import java.util.Collection;  
  4. import org.springframework.ldap.core.DirContextOperations;  
  5. import org.springframework.security.core.GrantedAuthority;  
  6. import org.springframework.security.core.authority.SimpleGrantedAuthority;  
  7. import org.springframework.security.ldap.userdetails.LdapAuthoritiesPopulator;  
  8.   
  9. public class SimpleRoleGrantingLdapAuthoritiesPopulator implements  
  10.         LdapAuthoritiesPopulator {  
  11.     protected String role = "HODLE";  
  12.   
  13.   
  14.     public Collection<GrantedAuthority> getGrantedAuthorities(  
  15.             DirContextOperations userData, String username) {  
  16.         GrantedAuthority ga = new SimpleGrantedAuthority(role);  
  17.         return Arrays.asList(ga);  
  18.     }  
  19.   
  20.     public String getRole() {  
  21.         return role;  
  22.     }  
  23.   
  24.     public void setRole(String role) {  
  25.         this.role = role;  
  26.     }  
  27. }  

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值