2017-08-06 2 views
0

Est-il possible d'avoir un fournisseur d'authentification ldap personnalisé avec les autorités ldap personnalisées populator? Je ne veux pas redémarrer mon application chaque fois que le serveur LDAP est inaccessible pendant un court instant (j'ai donc besoin du fournisseur personnalisé pour créer un nouveau contexte et remplacer la méthode d'authentification à chaque connexion).Printemps Sécurité personnalisé ldapAuthenticationProvider + personnalisé ldapAuthoritiesPopulator

De l'autre côté, je dois créer des rôles personnalisés pour chaque appartenance à l'utilisateur ldap (besoin de passer outre les getGrantedAuthorities)

Répondre

0

Pour la mise en œuvre fournisseur d'authentification ldap personnalisée, vous devez créer la classe qui va de AbstractLdapAuthenticator

public class BindPasswordAuthentificator extends AbstractLdapAuthenticator { 

    public BindPasswordAuthentificator(BaseLdapPathContextSource contextSource) { 
     super(contextSource); 
    } 

    @Override 
    public DirContextOperations authenticate(Authentication authentication) { 
     DirContextOperations user; 

     String username = authentication.getName(); 
     String password = (String)authentication.getCredentials(); 

     user = authenticateByLdap(username, password); // authenticate user here 

     if (user == null) { 
      throw new BadCredentialsException(
        messages.getMessage("BindAuthenticator.badCredentials", "Bad credentials")); 
     } 

     return user; 
    } 
} 

Et pour la mise en œuvre des autorités ldap vous Populator besoin de créer la classe qui va de LdapAuthoritiesPopulator

public class CustomLdapAuthoritiesPopulator implements LdapAuthoritiesPopulator { 

    @Override 
    public Collection<? extends GrantedAuthority> getGrantedAuthorities(DirContextOperations userData, String username) { 
     Collection<GrantedAuthority> gauth = new HashSet<>(); 
     //you need to place logic for populating user authorities here 
     return gauth; 
    } 
} 

Ensuite, vous devez configurer ces deux classes dans votre configuration

@Configuration 
@PropertySource("classpath:application.properties") 
@Order(SecurityProperties.ACCESS_OVERRIDE_ORDER) 
public class WebSecurityConfig extends WebSecurityConfigurerAdapter { 

    @Value("${life.ldap.server}") 
    private String ldapServer; 

    @Autowired 
    public void globalUserDetails(final AuthenticationManagerBuilder auth) throws Exception { 
     auth.authenticationProvider(ldapAuthenticationProvider()); 
    } 

    @Bean 
    public LdapAuthenticationProvider ldapAuthenticationProvider() { 
     return new LdapAuthenticationProvider(authentificator(), authPopulator()); 
    } 

    @Bean 
    public BindPasswordAuthentificator authentificator() { 
     return new BindPasswordAuthentificator(contextSource()); 
    } 

    @Bean 
    public DefaultSpringSecurityContextSource contextSource() { 
     return new DefaultSpringSecurityContextSource(ldapServer); 
    } 

    @Bean 
    public CustomLdapAuthoritiesPopulator authPopulator() { 
     CustomLdapAuthoritiesPopulator result = new CustomLdapAuthoritiesPopulator(); 
     return result; 
    } 

    @Override 
    @Bean 
    public AuthenticationManager authenticationManagerBean() throws Exception { 
     return super.authenticationManagerBean(); 
    } 

    @Override 
    protected void configure(final HttpSecurity http) throws Exception { 
     http.authorizeRequests().antMatchers("/login").permitAll() 
       .antMatchers("/oauth/token/revokeById/**").permitAll() 
       .antMatchers("/tokens/**").permitAll() 
       .anyRequest().authenticated() 
       .and().formLogin().permitAll() 
       .and().csrf().disable(); 
    } 
}