如何验证从 Cognito 获得的 jwt 令牌

Posted

技术标签:

【中文标题】如何验证从 Cognito 获得的 jwt 令牌【英文标题】:How do I validate a jwt token that I got from Cognito 【发布时间】:2018-09-04 10:12:32 【问题描述】:

我有一个 jwt 令牌,我在我的用户登录后从 cognito 检索到。

我的应用程序中有一个特定的 api 端点,我希望只有具有有效 jwt 的用户能够访问这个端点。我尝试查看网络上的各种资源,但我无法理解任何内容。我是 jwt 概念的新手。

PS 我有一个 Java 应用程序(spring boot)。如果有人能详细描述我需要遵循的步骤来验证我的 jwt,我将不胜感激。如果可能,请提供代码。

@CrossOrigin
@RequestMapping(value= "/login", method=RequestMethod.POST,consumes="application/json")
@ResponseBody
public String authenticate(@RequestBody SignInDTO signInDetails)

    //boolean isAuthenticated=false;
        CognitoHelper cognitoHelper=new CognitoHelper();
        String authResult=cognitoHelper.ValidateUser(signInDetails.getEmailId(), signInDetails.getPassword());
.....
.....
.....

authResult 是我从 cognito 获得的 jwt。在此之后,我完全不知道需要做什么。

【问题讨论】:

【参考方案1】:

使用像 java-jwt 这样的库(我猜你正在使用 Maven)

<dependency>
    <groupId>com.auth0</groupId>
    <artifactId>java-jwt</artifactId>
    <version>3.3.0</version>
</dependency>

然后:

String token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXUyJ9.eyJpc3MiOiJhdXRoMCJ9.AbIJTDMFc7yUa5MhvcP03nJPyCPzZtQcGEp-zWfOkEE";
try 
    Algorithm algorithm = Algorithm.HMAC256("secret");
    // or
    Algorithm algorithm = Algorithm.RSA256(publicKey, privateKey);
    JWTVerifier verifier = JWT.require(algorithm)
        .withIssuer("auth0")
        .build(); //Reusable verifier instance
    DecodedJWT jwt = verifier.verify(token);
 catch (UnsupportedEncodingException exception)
    //UTF-8 encoding not supported
 catch (JWTVerificationException exception)
    //Invalid signature/claims

您可以在此处手动解码jwt-token:https://jwt.io 更多关于java-jwt的信息在这里:https://github.com/auth0/java-jwt

【讨论】:

在哪里可以获得公钥和私钥? 亚马逊某处。试试这个:docs.aws.amazon.com/cognito/latest/developerguide/…【参考方案2】:

Here's 如何验证令牌(它是用 Kotlin 编写的)。

这是密钥所在的位置:

https://cognito-idp.$regionName.amazonaws.com/$cognitoUserPoolId/.well-known/jwks.json

我在这里实现了很多: https://github.com/awslabs/cognito-proxy-rest-service

【讨论】:

【参考方案3】:

Spring Security 5.1 引入了对此的支持,因此实现起来要容易得多。见https://docs.spring.io/spring-security/site/docs/current/reference/html/jc.html#oauth2resourceserver

基本上:

    如https://docs.spring.io/spring-security/site/docs/current/reference/html/jc.html#dependencies 中所述添加依赖项 添加 yml 配置,如 https://docs.spring.io/spring-security/site/docs/current/reference/html5/#oauth2resourceserver-jwt-minimalconfiguration 所述。对于认知使用以下网址:https://cognito-idp.&lt;region&gt;.amazonaws.com/&lt;YOUR_USER_POOL_ID&gt; 您可能需要按照https://docs.spring.io/spring-security/site/docs/current/reference/html/jc.html#oauth2resourceserver-sansboot 中的说明编辑您的安全配置

【讨论】:

【参考方案4】:

第 1 步:确认 JWT 的结构

第 2 步:验证 JWT 签名

第 3 步:验证声明

转到https://docs.aws.amazon.com/cognito/latest/developerguide/amazon-cognito-user-pools-using-tokens-verifying-a-jwt.html 了解更多信息。

【讨论】:

【参考方案5】:

至少,您需要 spring-boot-starter-oauth2-resource-server 和 spring-boot-starter-security 依赖项。你可能还需要spring-security-oauth2-jose 依赖。

然后您需要在属性或 yml 文件中设置颁发者 Uri。您可以在访问令牌有效负载中找到它作为“iss”值。 spring.security.oauth2.resourceserver.jwt.issuer-uri:

使用 curl 进行测试

curl -o /dev/null -s -w "%http_code\n" -H "Authorization: Bearer ey..." http://localhost:8080/hello

【讨论】:

以上是关于如何验证从 Cognito 获得的 jwt 令牌的主要内容,如果未能解决你的问题,请参考以下文章

AWS Cognito - 使用 JWT 与 cognito.getUser 开发工具包验证令牌

AWS Cognito JWT 令牌验证

Cognito - 如何在与客户端从(通过放大)获得令牌的子域不同的子域上对 nodejs 后端的客户端进行身份验证?

Cognito 用户池:如何使用刷新令牌刷新访问令牌

AWS Cognito 如何验证我自己的 IdP 颁发的身份令牌?

如何使用 jwt.io 手动验证从自定义授权服务器获得的令牌?