我正在尝试编写一个Spring Boot应用程序,该应用程序将使用Google Single Sign(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的一些良好文档或教程。有人可以解释我缺少的东西,还是将我指向一些功能齐全的多用户示例的工作教程?
您是否查看了托管授权服务器 oauth 2和SocialApplication.java
示例是Spring Boot的一部分?
此示例配置能够使用@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"}