defaultoauth2user不能强制转换为类defaultoidcuser

xwbd5t1u  于 2021-10-10  发布在  Java
关注(0)|答案(0)|浏览(237)

我有一个简单的oidc客户端spring引导应用程序,在该应用程序中,我用于检索登录的用户信息,如下所示:

Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
    if (authentication instanceof OAuth2AuthenticationToken) {
      DefaultOidcUser user = (DefaultOidcUser) authentication.getPrincipal();
      Set<String> roles = authentication.getAuthorities().stream().map(GrantedAuthority::getAuthority)
              .collect(Collectors.toSet());
      return Optional.of(UserDTO.builder()
          .username(user.getPreferredUsername())
          .firstName(user.getGivenName())
          .lastName(user.getFamilyName())
          .roles(roles)
          .organizationId(getOrganizationIdFromAuthToken((OAuth2AuthenticationToken) authentication))
          .build());
    } else {
      return Optional.empty();
    }

但升级后, spring-security-oauth2-client 对于版本5.4.5,有一个 ClassCastException :

class org.springframework.security.oauth2.core.user.DefaultOAuth2User cannot be cast to class org.springframework.security.oauth2.core.oidc.user.DefaultOidcUser (org.springframework.security.oauth2.core.user.DefaultOAuth2User and org.springframework.security.oauth2.core.oidc.user.DefaultOidcUser are in unnamed module of loader 'app')

我怎样才能解决这个问题?

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题