【问题标题】:Integration of Spring Oauth2 implementation with the HTML front end using javascript使用 javascript 将 Spring Oauth2 实现与 HTML 前端集成
【发布时间】:2017-02-06 08:33:22
【问题描述】:

我正在实施 spring oauth2 来保护我的 rest api。基本上,我将 rest api 的使用限制在特定用户,而不是仅限于每个用户。

我已经使用 spring oauth2 实现了后端并保护了我的 api。

我正在执行以下步骤:
1)发送带有五个参数的GET请求。

localhost:8080/SpringRestSecurityOauth/oauth/token?grant_type=password&client_id=Awyi123nasdk89&client_secret=asdj39m32@#$s&username=rahul@gmail&password=rahul@9831

2) 服务器在spring security的帮助下验证用户,并返回带有访问码的json响应。

{ "access_token": "22cb0d50-5bb9-463d-8c4a-8ddd680f553f",
"token_type": "承载者",
"refresh_token": "7ac7940a-d29d-4a4c-9a47-25a2167c8c49",
“expires_in”:119
}


3)我通过将此访问令牌作为参数传递来访问受保护的资源,请求如下所示:

localhost:8080/SpringRestSecurityOauth/api/users/?access_token=8c191a0f-ebe8-42cb- bc18-8e80f2c4238e

4) 如果令牌过期,用户需要使用在步骤(2)中接受的刷新令牌来获取新令牌。过期后的新访问令牌请求如下所示:

localhost:8080/SpringRestSecurityOauth/oauth/token?grant_type=refresh_token&client_id=restapp&client_secret=restapp&refresh_token=7ac7940a-d29d-4a4c-9a47-25a2167c8c49强>

以上所有步骤都可以正常工作。现在我需要在我的客户端实现这个。以便特定客户端可以访问此调用。我使用 HTML/CSS 作为客户端技术。

客户端如何获取访问令牌?它应该存储在浏览器本地存储中吗?还是应该在每次调用 rest 时生成?

任何例子都可以帮助我更进一步。

【问题讨论】:

    标签: html rest spring-mvc oauth-2.0 spring-oauth2


    【解决方案1】:

    我正在像你一样实施我的项目。我使用 angularjs 并从响应 json 获取访问令牌,然后将其存储到 cookie 中。 此链接为您提供示例代码:http://www.baeldung.com/rest-api-spring-oauth2-angularjs。 (请参阅前端 - 密码流程)。

    因为刷新令牌应该保密并且客户端是html应用程序,你应该看到这个链接http://www.baeldung.com/spring-security-oauth2-refresh-token-angular-js处理刷新令牌。

    对于html客户端,在访问令牌过期时使用刷新令牌获取访问令牌后,我使用http-auth-interceptor([http]://witoldsz.github.io/angular-http-auth/)重试所有由于访问令牌过期,其余请求失败。

    很抱歉,我没有足够的声誉来发布超过 2 个链接。

    【讨论】:

      猜你喜欢
      • 2016-06-09
      • 2014-09-14
      • 1970-01-01
      • 2018-11-05
      • 2012-07-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-06
      相关资源
      最近更新 更多