我使用的春天的oauth2和角度进行测试。 春天启动的应用程序是在端口8081和角度是4200,我的春天已经设置CORS端口4200。 当我点击Gmail按钮,春风不给任何异常,只有对铬,我得到了没有“访问控制允许来源”错误。
Java代码:
@Autowired
private OAuth2ClientContext oauthClientContext;
@Value("${cross-origin-url}")
private String crossOriginUrl;
@Override
protected void configure(HttpSecurity http) throws Exception {
// TODO Auto-generated method stub
http.cors()
.and().csrf().csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse())
.and()
.antMatcher("/**").authorizeRequests()
.antMatchers("/login**","/","/test","/login/gmail").permitAll()
.anyRequest().fullyAuthenticated()
.and()
.addFilterBefore(oauthGmailFilter(), BasicAuthenticationFilter.class)
;
}
@Bean
public CorsConfigurationSource corsConfigurationSource() {
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
CorsConfiguration cors = new CorsConfiguration();
cors.addAllowedOrigin(crossOriginUrl);
source.registerCorsConfiguration("/**", cors.applyPermitDefaultValues());
return source;
}
@Bean
public Filter oauthGmailFilter() {
OAuth2ClientAuthenticationProcessingFilter gmailFilter = new OAuth2ClientAuthenticationProcessingFilter("/login/gmail");
OAuth2RestTemplate gmailTemplate = new OAuth2RestTemplate(gmail(),oauthClientContext);
gmailFilter.setRestTemplate(gmailTemplate);
UserInfoTokenServices tokenService = new UserInfoTokenServices(gmailResource().getUserInfoUri(),gmail().getClientId());
tokenService.setRestTemplate(gmailTemplate);
gmailFilter.setTokenServices(tokenService);
return gmailFilter;
}
@Bean
@ConfigurationProperties("gmail.client")
public AuthorizationCodeResourceDetails gmail() {
return new AuthorizationCodeResourceDetails();
}
@Bean
@ConfigurationProperties("gmail.resource")
public ResourceServerProperties gmailResource() {
return new ResourceServerProperties();
}
@Bean
public FilterRegistrationBean<OAuth2ClientContextFilter> oauth2ClientFilterRegistration(OAuth2ClientContextFilter filter) {
FilterRegistrationBean<OAuth2ClientContextFilter> registration = new FilterRegistrationBean<OAuth2ClientContextFilter>();
registration.setFilter(filter);
registration.setOrder(-100);
return registration;
}
我下面这个教程: https://spring.io/guides/tutorials/spring-boot-oauth2/#_social_login_click
Gmail login button
Gmail API setup
Application.properities:
代码是从教程链接一样,我刚刚从Facebook改变到Gmail。 我已经添加本地主机:8081到Gmail API,但我仍然得到以下错误
感谢您的帮助,我便无法弄清楚什么是错我的应用程序。