一個包含用戶權限的 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我如何解決它?注意:用戶對象的權限類型是Set<GrantedAuthority>
1 回答
茅侃侃
TA貢獻1842條經驗 獲得超22個贊
如果oAuth2Authentication.getAuthorities()是 a List,您可以輕松地從中創建 a Set:
user.setAuthorities(new HashSet<GrantedAuthority>(oAuth2Authentication.getAuthorities()));
請注意,GrantedAuthority應該正確實現hashCode()andequals()才能用作 a 的成員HashSet。
添加回答
舉報
0/150
提交
取消
