2

次のコードのようなものを使用してテスト JWT を作成しました

String jwt = Jwts.builder()
    .setHeaderParam("typ", "jwt")
    .setId("myid")
    .setIssuer("ExampleIssuer")
    .setSubject("JohnDoe")
    .setIssuedAt(Date.from(LocalDateTime.now().toInstant(ZoneOffset.ofHours(-4))))
        .setExpiration(Date.from(LocalDateTime.now().toInstant(ZoneOffset.ofHours(-4)).plusSeconds(600)))
    .claim("perms",perms)
    .signWith(SignatureAlgorithm.HS512, "SECRET")
    .compact();

"perms" はカスタム クレームで、文字列 (アクセス許可) の ArrayList を含みます。

JWT を受け取ったら、次のコードを使用します。

try{
Jwt<?, ?> claims = Jwts.parser().setSigningKey("SECRET").parse(jwt); 
System.out.println(claims.getBody().toString());
} catch (SignatureException e){
//Error
}

そして、私は次のようなものを取得します

{jti=myid, iss=ExampleIssuer, sub=JohnDoe, iat=1495678299, exp=1495678899, perms=[CREATE, VIEW]}

私の質問は次のとおりです。これは、請求を取り戻すための正しい (意図した) 方法ですか? これからはカスタムメソッドで結果を解析する必要があるようですが、どういうわけかそれは意図した方法ではないと思います。

ありがとう」

4

1 に答える 1