如何验证从 Cognito 获得的 jwt 令牌
How do I validate a jwt token that I got from Cognito
我有一个 jwt 令牌,是我在用户登录后从 cognito 检索到的。
我的应用程序中有一个特定的 api 端点,我希望只有具有有效 jwt 的用户才能访问该端点。我尝试查看网络上的各种资源,但我什么都看不懂。我是 jwt 概念的新手。
PS 我有一个 Java 应用程序(spring 启动)。如果有人能详细描述验证我的 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-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
Here's 如何验证令牌(这是用 Kotlin 编写的)。
这是密钥所在的位置:
https://cognito-idp.$regionName.amazonaws.com/$cognitoUserPoolId/.well-known/jwks.json
我在这里实现了一堆:
https://github.com/awslabs/cognito-proxy-rest-service
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
中所述添加依赖项
- 按照 https://docs.spring.io/spring-security/site/docs/current/reference/html5/#oauth2resourceserver-jwt-minimalconfiguration 中的说明添加 yml 配置。对于 cognito 使用以下 url:
https://cognito-idp.<region>.amazonaws.com/<YOUR_USER_POOL_ID>
- 您可能需要按照 https://docs.spring.io/spring-security/site/docs/current/reference/html/jc.html#oauth2resourceserver-sansboot
中所述编辑您的安全配置
第 1 步:确认 JWT 的结构
第 2 步:验证 JWT 签名
第 3 步:验证声明
至少,您需要 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
我有一个 jwt 令牌,是我在用户登录后从 cognito 检索到的。
我的应用程序中有一个特定的 api 端点,我希望只有具有有效 jwt 的用户才能访问该端点。我尝试查看网络上的各种资源,但我什么都看不懂。我是 jwt 概念的新手。
PS 我有一个 Java 应用程序(spring 启动)。如果有人能详细描述验证我的 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-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
Here's 如何验证令牌(这是用 Kotlin 编写的)。
这是密钥所在的位置:
https://cognito-idp.$regionName.amazonaws.com/$cognitoUserPoolId/.well-known/jwks.json
我在这里实现了一堆: https://github.com/awslabs/cognito-proxy-rest-service
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 中所述添加依赖项
- 按照 https://docs.spring.io/spring-security/site/docs/current/reference/html5/#oauth2resourceserver-jwt-minimalconfiguration 中的说明添加 yml 配置。对于 cognito 使用以下 url:
https://cognito-idp.<region>.amazonaws.com/<YOUR_USER_POOL_ID>
- 您可能需要按照 https://docs.spring.io/spring-security/site/docs/current/reference/html/jc.html#oauth2resourceserver-sansboot 中所述编辑您的安全配置
第 1 步:确认 JWT 的结构
第 2 步:验证 JWT 签名
第 3 步:验证声明
至少,您需要 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