[spring security] - LDAP configuration - Bind approach

Springsecurity LDAP authentication sample XML.


   
   
	
    
    

   
   


   
   

   
   


   
   
	
    
    
		
     
     
			
      
      
			
      
      
				
       
        
         
        
          uid={0},ou=People 
         
       
			
      
      
			
      
      
				
       
        
         
        
          cn 
         
        
          sn 
         
        
          uid 
         
       
			
      
      
		
     
     
	
    
    
	
    
    
	
    
    

   
   


   
   
	
    
    

   
   

1. Implements UserDetailsContextMapper

public class UserDetailsContextMapperImpl implements UserDetailsContextMapper {
	
	@Override
	public UserDetails mapUserFromContext(DirContextOperations ctx, String username, Collection
    
     authorities) {
		
		//How to get attribute from DirContextOperations
		try {
			System.out.println("Get DN size: " + ctx.getDn().size());
			for(int i=0; i< ctx.getDn().size(); i++){
				System.out.println("DN[" + i + "] = " + ctx.getDn().get(i));
			}
			
			System.out.println("NameInNamespace = " + ctx.getNameInNamespace());
			
			Attributes attributes = ctx.getAttributes();
			NamingEnumeration
    
    
     
      ne = attributes.getIDs();
			while(ne.hasMore()){
				String id = ne.next();
				//System.out.println("id = " + id + ", value = " + ctx.getStringAttribute(id));
				//ctx.getStringAttribute(id) is not safe, if attribute is not String object it will throw exception.
				
				Attribute attribute = attributes.get(id);
				System.out.println("attribute id:" + id + ", attribute size: " + attribute.size());
				for(int j = 0; j < attribute.size(); j++){
					System.out.println("        attribute[" + j + "] = " + attribute.get(j));
				}
			}
			
		} catch (NamingException e) {
			e.printStackTrace();
		}
		
		//Decide where you want to get user detail information. DB or anywhere you want
		UserDetails ud = ...;
		
		return ud;
	}

}

    
    

2. Implements LdapAuthoritiesPopulator

/**
 * Only return "ROLE_USER"
 */
public class LdapAuthoritiesPopulatorImpl implements LdapAuthoritiesPopulator {
	
	public static final String ROLE_USER = "ROLE_USER";
    
	public Collection
    
     getGrantedAuthorities(DirContextOperations userData, String username) {

		return new ArrayList
    
    
     
     () {
			private static final long serialVersionUID = 1L;

			{
				add(new SimpleGrantedAuthority(ROLE_USER));
			}
		};
	}
}

    
    

3. UserDN pattern

According to practical how do you manage user information on LDAP server.
For example, uid={0},ou=Users

4. User attributes

Attributes will return to application from LDAP server after success authentication. If you don't specify LDAP server will return all the attributes of this authenticated user. 
For example, uid, cn and sn etc.
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值