WebSecurityConfigurerAdapter
是默认情况下spring security的http配置
ResourceServerConfigurerAdapter
是默认情况下spring security oauth2
的http配置
WebSecurityConfigurerAdapter
部分源码如下:
@Order(100)
public abstract class WebSecurityConfigurerAdapter implements WebSecurityConfigurer<WebSecurity> {
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
...
}
public void configure(WebSecurity web) throws Exception {
}
protected void configure(HttpSecurity http) throws Exception {
...
}
...
}
ResourceServerConfigurerAdapter
部分源码如下:
public class ResourceServerConfigurerAdapter implements ResourceServerConfigurer {
@Override
public void configure(ResourceServerSecurityConfigurer resources) throws Exception {
}
@Override
public void configure(HttpSecurity http) throws Exception {
http.authorizeRequests().anyRequest().authenticated();
}
}
在ResourceServerProperties
中,定义了它的order
默认值为SecurityProperties.ACCESS_OVERRIDE_ORDER - 1;
,是大于100的,即WebSecurityConfigurerAdapter
的配置的拦截要优先于ResourceServerConfigurerAdapter
,优先级高的http配置是可以覆盖优先级低的配置的。
某些情况下如果需要ResourceServerConfigurerAdapter
的拦截优先于WebSecurityConfigurerAdapter
需要在配置文件中添加
security.oauth2.resource.filter-order=99
或者重写WebSecurityConfigurerAdapter
的Order
配置,如下:
@Configuration
@EnableWebSecurity
@Order(SecurityProperties.ACCESS_OVERRIDE_ORDER)
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
...
}