Created
October 3, 2017 12:31
-
-
Save brunokrebs/367ef88bf79fb0d833fa91719b5cf841 to your computer and use it in GitHub Desktop.
Adding claims to a JWT on Spring Boot
This file contains hidden or 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
package com.auth0.samples.authapi.security; | |
// ... imports | |
public class JWTAuthenticationFilter extends UsernamePasswordAuthenticationFilter { | |
// ... | |
@Override | |
protected void successfulAuthentication(HttpServletRequest req, | |
HttpServletResponse res, | |
FilterChain chain, | |
Authentication auth) throws IOException, ServletException { | |
String token = Jwts.builder() | |
.setSubject(((User) auth.getPrincipal()).getUsername()) | |
.claim("preference", "dogs") // <========================================= HERE | |
.setExpiration(new Date(System.currentTimeMillis() + EXPIRATION_TIME)) | |
.signWith(SignatureAlgorithm.HS512, SECRET) | |
.compact(); | |
res.addHeader(HEADER_STRING, TOKEN_PREFIX + " " + token); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
should I place new claim in JWTAuthentication or JWTAuthorization?