【发布时间】:2016-10-21 00:52:00
【问题描述】:
我正在尝试编写一个 Spring Boot 应用程序,该应用程序将使用 Google 单点登录 (SSO) 来验证用户身份(它可以是任何其他 SSO 提供商,例如 Facebook - 本示例仅使用 Google)。
我遵循了几个教程并提出了一个非常基本的设置:
appplication.properties:
security.oauth2.client.client-id: xxx
security.oauth2.client.client-secret: yyy
security.oauth2.client.access-token-uri=https://www.googleapis.com/oauth2/v3/token
security.oauth2.client.user-authorization-uri=https://accounts.google.com/o/oauth2/auth
security.oauth2.client.client-authentication-scheme=query
security.oauth2.client.scope=profile,email
security.oauth2.resource.user-info-uri=https://www.googleapis.com/plus/v1/people/me
security.oauth2.resource.prefer-token-info=false
控制器:
@RestController
public class ExampleController {
@RequestMapping("/")
String hello(OAuth2Authentication authentication) {
return "Hello " + authentication.getName();
}
@RequestMapping("/details")
Object details(OAuth2Authentication authentication) {
return authentication.getUserAuthentication();
}
}
在浏览器中一切正常,系统提示我输入我的 Google 凭据,然后我才能访问我的端点。
问题是我也想以编程方式访问此 API(例如使用 cUrl 或 RestClient)。
我尝试了以下方法:
curl xxx:yyy@localhost:8080/my-api/oauth/token -d grant_type=client_credentials
但得到以下响应:
{"timestamp":1466365089477,"status":403,"error":"Forbidden","message":"Expected CSRF token not found. Has your session expired?","path":"/my-api/oauth/token"}
我正在努力寻找一些关于如何以编程方式使用 SSO Spring Boot API 的优秀文档或教程。有人可以解释一下我所缺少的内容,或者指出一些包含功能齐全的多用户 API 示例的工作教程吗?
【问题讨论】:
标签: java spring-boot oauth-2.0 single-sign-on