org.springframework.security.oauth2.core.oidc.user.DefaultOidcUser cannot be cast to model.UserPrincipal

1.5k Views Asked by At

I'm working with oauth2login in spring security. The redirection to the provider (facebook or google) was made with success but I'm getting the error : org.springframework.security.oauth2.core.oidc.user.DefaultOidcUser cannot be cast to model.UserPrincipal, in this line of code

UserPrincipal userPrincipal = (UserPrincipal) authentication.getPrincipal(); My UserPrincipal class is like this :

public class UserPrincipal implements OAuth2User, OidcUser, UserDetails {
    
    private Long id;
    private String email;
    private String password;
    private Collection<? extends GrantedAuthority> authorities;
    private Map<String, Object> attributes;
    private Map<String, Object> claims;
    private OidcUserInfo oidcUserInfo;
    private OidcIdToken oidcIdToken;
...
}

How can I resolve this issue please?

1

There are 1 best solutions below

0
Box24u On

Have You Override methods in UserPrincipal class that implements OAuth2User, UserDetails.

@Override
public String getName() {
    return String.valueOf(id);
}

@Override
public String getUsername() {
    return email;
}

@Override
public boolean isAccountNonExpired() {
    return true;
}