Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Security Oauth oauth2UserService(works on github but doesn't work on google)

I am learning tutorial https://spring.io/guides/tutorials/spring-boot-oauth2/ in the last example there is an example of adding error message. Everything seems work fine but i don't understand why when i login with github this bean works but when i login with google it doesn't work.(When i debug it breakpoint stops on github login and doesn't stop when login via google). Where is noticed that is only for github? bean(fully from example):

@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", ""));
    };
}
like image 828
Sultan Zhumatayev Avatar asked Oct 30 '25 11:10

Sultan Zhumatayev


1 Answers

You can use OidcUserService to hook google oauth authentication process.

Check the article below.

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

wish this helps..

like image 82
khseok Avatar answered Nov 02 '25 22:11

khseok