我的目标是以以下方式配置Spring安全性:
从私有开始的任何路由都应该通过Spring oauth2ResourceServer
我已经尝试了下面的代码,但这给了我一个问题,即它也试图验证非私有的其他路由。
@Configuration
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.cors().and()
.authorizeRequests()
.antMatchers("/private/**").authenticated()
.antMatchers("/**").permitAll()
.and()
.oauth2ResourceServer().jwt();
}
}
我的依赖关系是:
有什么想法吗?
发布于 2022-01-26 11:07:14
结果被csrf保护所阻塞,这在春季安全中是默认的。
以下是我的工作:
@Override
public void configure(HttpSecurity http) throws Exception {
http
.cors().and()
.csrf().disable()
.authorizeRequests()
.mvcMatchers("/private/**").authenticated()
.mvcMatchers("/**").permitAll()
.and()
.oauth2ResourceServer().jwt();
}
请注意,要使其工作,您需要在application.properties
中指定以下内容。
spring.security.oauth2.resourceserver.jwt.jwk-set-uri
例如,在google oauth2的例子中,这是:
spring.security.oauth2.resourceserver.jwt.jwk-set-uri=https://www.googleapis.com/oauth2/v3/certs
这指向用于验证令牌的JSON密钥(JWK)。您的令牌应该以以下形式作为授权头发送到您的spring服务器:
bearer {{your token here}}
https://stackoverflow.com/questions/70860005
复制相似问题