2017-03-01 83 views
0

使用Spring Security LDAP及其验证正常,但现在我需要从LDAP条目加载userLevel属性来确定用户的级别。Spring Security LDAP从LDAP加载角色

我的春季安全配置看起来像这样

@Profile(value = {"sit", "uat", "prod"}) 
@Configuration 
@EnableWebSecurity 
public class SecurityConfig extends WebSecurityConfigurerAdapter { 

    @Autowired 
    protected Environment environment; 

    public SecurityConfig() { 
     super(); 
    } 

    @Override 
    protected void configure(AuthenticationManagerBuilder auth) throws Exception { 
     auth.ldapAuthentication() 
       .userSearchBase("dc=fantasycompany,dc=com") 
       .userDnPatterns("cn={0},ou=users,ou=somedepartment,o=departments,c=US,dc=fantasycompany,dc=com") 
       .contextSource() 
        .url("ldaps://someserver:636") 
        .managerDn("cn=someone,cn=users,dc=fantasycompany,dc=com") 
        .managerPassword("somethingsomething"); 
    } 

    @Override 
    protected void configure(HttpSecurity httpSecurity) throws Exception { 
     /**httpSecurity.authorizeRequests() 
       .anyRequest().fullyAuthenticated() 
       .and() 
       .formLogin() 
        .loginPage("/login") 
        .loginProcessingUrl("/perform_login") 
        .defaultSuccessUrl("/",true) 
        .failureUrl("/login.html?error=true");*/ 

     httpSecurity 
       .csrf().disable() 
       .authorizeRequests() 
       .antMatchers("/login*").permitAll() 
       .antMatchers("/css/*").permitAll() 
       .anyRequest().authenticated() 
       .and() 
       .formLogin() 
       .loginPage("/login") 
       .defaultSuccessUrl("/",true) 
       .failureUrl("/login?error=true") 
       .and() 
       .logout() 
       .logoutUrl("/logout") 
       .deleteCookies("JSESSIONID"); 
    } 
} 

我怎么能够让Spring加载从LDAP进入角色的UserLevel财产?

回答

0

您需要一个自定义的LdapAuthoritiesPopulator来读取一个属性(例如,当使用AD时'memberOf'或使用OpenDJ时'isMemberOf')来提取'角色'。

ActiveDirectoryLdapAuthenticationProvider在不使用LdapAuthoritiesPopulator的情况下执行此操作。

相关问题