[英]Spring Boot and Google SSO
我正在尝试编写一个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 Apis的优秀文档或教程。 有人可以解释我所缺少的内容,还是可以带我使用功能齐全的多用户API示例的一些工作教程?
您是否看过作为Spring Boot一部分的Hosting a Authorization Server OAuth 2和SocialApplication.java
示例?
本示例配置了一个服务器,该服务器能够使用@EnableAuthorizationServer
批注来授予OAuth令牌。
还有两个curl
示例,它们演示了客户端如何请求访问令牌:
$ curl acme:acmesecret@localhost:8080/oauth/token -d grant_type=client_credentials {"access_token":"370592fd-b9f8-452d-816a-4fd5c6b4b8a6","token_type":"bearer","expires_in":43199,"scope":"read write"} $ curl acme:acmesecret@localhost:8080/oauth/token -d grant_type=password -d username=user -d password=... {"access_token":"aa49e025-c4fe-4892-86af-15af2e6b72a2","token_type":"bearer","refresh_token":"97a9f978-7aad-4af7-9329-78ff2ce9962d","expires_in":43199,"scope":"read write"}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.