filter java oauth_java – OAuth2.0 – 使用GitHub进行身份验证...

本文介绍了一个尝试使用OAuth2.0协议通过GitHub进行身份验证的Java Spring应用程序案例。前端使用Angular2,后端部署在Heroku上。问题在于前端和后端在GitHub上注册的OAuth应用和实际部署地址不匹配,导致身份验证失败。作者寻求解决方案。
摘要由CSDN通过智能技术生成

我正在尝试创建一个将前端和后端资产分开的应用程序.举个例子,假设前端最终将被托管在gh页面上,而后端将被部署在Heroku上.

我想使用OAuth2.0协议来验证我的客户端,GitHub是我的主要提供商.

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

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

// template

{{title}}

getServerResponse()

{{response}}

// 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()按钮,我得到相同的结果:

我想问一下,是否有可能以这种方式分离资产?如果是这样,我在哪里犯错误?

谢谢!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值