【问题标题】:AWS Cognito Angular SpringBoot Oauth2 - invalid_token errorAWS Cognito Angular SpringBoot Oauth2 - invalid_token 错误
【发布时间】:2019-04-11 10:34:09
【问题描述】:

我们将 AWS Cognito 用于 Oauth2。我们的 UI 是基于 Angular 构建的。在我的用户登录后,我发起对 Cognito 的调用以获取授权令牌。我正在使用带有 PKCE 的授权代码授予从 Cognito 获取令牌。从 Cognito 获得令牌后,我调用了我的 Spring Boot REST 服务。从 Angular 调用 Spring Boot 服务时,我将 Authorization 标头中的令牌作为“Bearer”令牌发送。

这是我的 ResourceServerConfiguration.java:

import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer;
import org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurerAdapter;
import org.springframework.security.oauth2.config.annotation.web.configurers.ResourceServerSecurityConfigurer;

@Configuration
@EnableResourceServer
public class ResourceServerConfiguration extends ResourceServerConfigurerAdapter {
    private static final String RESOURCE_ID = "resource-server-rest-api";
    private static final String SECURED_READ_SCOPE = "#oauth2.hasScope('openid')";
    private static final String SECURED_WRITE_SCOPE = "#oauth2.hasScope('openid')";
    private static final String SECURED_PATTERN = "/**";
    @Override
    public void configure(ResourceServerSecurityConfigurer resources) {
        resources.resourceId(RESOURCE_ID);
    }
    @Override
    public void configure(HttpSecurity http) throws Exception {
        http.requestMatchers()
                .antMatchers(SECURED_PATTERN).and().authorizeRequests()
                .antMatchers(HttpMethod.POST, SECURED_PATTERN).access(SECURED_WRITE_SCOPE)
                .anyRequest().access(SECURED_READ_SCOPE);
    }
}

调用 REST 服务时,我的 Angular UI 收到 HTTP 响应 401,并显示以下错误消息:

DEBUG o.s.s.o.p.a.OAuth2AuthenticationProcessingFilter - Authentication request failed: error="invalid_token", error_description="Invalid access token: eyJraWQiOiIy.......
  1. 知道为什么我会收到 invalid_token 吗?
  2. spring 是否会调用 Cognito 来验证令牌?
  3. 我没有将令牌存储在我的 REST 服务层中。这是必需的吗?
  4. 我使用 logging.level.root=DEBUG 启用了 DEBUG。但我在输出中没有看到描述性消息。如何解决此问题?

提前感谢您为解决此问题提供的任何帮助。

【问题讨论】:

  • 解决这个问题好运吗?

标签: spring angular spring-boot amazon-cognito spring-security-oauth2


【解决方案1】:

以下更改对我有用

  • 删除@EnableResourceServer

  • 将以下内容添加到您的 Spring 安全配置中

 http.authorizeRequests().antMatchers(HttpMethod.OPTIONS,"**").permitAll()
            .anyRequest()
                .authenticated()
                .and().oauth2ResourceServer().jwt();
  • 向 pom.xml 添加以下 2 个依赖项
    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-oauth2-resource-server</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-oauth2-jose</artifactId>
    </dependency>
  • 最后将以下属性添加到您的 application.yml
    spring:
      security:
        oauth2:
          resourceserver:
            jwt:
              issuer-uri: https://cognito-idp.us-east-1.amazonaws.com/{{userpoolid}}
    com:
      ixortalk:
        security:
          jwt:
            aws:
              userPoolId: {{userpoolid}}
              region: "us-east-1"

【讨论】:

    猜你喜欢
    • 2014-01-26
    • 1970-01-01
    • 2020-08-17
    • 2021-09-24
    • 2019-09-12
    • 2017-10-15
    • 1970-01-01
    • 1970-01-01
    • 2020-08-06
    相关资源
    最近更新 更多