一個包含用戶權(quán)限的 Oauth2Authentication 對象。當我想獲得它的權(quán)限并將其設(shè)置為 User 對象的權(quán)限時,如下所示:OAuth2Authentication oAuth2Authentication = (OAuth2Authentication) SecurityContextHolder.getContext().getAuthentication();LinkedHashMap linkedHashMap = (LinkedHashMap) oAuth2Authentication.getUserAuthentication().getDetails();user.setAuthorities((Set<GrantedAuthority>) oAuth2Authentication.getAuthorities());引發(fā)以下異常:java.lang.ClassCastException:java.util.Collections$UnmodifiableRandomAccessList 無法轉(zhuǎn)換為 java.util.Set我如何解決它?注意:用戶對象的權(quán)限類型是Set<GrantedAuthority>
1 回答

茅侃侃
TA貢獻1842條經(jīng)驗 獲得超22個贊
如果oAuth2Authentication.getAuthorities()
是 a List
,您可以輕松地從中創(chuàng)建 a Set
:
user.setAuthorities(new HashSet<GrantedAuthority>(oAuth2Authentication.getAuthorities()));
請注意,GrantedAuthority
應(yīng)該正確實現(xiàn)hashCode()
andequals()
才能用作 a 的成員HashSet
。
添加回答
舉報
0/150
提交
取消