OAuth2.0 - 使用 GitHub 进行身份验证,前端和后端运行在不同的服务器上。CORS 错误

马切伊·M。

我正在尝试创建一个将前端和后端资产分开的应用程序。例如,假设前端最终将托管在 gh-pages 上,而后端将部署在 Heroku 上。

我想使用 OAuth2.0 协议对我的客户进行身份验证,GitHub 作为我的主要提供者。

作为“概念证明”,我想创建一些利用这种身份验证的虚拟应用程序。下面是代码:

前端(Angular2 应用程序) - 在 localhost:8080 上启动

// template
<h1>
  {{title}}
</h1>
<button type="button" (click)="getServerResponse()">getServerResponse()</button>
<h1>{{response}}</h1>

// component
export class AppComponent {
  title = 'app works!';
  response = 'server response';

  constructor(private http: Http) {}

  getServerResponse() {
    this.http.get('http://localhost:9000/hello')
      .subscribe(res => this.response = JSON.stringify(res.json()));
  }
}

后端(Java + Spring 应用程序)- 在 localhost:9000 上启动

// Application.java
@SpringBootApplication
@EnableOAuth2Sso
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

// HelloController.java
@Controller
public class HelloController {

    @RequestMapping("/hello")
    public String hello() {
        return "Hello!";
    }
}

// FilterConfig.java
@Configuration
public class FilterConfig {

    @Bean
    public FilterRegistrationBean corsFilter() {
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        CorsConfiguration config = new CorsConfiguration();
        config.setAllowCredentials(true);
        config.addAllowedOrigin("*");
        config.addAllowedHeader("*");
        config.addAllowedMethod("*");
        source.registerCorsConfiguration("/**", config);
        FilterRegistrationBean bean = new FilterRegistrationBean(new CorsFilter(source));
        bean.setOrder(1);
        return bean;
    }

}

// resources/config/application.yml
security:
  oauth2:
    client:
      clientId: xxx
      clientSecret: yyy
      accessTokenUri: https://github.com/login/oauth/access_token
      userAuthorizationUri: https://github.com/login/oauth/authorize
      clientAuthenticationScheme: form
      scope: repo, user, read:org
    resource:
      userInfoUri: https://api.github.com/user
  filter-order: 5

server:
  port: 9000

我已经尝试在 GitHub 上将localhost:8080localhost:9000 注册为 OAuth 应用程序,但无论如何,每当我尝试单击 getServerResponse() 按钮时,我都会得到相同的结果:

在此处输入图片说明

我想问一下,这样的资产分割是否可行?如果是这样,我在哪里出错?

谢谢!

杂耍艺人

您看到的 CORS 消息是因为您的代码正在向其发送跨域请求,https://github.com/login/oauth/authorize但来自 github 的Access-Control-Allow-Origin响应不包含响应标头。

因此,您对 Spring 代码中的 CORS 配置所做的任何更改都无关紧要 — 不会有任何区别,因为需要更改的行为在 github 端,而您无法更改它。

您可能想要从后端而不是像您现在所做的前端代码执行 oauth 请求,或者使用https://github.com/Rob--W/cors-anywhere/设置 CORS 代理或这样,或者设置类似https://github.com/prose/gatekeeper 的东西

由于一些与安全相关的限制,Github 阻止您在仅客户端应用程序上实现 OAuth Web 应用程序流。

这是一个真正的无赖。因此,我们构建了 Gatekeeper,这是您让它工作所需的缺失部分。

Gatekeeper 与 Github.js 配合得很好,它可以帮助您从浏览器访问 Github API。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

为什么OAuth2身份验证服务器使用jdbc数据源返回401错误?

在iOS中使用NSURLSessions对服务器API进行oAuth2身份验证

自定义您使用OAuth2从Spring Security的身份验证错误

在iOS上使用OAuth2进行身份验证

在Spring Boot OAuth2授权服务器中使用Active Directory身份验证

使用ServiceStack的OWIN OAuth2资源服务器身份验证

使用第三方OAUTH2服务器进行SuiteCRM身份验证

使用OAuth2进行Geoserver身份验证

API身份验证和OAuth2的使用

phpmailer的Oauth2 gmail SMTP身份验证错误

使用zoom oauth2和flutter_web_auth的无效重定向错误

使用Laravel 4和php League OAuth2时出现奇怪的错误

有关使用Facebook隐式令牌进行服务器端资源服务器OAuth2身份验证的安全问题

邮递员在针对 Keycloak 服务器的 OAuth2 流中使用错误(废话?)代码

使用 OAuth2 和 Github 的 Spring Security 仅允许组织中的开发人员使用某些 API

如何使用REST API使用OAuth2和React Native进行身份验证?

如何使用Postman身份验证助手调用JHipster(春季)OAuth2 Rest服务器

使用 OAuth2 的 API 上的 RequestsLibrary

使用软件包“ golang.org/x/oauth2”对oauth2进行身份验证

使用Spring Security进行gRPC和OAuth2身份验证

简单的cocoapods使用OAuth2安装错误

使用 Google OAuth2 错误重定向 Uri

Google登录(iOS)使用后端服务器进行身份验证无效的发行者错误

使用OAuth2作为Azure Appservice运行asp.net core 2应用程序会导致502错误

wso2身份服务器oauth2请求返回HTTP错误415

尝试使用Auth0对用户进行身份验证时出现CORS错误

OAuth2 with Google - CORS 错误(Angular + Spring boot)

无法在其他PC上使用Firebase登录-与OAuth2相关的配置错误

使用OAuth 2.0和Google API进行服务器到服务器身份验证的示例