2016-06-19 155 views
0

我正在尝试编写一个Spring Boot应用程序,它将使用Google单一登录(SSO)对用户进行身份验证(可能是任何其他SSO提供程序,如Facebook--此示例仅使用Google)。Spring Boot和Google SSO

我跟着几个教程和一个非常基本的设置提出了:

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(例如使用cUrlRestClient)。

我试过如下:

curl xxx:[email protected]: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春天引导蜜蜂编程工作的一些好的文档或教程。有人可以解释我缺少的东西吗?或者指向一些具有完整功能的多用户API示例的工作教程?

回答

1

你看过Hosting an Authorization Server OAuth 2SocialApplication.java是Spring Boot的一部分吗?

本示例配置可以使用@EnableAuthorizationServer批注授予OAuth令牌的服务器。

还有两个curl示例说明客户机可以请求访问令牌:

$ curl acme:[email protected]: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:[email protected]: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"} 
+0

感谢您的链接。稍后会进行测试,但看起来很有希望! – Smajl

+0

似乎工作,但我仍然无法弄清楚如何编写我的休息客户端请求(cUrl或类似的实用工具)连接到API ... – Smajl