Skip to content

Instantly share code, notes, and snippets.

@brunokrebs
Created October 3, 2017 12:31
Show Gist options
  • Save brunokrebs/367ef88bf79fb0d833fa91719b5cf841 to your computer and use it in GitHub Desktop.
Save brunokrebs/367ef88bf79fb0d833fa91719b5cf841 to your computer and use it in GitHub Desktop.
Adding claims to a JWT on Spring Boot
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);
}
}
@thailt
Copy link

thailt commented Oct 25, 2017

should I place new claim in JWTAuthentication or JWTAuthorization?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment