Skip to content

Instantly share code, notes, and snippets.

@codinko
Created November 13, 2018 03:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codinko/b146a9dcb4cf9549a5b0d86ce11d7b84 to your computer and use it in GitHub Desktop.
Save codinko/b146a9dcb4cf9549a5b0d86ce11d7b84 to your computer and use it in GitHub Desktop.
JWT usage in Rest Controller while accessing resource passing the existing JWT
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
final HttpServletRequest req = (HttpServletRequest) request;
final String authHeader = req.getHeader("authorization");
if (authHeader == null || !authHeader.startsWith("Bearer ")) {
throw new ServletException("Missing or invalid Authorization header");
}
final String token = authHeader.substring(7);
final Claims claims = Jwts.parser().setSigningKey("secretkey").parseClaimsJws(token).getBody();
req.setAttribute("claims", claims);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment