【发布时间】:2019-05-27 23:42:56
【问题描述】:
我有弹簧启动应用程序。我已经配置了OAuth2 - 授权和资源服务器(分开)。在资源服务器 (application.properties) 我有:
server.servlet.context-path=/api
还有:
@Configuration
@EnableResourceServer
public class ResourceServerConfig extends ResourceServerConfigurerAdapter {
(...)
@Override
public void configure(HttpSecurity http) throws Exception {
http
.requestMatchers()
.and()
.authorizeRequests()
.antMatchers("/actuator/**", "/api-docs/**").permitAll()
.antMatchers("/api/**" ).authenticated();
}
}
问题在于,api 实际上根本不安全。感谢 doc 和 @dur's answer 我知道
模式不能包含上下文路径
确实,从:
.antMatchers("/api/**" ).authenticated();
到:
.antMatchers("/**" ).authenticated();
工作正常。但问题是:在这个用例中是否可以使用上下文路径,而不是使用 /** ?我可以为每个控制器重复.antMatchers()(或使用/**),但也许有办法使用上下文路径?
【问题讨论】:
-
2.0.5.RELEASE -
为什么需要上下文路径?为什么你认为它更好?但是,您始终可以实现自己的
RequestMatcher。 -
我认为当你使用特定的上下文
/api/**比使用/**时,它更具可读性。这让我觉得我没有意外影响/api/**之外的东西。第二个原因可能是我有多个上下文并希望对每个上下文应用不同的安全逻辑。
标签: spring spring-boot spring-security spring-oauth2