Created
November 13, 2018 03:34
-
-
Save codinko/b146a9dcb4cf9549a5b0d86ce11d7b84 to your computer and use it in GitHub Desktop.
JWT usage in Rest Controller while accessing resource passing the existing JWT
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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