Spring 安全:手动恢复默认配置
Spring security: manually bring back default configuration
我有一个 spring 引导应用程序,可以与 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 : Authentication request failed: error="access_denied", error_description="Invalid token does not contain resource 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
。当根本没有配置时,它似乎被忽略了
我有一个 spring 引导应用程序,可以与 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 : Authentication request failed: error="access_denied", error_description="Invalid token does not contain resource 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
。当根本没有配置时,它似乎被忽略了