英文:
Java get subject from JWT token
问题
我想读取JWT令牌,并从中获取userID。
据我所知,userID等于JWT声明中的"sub",即"subject"。
@GET()
@Path("path")
@RolesAllowed("user")
public String method( ){
String userID = jwt.claims.get("sub"); // 或类似的代码
}
有可能实现吗?
英文:
I would like to read the JWT token, and get the userID from it.
As I know the userID is equal to "sub" as "subject" in the JWT claims.
@GET()
@Path("path")
@RolesAllowed("user")
public String method( ){
String userID = jwt.claims.get ("sub"); // or something like this
}
How is it possible?
答案1
得分: 2
如果userID
在“sub”声明中,您可以使用此库按以下方式接收它:
Long userID = Long.parseLong(Jwts.parser()
.setSigningKey(secretKey)
.parseClaimsJws(token)
.getBody()
.getSubject());
其中secretKey
是您的签名密钥,token
是您的JWT令牌。
英文:
If the userID
is in the "sub" claim, you can receive it in the following way using this library:
Long userID = Long.parseLong(Jwts.parser()
.setSigningKey(secretKey)
.parseClaimsJws(token)
.getBody()
.getSubject());
Where secretKey
is your signing key and token
is your JWT token.
专注分享java语言的经验与见解,让所有开发者获益!
评论