DefaultOidcUser cannot be cast to class CustomOAuth2User

Viewed 1446

I have the following success handler that is part of the WebSecurityConfig.java file:

            .successHandler(new AuthenticationSuccessHandler() {

            @Override
            public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response,
                                                Authentication authentication) throws IOException, ServletException {

                CustomOAuth2User oauthUser = (CustomOAuth2User) authentication.getPrincipal();

                userAuthService.processOAuthPostLogin(oauthUser.getEmail());

                response.sendRedirect("/list");
            }
        })

At the line where casting is supposed to take place, it gives the error:

java.lang.ClassCastException: class org.springframework.security.oauth2.core.oidc.user.DefaultOidcUser cannot be cast to class com.myapp.myapp.mvc.business.domain.user.CustomOAuth2User

How can I go about solving this?

The CustomOAuth2User class is as follows:

public class CustomOAuth2User implements OAuth2User {

    private OAuth2User oauth2User;

    public CustomOAuth2User(OAuth2User oauth2User) {
        this.oauth2User = oauth2User;
    }


    @Override
    public Map<String, Object> getAttributes() {
        return oauth2User.getAttributes();
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return oauth2User.getAuthorities();
    }

    @Override
    public String getName() {
        return oauth2User.getAttribute("name");
    }

    public String getEmail() {
        return oauth2User.<String>getAttribute("email");
    }
}
2 Answers

I think your code are from codejava.net right? I also got the same issues and I fixed with update on the onAuthenticationSuccess function. I change the following code

CustomOAuth2User oauthUser = (CustomOAuth2User) authentication.getPrincipal();

userAuthService.processOAuthPostLogin(oauthUser.getEmail());

with this code

DefaultOidcUser oauthUser = (DefaultOidcUser) authentication.getPrincipal();
String email = oauthUser.getAttribute("email");
userDetailsService.processOAuthPostLogin(email);

And it's working now :)

You are getting this error because you have not defined the scopes explicitly in your app and by default scopes also include openId. If you define your scopes in the application.properties like below you can cast the principal to CustomOAuth2User .

spring.security.oauth2.client.registration.google.scope=email,profile

If you wish to include openId in your scopes, then you need to cast your principal to DefaultOidcUser an you can create a CustomOidcUserService service like below

@Service
public class CustomOidcUserService extends OidcUserService {

  @Autowired private UserRepository userRepository;
  @Autowired private RoleRepository roleRepository;

  @Override
  public OidcUser loadUser(OidcUserRequest userRequest) throws OAuth2AuthenticationException {
    final OidcUser oidcUser = super.loadUser(userRequest);
    return oidcUser;
}
Related