Search code examples
springspring-securityapache2google-oauthspring-security-oauth2

Unauthorized Error when redirecting from OAuth to application


I have a spring application that use google, facebook & LinkedIn OAuth to login.

When user is logging in, user directed to the google/LinkedIn/Facebook login page and they can enter their email password and login. After login when the user is redirected to the application it shows a unauthorized error.

enter image description here

Following is the code block


@Configuration
@EnableOAuth2Client
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {

@Autowired
    OAuth2ClientContext oauth2ClientContext;

    public SecurityConfiguration() {
    }

    protected void configure(HttpSecurity http) throws Exception {
        ((HttpSecurity)((HttpSecurity)((HttpSecurity)((HttpSecurity)((AuthorizedUrl)((AuthorizedUrl)((HttpSecurity)http.headers().frameOptions().sameOrigin().httpStrictTransportSecurity().disable().and()).antMatcher("/**").authorizeRequests().antMatchers(new String[]{"/assets/**", "/landing", "/sociallogin"})).permitAll().anyRequest()).authenticated().and()).exceptionHandling().authenticationEntryPoint(new LoginUrlAuthenticationEntryPoint("/landing")).and()).logout().logoutSuccessUrl("/landing").permitAll().and()).csrf().disable()).addFilterBefore(this.ssoFilter(), BasicAuthenticationFilter.class);
    }

    private Filter ssoFilter() {
        CompositeFilter filter = new CompositeFilter();
        List<Filter> filters = new ArrayList();
        OAuth2ClientAuthenticationProcessingFilter facebookFilter = new OAuth2ClientAuthenticationProcessingFilter("/login/facebook");
        OAuth2RestTemplate facebookTemplate = new OAuth2RestTemplate(this.facebook(), this.oauth2ClientContext);
        facebookFilter.setRestTemplate(facebookTemplate);
        facebookFilter.setTokenServices(new UserInfoTokenServices(this.facebookResource().getUserInfoUri(), this.facebook().getClientId()));
        facebookFilter.setAuthenticationSuccessHandler(this.successHandler());
        filters.add(facebookFilter);
        OAuth2ClientAuthenticationProcessingFilter googleFilter = new OAuth2ClientAuthenticationProcessingFilter("/login/google");
        OAuth2RestTemplate googleTemplate = new OAuth2RestTemplate(this.google(), this.oauth2ClientContext);
        googleFilter.setRestTemplate(googleTemplate);
        googleFilter.setTokenServices(new UserInfoTokenServices(this.googleResource().getUserInfoUri(), this.google().getClientId()));
        googleFilter.setAuthenticationSuccessHandler(this.successHandler());
        filters.add(googleFilter);
        OAuth2ClientAuthenticationProcessingFilter githubFilter = new OAuth2ClientAuthenticationProcessingFilter("/login/linkedin");
        OAuth2RestTemplate githubTemplate = new OAuth2RestTemplate(this.github(), this.oauth2ClientContext);
        githubFilter.setRestTemplate(githubTemplate);
        githubFilter.setTokenServices(new UserInfoTokenServices(this.githubResource().getUserInfoUri(), this.github().getClientId()));
        githubFilter.setAuthenticationSuccessHandler(this.successHandler());
        filters.add(githubFilter);
        filter.setFilters(filters);
        return filter;

}

ALSO I WANT TO HIGHLIGHT -

The request url -

https://example.com/ticket/login/google?state=V2mdsJ&code=4%2F0wGA6aCswqnSD68xj34acDSDhz_DVFE5UiUOr-QQOZl9w6CpZ2aLoFCfBFL_5k_Pzx89I&scope=email+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.email+openid&authuser=0&prompt=consent

Is the place where 401 error happens.

  • There is apache 2 server which proxy the request to the app server (I'm not sure if that can be cause for this issue, but just want to mention)

* Have someone experienced this kind of scenario or is there way to troubleshoot where the error is. Let me know if there any additional info that I can provide. *


Solution

  • Found the issue, from my server couldn't access www.googleapis.com to get the information

    Had to add 8.8.8.8 ip to the /etc/resolve.conf file