I'm trying to implement two different security configs (for admin and normal users). To achieve that, I have two classes that extend WebSecurityConfigurerAdapter - one for each user type.
My issue is that configuration for admin gets applied and works as expected with redirect to login page, but my other configuration doesn't seem to affect anything. Changing @Order annotation seems to not change anything. And logs show that on application deployment, both filters get applied to the chain.
My configuration classes:
@Configuration
@Order(1)
public class AdminConfigurationAdapter extends WebSecurityConfigurerAdapter {
@Override
public void configure(WebSecurity web) throws Exception {
web
.ignoring()
.antMatchers("/images/**", "/css/**", "/js/**", "/webjars/**", "**/favicon.ico");
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/admin/login")
.permitAll()
.antMatchers("/admin/**")
.hasRole("ADMIN")
.and()
.formLogin()
.loginPage("/admin/login")
.loginProcessingUrl("/admin/loginAction")
//.failureUrl("/loginAdmin?error=loginError")
.defaultSuccessUrl("/admin/dashboard", true)
.and()
.logout()
.logoutUrl("/admin/logoutAction")
//.logoutSuccessUrl("/protectedLinks")
.deleteCookies("JSESSIONID")
.and()
.exceptionHandling()
.accessDeniedPage("/403")
.and()
.csrf().disable();
}
@Override
protected void configure(final AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication()
.withUser("admin").password(passwordEncoder().encode("admin")).roles("ADMIN");
}
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
}
Second class:
@Configuration
@Order(2)
public class GameConfigurationAdapter extends WebSecurityConfigurerAdapter {
@Override
public void configure(WebSecurity web) throws Exception {
web
.ignoring()
.antMatchers("/images/**", "/css/**", "/js/**", "/webjars/**", "**/favicon.ico");
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/", "/game/login")
.permitAll()
.antMatchers("/game/**")
.hasRole("USER")
.and()
.formLogin()
.loginPage("/game/login")
.loginProcessingUrl("/game/login")
//.failureUrl("/game/login")
.defaultSuccessUrl("/game/home", true)
.and()
.logout()
.logoutUrl("/game/logout")
//.logoutSuccessUrl("/")
.deleteCookies("JSESSIONID")
.and()
.exceptionHandling()
.accessDeniedPage("/403")
.and()
.csrf().disable();
}
}