有一個(gè)包含用戶權(quán)限的 Oauth2Authentication 對象。當(dāng)我想獲得它的權(quán)威并將其設(shè)置為User對象的權(quán)威時(shí),如下所示: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 不能 cast to java.util.Set我該如何修復(fù)它?注:用戶權(quán)限對象的類型為Set<GrantedAuthority>
1 回答

蕪湖不蕪
TA貢獻(xiàn)1796條經(jīng)驗(yàn) 獲得超7個(gè)贊
如果 是 ,您可以輕松地從中創(chuàng)建一個(gè):oAuth2Authentication.getAuthorities()
List
Set
user.setAuthorities(new HashSet<GrantedAuthority>(oAuth2Authentication.getAuthorities()));
請注意,應(yīng)具有 適當(dāng)?shù)膶?shí)現(xiàn),以便用作 .GrantedAuthority
hashCode()
equals()
HashSet
添加回答
舉報(bào)
0/150
提交
取消