jwt claims example coding in java

Solutions on MaxInterview for jwt claims example coding in java by the best coders in the world

showing results for - "jwt claims example coding in java"
Miranda
15 Mar 2016
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