Вот код, который я получил с работы, который обеспечивает настройку безопасности Spring с использованием LDAP в качестве поставщика аутентификации. Может быть, это даст вам некоторое представление о том, как заставить это работать. Я добавил несколько комментариев, чтобы описать некоторые ключевые моменты реализации. Это код из моего класса конфигурации, который расширяет WebSecurityConfigurerAdapter
.
@Override
protected void configure(HttpSecurity http) throws Exception {
http.cors().and()
.authorizeRequests()
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage("/login") // defines the login page
.loginProcessingUrl("/userAuth") // defines the endpoint which the post with the login form must be sent to.
.permitAll()
.and()
.logout()
.permitAll()
}
@Override
public void configure(AuthenticationManagerBuilder auth) {
auth.authenticationProvider(activeDirectoryLdapAuthenticationProvider()); // defines LDAP as your authentication provider.
}
@Bean
public AuthenticationProvider activeDirectoryLdapAuthenticationProvider() {
ActiveDirectoryLdapAuthenticationProvider authenticationProvider = new ActiveDirectoryLdapAuthenticationProvider(DOMAIN, URL, ROOT_DN); // instantiate and connects to your LDAP provider. DOMAIN, URL and ROOT_DN must be info you have from your LDAP.
authenticationProvider.setConvertSubErrorCodesToExceptions(true);
authenticationProvider.setUseAuthenticationRequestCredentials(true);
return authenticationProvider;
}
При такой реализации любая форма входа, начиная с /login
и публикуемая на /userAuth
, будет аутентифицироваться на вашем сервере LDAP. Поэтому вам не следует беспокоиться о шифровании паролей или о чем-либо подобном. Провайдер аутентификации LDAP поможет вам.