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.
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 -
Is the place where 401 error happens.
* 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. *
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