@Test public void testRolesCanBeExtractedFromTokenClaims() { when(claims.get(JwtUtils.CLAIM_KEY_ROLES)).thenReturn(ROLES); final List<GrantedAuthority> roles = jwtUtils.getRolesFromTokenClaims(claims); assertThat(roles.size()).isEqualTo(2); assertThat(roles.get(0).getAuthority()).isEqualTo(RoleName.ROLE_ADMIN.name()); assertThat(roles.get(1).getAuthority()).isEqualTo(RoleName.ROLE_USER.name()); verify(claims, times(1)).get(JwtUtils.CLAIM_KEY_ROLES); }
public List<GrantedAuthority> getRolesFromTokenClaims(Claims claims) { final List<GrantedAuthority> roles = new ArrayList<>(); try { @SuppressWarnings("unchecked") final List<String> rolesFromClaim = (List<String>) claims.get(CLAIM_KEY_ROLES); for (final String roleFromClaim : rolesFromClaim) { roles.add(new SimpleGrantedAuthority(roleFromClaim)); } return roles; } catch (Exception e) { final String errorMsg = "Failed to extract roles claim from token!"; LOG.error(errorMsg, e); throw new JwtAuthenticationException(errorMsg, e); } }
JwtUtils { public List<GrantedAuthority> getRolesFromTokenClaims(Claims claims) { final List<GrantedAuthority> roles = new ArrayList<>(); try { @SuppressWarnings("unchecked") final List<String> rolesFromClaim = (List<String>) claims.get(CLAIM_KEY_ROLES); for (final String roleFromClaim : rolesFromClaim) { roles.add(new SimpleGrantedAuthority(roleFromClaim)); } return roles; } catch (Exception e) { final String errorMsg = "Failed to extract roles claim from token!"; LOG.error(errorMsg, e); throw new JwtAuthenticationException(errorMsg, e); } } }
JwtUtils { public List<GrantedAuthority> getRolesFromTokenClaims(Claims claims) { final List<GrantedAuthority> roles = new ArrayList<>(); try { @SuppressWarnings("unchecked") final List<String> rolesFromClaim = (List<String>) claims.get(CLAIM_KEY_ROLES); for (final String roleFromClaim : rolesFromClaim) { roles.add(new SimpleGrantedAuthority(roleFromClaim)); } return roles; } catch (Exception e) { final String errorMsg = "Failed to extract roles claim from token!"; LOG.error(errorMsg, e); throw new JwtAuthenticationException(errorMsg, e); } }  }
JwtUtils { public List<GrantedAuthority> getRolesFromTokenClaims(Claims claims) { final List<GrantedAuthority> roles = new ArrayList<>(); try { @SuppressWarnings("unchecked") final List<String> rolesFromClaim = (List<String>) claims.get(CLAIM_KEY_ROLES); for (final String roleFromClaim : rolesFromClaim) { roles.add(new SimpleGrantedAuthority(roleFromClaim)); } return roles; } catch (Exception e) { final String errorMsg = "Failed to extract roles claim from token!"; LOG.error(errorMsg, e); throw new JwtAuthenticationException(errorMsg, e); } }  Claims validateTokenAndGetClaims(String token); String createToken(JwtUser userDetails); boolean canTokenBeRefreshed(Claims claims, Date lastPasswordReset); String refreshToken(String token); String getUsernameFromTokenClaims(Claims claims); List<GrantedAuthority> getRolesFromTokenClaims(Claims claims); }
JwtUtils { public List<GrantedAuthority> getRolesFromTokenClaims(Claims claims) { final List<GrantedAuthority> roles = new ArrayList<>(); try { @SuppressWarnings("unchecked") final List<String> rolesFromClaim = (List<String>) claims.get(CLAIM_KEY_ROLES); for (final String roleFromClaim : rolesFromClaim) { roles.add(new SimpleGrantedAuthority(roleFromClaim)); } return roles; } catch (Exception e) { final String errorMsg = "Failed to extract roles claim from token!"; LOG.error(errorMsg, e); throw new JwtAuthenticationException(errorMsg, e); } }  Claims validateTokenAndGetClaims(String token); String createToken(JwtUser userDetails); boolean canTokenBeRefreshed(Claims claims, Date lastPasswordReset); String refreshToken(String token); String getUsernameFromTokenClaims(Claims claims); List<GrantedAuthority> getRolesFromTokenClaims(Claims claims);  }