简体   繁体   中英

Java get subject from JWT token

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM