【问题标题】:Spring Boot and Google SSOSpring Boot 和 Google SSO
【发布时间】: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(例如使用 cUrlRestClient)。

我尝试了以下方法:

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


    【解决方案1】:

    您是否查看过属于 Spring Boot 一部分的 Hosting an Authorization Server OAuth 2SocialApplication.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"}
    

    【讨论】:

    • 感谢您的链接。稍后会对其进行测试,但看起来很有希望!
    • 似乎可以工作,但我仍然无法弄清楚如何编写我的休息客户端请求(cUrl 或类似实用程序)以连接到 api...
    猜你喜欢
    • 1970-01-01
    • 2019-12-26
    • 2017-09-05
    • 1970-01-01
    • 2019-08-21
    • 1970-01-01
    • 2018-07-24
    • 2019-01-08
    • 2020-12-30
    相关资源
    最近更新 更多