【问题标题】:How to set UnmodifiableRandomAccessList to Set<Object> in java如何在 java 中将 UnmodifiableRandomAccessList 设置为 Set<Object>
【发布时间】:2019-03-11 22:13:56
【问题描述】:

有一个包含用户权限的 Oauth2Authentication 对象。当我想获得它的权限并将其设置为 User 对象的权限时,如下所示:

OAuth2Authentication oAuth2Authentication = (OAuth2Authentication) SecurityContextHolder.getContext().getAuthentication();

LinkedHashMap linkedHashMap = (LinkedHashMap) oAuth2Authentication.getUserAuthentication().getDetails();


user.setAuthorities((Set<GrantedAuthority>) oAuth2Authentication.getAuthorities());

引发以下异常:

java.lang.ClassCastException: java.util.Collections$UnmodifiableRandomAccessList 不能强制转换为 java.util.Set

我该如何解决?

注意:
User 对象的 Authorities 类型为Set&lt;GrantedAuthority&gt;

【问题讨论】:

    标签: java spring-security oauth-2.0 spring-security-oauth2 unmodifiable


    【解决方案1】:

    如果oAuth2Authentication.getAuthorities()List,您可以轻松地从中创建Set

    user.setAuthorities(new HashSet<GrantedAuthority>(oAuth2Authentication.getAuthorities()));
    

    请注意,GrantedAuthority 应正确实现 hashCode()equals(),以便用作 HashSet 的成员。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-29
      • 2021-08-22
      相关资源
      最近更新 更多