Spring Security Oauth oauth2UserService(在github上有效,但在google上无效)

问题描述 投票:0回答:1

我正在学习教程https://spring.io/guides/tutorials/spring-boot-oauth2/在最后一个示例中,有一个添加错误消息的示例。一切似乎都可以正常工作,但是我不明白为什么当我使用github登录时,这个bean可以工作但是当我使用谷歌登录时它不起作用。(当我调试它时,断点在github登录时停止,并且在通过Google登录时不会停止)。在哪里注意到这仅适用于github?bean(完全来自示例):

@Bean
public OAuth2UserService<OAuth2UserRequest, OAuth2User> oauth2UserService(WebClient rest) {
    DefaultOAuth2UserService delegate = new DefaultOAuth2UserService();// breakpoint here
    return request -> {
        OAuth2User user = delegate.loadUser(request); //and breakpoint here
        if (!"github".equals(request.getClientRegistration().getRegistrationId())) {
            return user;
        }

        OAuth2AuthorizedClient client = new OAuth2AuthorizedClient
                (request.getClientRegistration(), user.getName(), request.getAccessToken());
        String url = user.getAttribute("organizations_url");
        List<Map<String, Object>> orgs = rest
                .get().uri(url)
                .attributes(oauth2AuthorizedClient(client))
                .retrieve()
                .bodyToMono(List.class)
                .block();

        if (orgs.stream().anyMatch(org -> "spring-projects".equals(org.get("login")))) {
            return user;
        }

        throw new OAuth2AuthenticationException(new OAuth2Error("invalid_token", "Not in Spring Team", ""));
    };
}
spring-boot spring-security oauth-2.0 spring-social
1个回答
0
投票

您可以使用OidcUserService来挂钩google oauth身份验证过程。

查看下面的文章。

https://www.devglan.com/spring-security/spring-boot-security-google-oauth

希望这会有所帮助..

© www.soinside.com 2019 - 2024. All rights reserved.