我有弹簧引导申请。我已经配置了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实际上根本没有安全。多亏了医生和@dur's answer我知道
模式不能包含上下文路径。
实际上,从以下几个方面发生了变化:
.antMatchers("/api/**" ).authenticated();至:
.antMatchers("/**" ).authenticated();效果很好。但问题是:在这个用例中是否可以使用上下文路径,而不是使用/**?我可以对每个控制器重复.antMatchers() (或者使用/**),但是也许有一种方法可以使用上下文路径?
发布于 2021-10-08 17:04:46
.requestMatchers().and()什么也不做,所以您可以删除它。这在lambda表示法中也更为明显:.requestMatchers(matchers -> matchers)。@Configuration
@EnableResourceServer
public class ResourceServerConfig extends ResourceServerConfigurerAdapter {
(...)
@Value("${server.servlet.context-path:''}")
private String contextPath; // <<<< I am the path !
@Override
public void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests(authorize -> authorize
.antMatchers(contextPath + "/actuator/**", "/api-docs/**").permitAll()
.antMatchers(contextPath + "/**" ).authenticated()
);
}
}但是如果你真的想要的话,你也可以用旧的方式写代码。它对使用变量没有任何影响。:
http.
.authorizeRequests()
.antMatchers(contextPath + "/actuator/**", "/api-docs/**")
.permitAll()
.antMatchers(contextPath + "/**" )
.authenticated()
.and());https://stackoverflow.com/questions/53987484
复制相似问题