java – OAuth2.0 – 使用GitHub进行身份验证,前端和后端运行在不同的服务器上. CORS错误
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java – OAuth2.0 – 使用GitHub进行身份验证,前端和后端运行在不同的服务器上. CORS错误,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2943字,纯文字阅读大概需要5分钟。
内容图文
![java – OAuth2.0 – 使用GitHub进行身份验证,前端和后端运行在不同的服务器上. CORS错误](/upload/InfoBanner/zyjiaocheng/705/f8659956f65b4f9297cbfb6d9afeee4b.jpg)
我正在尝试创建一个将前端和后端资产分开的应用程序.举个例子,假设前端最终将被托管在gh页面上,而后端将被部署在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:8080和localhost: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的内容:
Because of some security-related limitations, Github prevents you from implementing the OAuth Web Application Flow on a client-side only application.
This is a real bummer. So we built Gatekeeper, which is the missing piece you need in order to make it work.
Gatekeeper works well with Github.js, which helps you access the Github API from the browser.
内容总结
以上是互联网集市为您收集整理的java – OAuth2.0 – 使用GitHub进行身份验证,前端和后端运行在不同的服务器上. CORS错误全部内容,希望文章能够帮你解决java – OAuth2.0 – 使用GitHub进行身份验证,前端和后端运行在不同的服务器上. CORS错误所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。