generate jwt token java example

Solutions on MaxInterview for generate jwt token java example by the best coders in the world

showing results for - "generate jwt token java example"
Miguel
06 Oct 2020
1public String createToken(String username, List<Role> roles, String country) {
2
3    Claims claims = Jwts.claims().setSubject(username);
4    claims.put("auth", roles.stream().map(s -> new SimpleGrantedAuthority(s.getAuthority())).filter(Objects::nonNull).collect(Collectors.toList()));
5    claims.put("Country", country);
6    
7    Date now = new Date();
8    Date validity = new Date(now.getTime() + validityInMilliseconds);
9
10    return Jwts.builder()//
11        .setClaims(claims)//
12        .setIssuedAt(now)//
13        .setExpiration(validity)//
14        .signWith(SignatureAlgorithm.HS256, secretKey)//
15        .compact();
16  }
17