I'm trying to implement a simple spring boot project. I got several REST-Endpoints which I've to secure differently. One has to be secured by Basic Auth, another one with OAuth and one with a custom security implementation.
REST-Endpoints:
- /basic/auth
- /application/secure (oauth)
- /application/secure2 (own implementation)
From tutorials, I know I've to set the order of the security adapters. My first intention was to set the order in steps of ten (e.g. @Order(10)
, @Order(20)
) in case I need to add other security filters in between. By doing so I investigated the following behavior:
- If I add the basic auth filter with
@Order(10)
and an OAuth filter with@Order(20)
only the OAuth filter works. - If I add the basic auth filter with
@Order(1)
or@Order(2)
and an OAuth filter with@Order(4)
both filters works. - If I add a filter to
@Order(3)
I receive an error which says, that order 3 is already in use and cannot be configured twice.
So there is a default spring security adapter (or whatever) which has the default order 3. I thought I disable every default spring security behavior by adding @EnableWebSecurity
. After I did not find an answer by google my questions would be:
- Am I doing the right things?
- What is this security adapter with order 3 by spring?
- Does the default security adapter block my basic auth implementation?
WebSecurityConfig:
@Configuration
@EnableWebSecurity
public class WebSecurityConfig {
@Order(10)
@Configuration
public class BasicAuthConfig extends WebSecurityConfigurerAdapter {
@Value("${security.user.password}")
private String password;
@Value("${security.user.name}")
private String username;
private static final String ROLE_ADMIN = "ADMIN";
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication().withUser(username).password(password).roles(ROLE_ADMIN);
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable();
http.requestMatchers().antMatchers("/basic/**", "/") //
.and().authorizeRequests().anyRequest().authenticated() //
.and().httpBasic();
}
}
@Order(20)
@Configuration
@EnableResourceServer
@EnableGlobalMethodSecurity(prePostEnabled = true)
protected static class Oauth2ServerConfig extends ResourceServerConfigurerAdapter {
@Override
public void configure(HttpSecurity http) throws Exception {
System.out.println("Filter called");
// @formatter:off
http.csrf().disable();
http.authorizeRequests().antMatchers("/application/**").authenticated()
// .antMatchers(GET, "/application/secure").authenticated()
.anyRequest().authenticated();
// @formatter:on
}
// offline token validator
}