有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Spring安全性:手动恢复默认配置

我有一个spring boot应用程序,可以与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配置。怎么把它带回来


共 (1) 个答案

  1. # 1 楼答案

    我找到了答案。在@dur注释之后,我需要在资源服务器配置中设置资源id(如下所示)。我需要将其设置为令牌的aud字段中传递的一个资源

    @Configuration
    class Security extends ResourceServerConfigurerAdapter {
    
      @Override
      public void configure(ResourceServerSecurityConfigurer resources) throws Exception {
            resources.resourceId("my-resource-name");
      }
    }
    

    但我不知道为什么它在没有任何安全配置的情况下工作,因为spring的默认id是^{。当根本没有配置时,它似乎被忽略了