@Test public void authenticateWithValidAccessToken() { final String accessToken = "eyJhbGciOiJIUzI1NiJ9" + ".eyJzdWIiOiJ0ZXN0QGdtYWlsLmNvbSIsImF1dGhvcml0aWVzIjpbIlJPTEVfVVN" + "FUiJdLCJpYXQiOjE1NzU4NDUzNTAsImV4cCI6NjE1NzU4NDUyOTB9" + ".x1D799yGc0dj2uWDQYusnLyG5r6-Rjj6UgBhp2JjVDE"; when(jwtTool.getAccessTokenKey()).thenReturn("123123123"); Date expectedExpiration = new Date(61575845290000L); Date actualExpiration = Jwts.parser() .setSigningKey(jwtTool.getAccessTokenKey()) .parseClaimsJws(accessToken) .getBody() .getExpiration(); assertEquals(expectedExpiration, actualExpiration); Authentication authentication = new UsernamePasswordAuthenticationToken( accessToken, null ); Authentication actual = jwtAuthenticationProvider.authenticate(authentication); final String expectedEmail = "test@gmail.com"; assertEquals(expectedEmail, actual.getPrincipal()); assertEquals( Stream.of(expectedRole) .map(ROLE::toString) .map(SimpleGrantedAuthority::new) .collect(Collectors.toList()), actual.getAuthorities() ); assertEquals("", actual.getCredentials()); }
@Override public Authentication authenticate(Authentication authentication) { String email = Jwts.parser() .setSigningKey(jwtTool.getAccessTokenKey()) .parseClaimsJws(authentication.getName()) .getBody() .getSubject(); @SuppressWarnings({"unchecked, rawtype"}) List<String> authorities = (List<String>) Jwts.parser() .setSigningKey(jwtTool.getAccessTokenKey()) .parseClaimsJws(authentication.getName()) .getBody() .get(AUTHORITIES); return new UsernamePasswordAuthenticationToken( email, "", authorities.stream().map(SimpleGrantedAuthority::new).collect(Collectors.toList()) ); }
JwtAuthenticationProvider implements AuthenticationProvider { @Override public Authentication authenticate(Authentication authentication) { String email = Jwts.parser() .setSigningKey(jwtTool.getAccessTokenKey()) .parseClaimsJws(authentication.getName()) .getBody() .getSubject(); @SuppressWarnings({"unchecked, rawtype"}) List<String> authorities = (List<String>) Jwts.parser() .setSigningKey(jwtTool.getAccessTokenKey()) .parseClaimsJws(authentication.getName()) .getBody() .get(AUTHORITIES); return new UsernamePasswordAuthenticationToken( email, "", authorities.stream().map(SimpleGrantedAuthority::new).collect(Collectors.toList()) ); } }
JwtAuthenticationProvider implements AuthenticationProvider { @Override public Authentication authenticate(Authentication authentication) { String email = Jwts.parser() .setSigningKey(jwtTool.getAccessTokenKey()) .parseClaimsJws(authentication.getName()) .getBody() .getSubject(); @SuppressWarnings({"unchecked, rawtype"}) List<String> authorities = (List<String>) Jwts.parser() .setSigningKey(jwtTool.getAccessTokenKey()) .parseClaimsJws(authentication.getName()) .getBody() .get(AUTHORITIES); return new UsernamePasswordAuthenticationToken( email, "", authorities.stream().map(SimpleGrantedAuthority::new).collect(Collectors.toList()) ); } JwtAuthenticationProvider(JwtTool jwtTool); }
JwtAuthenticationProvider implements AuthenticationProvider { @Override public Authentication authenticate(Authentication authentication) { String email = Jwts.parser() .setSigningKey(jwtTool.getAccessTokenKey()) .parseClaimsJws(authentication.getName()) .getBody() .getSubject(); @SuppressWarnings({"unchecked, rawtype"}) List<String> authorities = (List<String>) Jwts.parser() .setSigningKey(jwtTool.getAccessTokenKey()) .parseClaimsJws(authentication.getName()) .getBody() .get(AUTHORITIES); return new UsernamePasswordAuthenticationToken( email, "", authorities.stream().map(SimpleGrantedAuthority::new).collect(Collectors.toList()) ); } JwtAuthenticationProvider(JwtTool jwtTool); @Override Authentication authenticate(Authentication authentication); @Override boolean supports(Class<?> authentication); }
JwtAuthenticationProvider implements AuthenticationProvider { @Override public Authentication authenticate(Authentication authentication) { String email = Jwts.parser() .setSigningKey(jwtTool.getAccessTokenKey()) .parseClaimsJws(authentication.getName()) .getBody() .getSubject(); @SuppressWarnings({"unchecked, rawtype"}) List<String> authorities = (List<String>) Jwts.parser() .setSigningKey(jwtTool.getAccessTokenKey()) .parseClaimsJws(authentication.getName()) .getBody() .get(AUTHORITIES); return new UsernamePasswordAuthenticationToken( email, "", authorities.stream().map(SimpleGrantedAuthority::new).collect(Collectors.toList()) ); } JwtAuthenticationProvider(JwtTool jwtTool); @Override Authentication authenticate(Authentication authentication); @Override boolean supports(Class<?> authentication);  }