我有一个弹簧启动应用程序,可以正常使用OAuth2(作为资源服务器)。没有自定义的configure(HttpSecurity http)
方法。只要
spring-boot-starter-security
spring-security-oauth2
spring-security-jwt
被添加到pom。
我需要添加应该不受保护的端点。所以我添加了自定义配置:
@Configuration
public class Security extends ResourceServerConfigurerAdapter {
@Override
public void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers(GET, "/public/**").anonymous()
.anyRequest().authenticated()
}
}
现在/public/**
端点正确地为所有人开放。但所有其他端点停止工作,在调试级别,我看到:
p.a.OAuth2AuthenticationProcessingFilter:身份验证请求失败:error =“access_denied”,error_description =“无效令牌不包含资源ID(oauth2-resource)”
所以我想我不知何故错误地覆盖了默认的OAuth2配置。怎么把它带回来?
我找到了答案。在@dur注释之后,我需要在资源服务器配置中设置资源ID(如下所示)。我需要将它设置为令牌的aud
字段中传递的资源之一。
@Configuration
class Security extends ResourceServerConfigurerAdapter {
@Override
public void configure(ResourceServerSecurityConfigurer resources) throws Exception {
resources.resourceId("my-resource-name");
}
}
但我不知道为什么它没有任何安全配置,因为spring的默认id是oauth2-resource
。当没有配置时,它似乎被忽略了