【问题标题】:How do I validate a jwt token that I got from Cognito如何验证从 Cognito 获得的 jwt 令牌
【发布时间】: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。在此之后,我完全不知道需要做什么。

【问题讨论】:

    标签: java amazon-web-services spring-boot jwt aws-cognito


    【解决方案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-tokenhttps://jwt.io
    更多关于java-jwt的信息在这里:https://github.com/auth0/java-jwt

    【讨论】:

    【解决方案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

      基本上:

      1. https://docs.spring.io/spring-security/site/docs/current/reference/html/jc.html#dependencies 中所述添加依赖项
      2. 添加 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;
      3. 您可能需要按照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
          

          【讨论】:

            猜你喜欢
            • 2018-03-07
            • 2020-10-27
            • 2022-12-15
            • 2019-11-16
            • 2018-10-16
            • 2020-12-17
            • 2020-03-07
            • 2019-06-23
            • 2019-02-28
            相关资源
            最近更新 更多