#adding claims to jwt removes subject - how to fix ?

3 messages · Page 1 of 1 (latest)

languid swift
#

as the title describes when i add the claims it will remove the subject:

    public String generateToken(MyUserDetails myUserDetails) {

        Map<String, Object> claims = new HashMap<>();
        claims.put("ROLES", myUserDetails.getCredentials().getRoles().stream()
            .map(Role::getName)
            .collect(Collectors.toSet()));

        return Jwts.builder()
            .setHeaderParam("typ", "JWT")
            .setIssuer("xxxx")
            .setSubject(myUserDetails.getEmail())
            .setClaims(claims)
            .setIssuedAt(new Date())
            .setExpiration(new Date((new Date()).getTime() + jwtExpirationMs))
            .signWith(SignatureAlgorithm.HS512, jwtSecret)
            .compact();

before claims

{
  "sub": "system@login.com",
  "iss": "xxxx",
  "iat": 1686327421,
  "exp": 1686336061
}

after claims:

{
  "Role": [
    "ROLE_UNDEFINED"
  ],
  "iss": "xxxx",
  "exp": 1686335909,
  "iat": 1686327269
}
oak hollowBOT
#

This post has been reserved for your question.

Hey @languid swift! Please use /close or the Close Post button above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.