通过OAuth2,Spring授权后无法获取UserInfo

时间:2019-10-30 13:29:42

标签: java spring oauth-2.0 userinfo

在configure()中,我有:

       ...
           .oauth2Login()
                .userInfoEndpoint()
                .oidcUserService(oidcUserService);

我的CustomIodcUserService类:

@Service
public class CustomOidcUserService extends OidcUserService {

@Autowired
private UserRepository userRepository;

@Override
public OidcUser loadUser(OidcUserRequest userRequest) throws OAuth2AuthenticationException {
    OidcUser oidcUser = super.loadUser(userRequest);
    Map attributes = oidcUser.getAttributes();
    GoogleOAuth2UserInfo userInfo = new GoogleOAuth2UserInfo(attributes);
    updateUser(userInfo);
    return oidcUser;
}

private void updateUser(GoogleOAuth2UserInfo userInfo) {
    User user = userRepository.findByEmail(userInfo.getEmail()).get();
    if(user == null) {
        user = new User();
    }
    user.setEmail(userInfo.getEmail());
    user.setName(userInfo.getName());

    System.out.println(userRepository.save(user));
}

}

但是永远不会调用此类的方法 有没有人遇到过这个问题,或者我该如何解决这个问题或以其他方式获得UserInfo?

1 个答案:

答案 0 :(得分:0)

https://www.callicoder.com/spring-boot-security-oauth2-social-login-part-2/

本文介绍了如何做到这一点,只有他们使用过

userInfoEndpoint ()
       .userService (customOAuth2UserService)