Answers for "how to set username, roles, admin in jwt claims example coding in java"

0

how to set username, roles, admin in jwt claims example coding in java

public String createToken(String username, List<Role> roles) {

    Claims claims = Jwts.claims().setSubject(username);
    claims.put("auth", roles.stream().map(s -> new SimpleGrantedAuthority(s.getAuthority())).filter(Objects::nonNull).collect(Collectors.toList()));

    Date now = new Date();
    Date validity = new Date(now.getTime() + validityInMilliseconds);

    return Jwts.builder()//
        .setClaims(claims)//
        .setIssuedAt(now)//
        .setExpiration(validity)//
        .signWith(SignatureAlgorithm.HS256, secretKey)//
        .compact();
  }
Posted by: Guest on May-19-2021

Code answers related to "how to set username, roles, admin in jwt claims example coding in java"

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language