从 OAuth 重定向到应用程序时出现未经授权的错误
Unauthorized Error when redirecting from OAuth to application
我有一个 spring 应用程序,它使用 google、facebook 和 LinkedIn OAuth 登录。
当用户登录时,用户被定向到 google/LinkedIn/Facebook 登录页面,他们可以输入他们的电子邮件密码并登录。登录后,当用户被重定向到应用程序时,它显示未经授权的错误。
代码块如下
@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;
}
我还想强调一下 -
请求url-
是401错误发生的地方
- 有 apache 2 服务器将请求代理到应用程序服务器(我不确定这是否会导致此问题,但只想提一下)
* 有没有人遇到过这种情况,或者有没有办法排查错误的地方。如果我可以提供任何其他信息,请告诉我。 *
发现问题,无法从我的服务器访问www.googleapis.com获取信息
必须将 8.8.8.8 ip 添加到 /etc/resolve.conf
文件
我有一个 spring 应用程序,它使用 google、facebook 和 LinkedIn OAuth 登录。
当用户登录时,用户被定向到 google/LinkedIn/Facebook 登录页面,他们可以输入他们的电子邮件密码并登录。登录后,当用户被重定向到应用程序时,它显示未经授权的错误。
代码块如下
@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;
}
我还想强调一下 -
请求url-
是401错误发生的地方
- 有 apache 2 服务器将请求代理到应用程序服务器(我不确定这是否会导致此问题,但只想提一下)
* 有没有人遇到过这种情况,或者有没有办法排查错误的地方。如果我可以提供任何其他信息,请告诉我。 *
发现问题,无法从我的服务器访问www.googleapis.com获取信息
必须将 8.8.8.8 ip 添加到 /etc/resolve.conf
文件