JwtAuthenticationToken cannot be cast to OAuth2AuthenticationToken

You should be able get the attributes from either token type using logic like the following:

Authentication authToken = SecurityContextHolder.getContext().getAuthentication();
Map<String, Object> attributes;
if (authToken instanceof OAuth2AuthenticationToken) {
    attributes = ((OAuth2AuthenticationToken) authToken).getPrincipal().getAttributes();
} else if (authToken instanceof JwtAuthenticationToken) {
    attributes = ((JwtAuthenticationToken) authToken).getTokenAttributes();
}
2 Likes