There is an Oauth2Authentication object that contains user-authority. When I want to get its authority and set it in authority of User object like this:
OAuth2Authentication oAuth2Authentication = (OAuth2Authentication) SecurityContextHolder.getContext().getAuthentication();
LinkedHashMap linkedHashMap = (LinkedHashMap) oAuth2Authentication.getUserAuthentication().getDetails();
user.setAuthorities((Set<GrantedAuthority>) oAuth2Authentication.getAuthorities());
the following exception is raised:
java.lang.ClassCastException: java.util.Collections$UnmodifiableRandomAccessList cannot be cast to java.util.Set
How do I fix it?
Note:
The type of Authorities of User object is Set<GrantedAuthority>
If
oAuth2Authentication.getAuthorities()is aList, you can easily create aSetfrom it:Note that
GrantedAuthorityshould have a proper implementation ofhashCode()andequals()in order to be used as a member of aHashSet.