【发布时间】:2019-05-28 16:52:02
【问题描述】:
我使用 Spring Security OAuth2 设置了一个 Spring Boot 多模块(5 个模块)应用程序。一切正常,但随着应用程序的增长,我想将每个模块中的安全部分分开。主模块启用一切:
@SpringBootApplication
@EnableResourceServer
@EnableAuthorizationServer
@EnableWebSecurity(debug = true)
public class Application {
...
}
现在我在每个模块中定义了一个 ResourceServerConfigurer 类型的 bean
@Configuration
@Order(2)
public class Module1SecurityFilterChain extends ResourceServerConfigurerAdapter {
@Override
public void configure( HttpSecurity http ) throws Exception {
http.sessionManagement().sessionCreationPolicy( STATELESS );
http.antMatcher( "/module1/**")
.authorizeRequests()
.antMatchers( "/module1/resource").authenticated()
.antMatchers( "/module1/test" ).authenticated()
.anyRequest().access( "#oauth2.hasScope('webclient')" );
}
}
与模块 2 相同:
@Configuration
@Order(1)
public class Module2SecurityFilterChain extends ResourceServerConfigurerAdapter {
@Override
public void configure( HttpSecurity http ) throws Exception {
http.sessionManagement().sessionCreationPolicy( STATELESS );
http.antMatcher( "/module2/**")
.authorizeRequests()
.antMatchers( "/module2/resource").authenticated()
.antMatchers( "/module2/test" ).authenticated()
.anyRequest().access( "#oauth2.hasScope('webclient')" );
}
}
等等……
问题是只注册了一个FilterChain,即@Order(2)。我查看了ResourceServerConfigurer 的doc 并指出:
...如果不止一个配置相同的preoperty,那么最后一个获胜。配置器在应用前按顺序排序
如何才能绕过此限制? 非常感谢。
编辑
这样做(扩展 WebSecurityConfigurerAdapter 而不是 ResourceServerConfigurerAdapter):
@Configuration
@Order(1)
public class Module2SecurityFilterChain extends WebSecurityConfigurerAdapter {...}
似乎注册了过滤器链,但还有另一个问题,当我验证用户身份时(在/oauth/token 上获取令牌)我无法访问受此链保护的资源,我得到了403 Forbidden。这个黑匣子是如何工作的?
【问题讨论】:
标签: spring-boot spring-security spring-security-oauth2