JwtTokenUtil { public String generateToken(UserDetails userDetails) { Map<String, Object> claims = new HashMap<>(); claims.put(CLAIM_KEY_ROLES, userDetails.getAuthorities().stream().map(GrantedAuthority::getAuthority).collect(joining(ROLES_CLAIM_DELIMITER))); log.info("generateToken()"); return doGenerateToken(userDetails.getUsername(), claims); }  String getUsernameFromToken(String token); Instant getIssuedAtDateFromToken(String token); Instant getExpirationDateFromToken(String token); T getClaimFromToken(String token, Function<Claims, T> claimsResolver); String generateToken(UserDetails userDetails); Boolean canTokenBeRefreshed(String token, Instant lastPasswordReset); String generateRefreshToken(String subject); Boolean validateToken(String token, UserDetails userDetails); Collection<? extends GrantedAuthority> getGrantedAuthoritiesFromToken(String authToken);  }
@Test public void testGenerateToken() throws Exception { Instant start = Instant.now().minusSeconds(1); User userDetails = new User("username", "", asList(new SimpleGrantedAuthority("USER"), new SimpleGrantedAuthority("ADMIN"))); Duration expiration = Duration.ofMinutes(30); String token = jwtTokenUtil.generateToken(userDetails); assertThat(jwtTokenUtil.getUsernameFromToken(token)).isEqualTo("username"); assertThat(jwtTokenUtil.getIssuedAtDateFromToken(token)).isBetween(start, Instant.now()); assertThat(jwtTokenUtil.getExpirationDateFromToken(token)).isBetween(start.plus(expiration), Instant.now().plus(expiration)); String[] roles = jwtTokenUtil.getClaimFromToken(token, c -> c.get(JwtTokenUtil.CLAIM_KEY_ROLES, String.class)).split(JwtTokenUtil.ROLES_CLAIM_DELIMITER); assertThat(roles).containsExactlyInAnyOrder("ADMIN", "USER"); }