Skip to content

Instantly share code, notes, and snippets.

@Cameron-C-Chapman
Last active October 10, 2016 20:10
Show Gist options
  • Save Cameron-C-Chapman/4809a3df19030e2efee6 to your computer and use it in GitHub Desktop.
Save Cameron-C-Chapman/4809a3df19030e2efee6 to your computer and use it in GitHub Desktop.
Spring Security LDAP Config
package package.name;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.ldap.authentication.ad.ActiveDirectoryLdapAuthenticationProvider;
import org.springframework.security.ldap.userdetails.InetOrgPersonContextMapper;
import custom.security.classes.package.AccessDeniedHandlerImpl;
import custom.security.classes.package.AuthenticationFailureHandlerImpl;
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Value("${ldapauthenticationprovider.domain}")
private String domain;
@Value("${ldapauthenticationprovider.url}")
private String url;
@Value("${authorities}")
private String authorities;
@Autowired
Environment environment;
@Autowired
AuthenticationFailureHandlerImpl authenticationFailureHandlerImpl;
@Autowired
AccessDeniedHandlerImpl accessDeniedHandlerImpl;
@Bean
public ActiveDirectoryLdapAuthenticationProvider activeDirectoryLdapAuthenticationProvider() {
ActiveDirectoryLdapAuthenticationProvider authenticationProvider = new ActiveDirectoryLdapAuthenticationProvider(domain, url);
authenticationProvider.setUserDetailsContextMapper(new InetOrgPersonContextMapper());
authenticationProvider.setConvertSubErrorCodesToExceptions(true);
return authenticationProvider;
}
@Override
protected void configure(AuthenticationManagerBuilder authenticationManagerBuilder) {
authenticationManagerBuilder.authenticationProvider(activeDirectoryLdapAuthenticationProvider());
}
@Override
public void configure( WebSecurity web ) throws Exception
{
// ensure that the static content (js, css) is accessible from the login page without authentication
web.
ignoring().
antMatchers( "/dist/**","/fonts/**" );
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.
exceptionHandling().
accessDeniedHandler(accessDeniedHandlerImpl).
and().
authorizeRequests().
antMatchers( "/login" ).permitAll().
antMatchers("/", "/logout").hasAnyAuthority(authorities.split(",")).
anyRequest().authenticated().
and().
formLogin().
loginPage("/login").
usernameParameter("username").
passwordParameter("password").
defaultSuccessUrl("/").
failureHandler(authenticationFailureHandlerImpl).
and().
logout().
logoutUrl("/logout").
invalidateHttpSession(true).
logoutSuccessUrl("/login");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment