Skip to content

Instantly share code, notes, and snippets.

@efraimcf
Created May 26, 2016 17:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save efraimcf/8499667ff876c5394ed88581dbdd83f7 to your computer and use it in GitHub Desktop.
Save efraimcf/8499667ff876c5394ed88581dbdd83f7 to your computer and use it in GitHub Desktop.
@Configuration
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
private static final Logger LOGGER = LoggerFactory.getLogger(SecurityConfiguration.class);
@Autowired
private WebAuthenticationProvider authenticationProvider;
@Autowired
private LoginAuthenticationSuccessHandler loginAuthenticationSuccessHandler;
@Autowired
private LoginAuthenticationFailureHandler loginAuthenticationFailureHandler;
@Override
public void configure(WebSecurity web) throws Exception {
web.ignoring().antMatchers( "/auth**", "/logout", "/login", "/signup",
"/css/**", "/js/**", "/img/**", "/healthcheck", "/healthcheck/**");
}
@Override
protected void configure(final HttpSecurity http) throws Exception {
LOGGER.info("CONFIGURE WEB SECURITY");
http
.authorizeRequests()
.antMatchers( "/admin**" )
.hasAnyRole( "ADMIN" )
.and()
.authorizeRequests()
.antMatchers(HttpMethod.OPTIONS)
.permitAll()
.and()
.authorizeRequests()
.anyRequest()
.hasAnyRole("USER")
.and()
.formLogin()
.loginPage( "/login" )
.permitAll()
.loginProcessingUrl( "/login.action" )
.usernameParameter( "username" )
.passwordParameter( "password" )
.successHandler( loginAuthenticationSuccessHandler )
.failureHandler( loginAuthenticationFailureHandler )
.and()
.logout()
.permitAll()
.logoutRequestMatcher(new AntPathRequestMatcher("/logout"))
.logoutSuccessUrl( "/login" )
.and()
.exceptionHandling()
.accessDeniedPage( "/erro?code=999" )
.and()
.csrf()
.disable();
}
/**
* Register application's authentication provider
* @param auth
* @throws Exception
*/
@Autowired
public void configureGlobal(final AuthenticationManagerBuilder auth) throws Exception {
auth.authenticationProvider(authenticationProvider);
}
@Configuration
@Order(1)
public static class RestSecurityConfiguration extends WebSecurityConfigurerAdapter {
private static final Logger LOGGER2 = LoggerFactory.getLogger(RestSecurityConfiguration.class);
@Override
protected void configure(final HttpSecurity http) throws Exception {
LOGGER2.info("CONFIGURE REST SECURITY");
http
.antMatcher("/api/**")
.authorizeRequests()
.antMatchers(HttpMethod.OPTIONS)
.permitAll()
.antMatchers("/api/**")
.authenticated()
.and()
.httpBasic()
.and()
.csrf()
.disable();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment