【问题标题】:How to integrate AngularJS and Spring-OAuth2?如何集成 AngularJS 和 Spring-OAuth2?
【发布时间】:2016-03-30 17:53:48
【问题描述】:

我使用 Spring-Boot-1.3.1 开发了 Spring-Rest API。然后我为这些 API 提供了 Spring-OAuth2 安全性。这作为单个项目驻留在 Apache Tomcat 中。 我开发的 API:

GET -> /api/articles
GET -> /api/articles/1
POST -> /api/articles

Spring-Oauth2 默认提供以下 API:

POST -> /oauth/token
POST -> /oauth/authorize

还有一些其他的 api。

现在我正在开发 AngularJS UI,它将驻留在不同的服务器-Apache2-Http 服务器中。

我需要知道如何将 angularJS 登录页面与 Spring-OAuth2 保护的 Spring-REST 集成?

【问题讨论】:

    标签: angularjs spring-security spring-rest spring-oauth2


    【解决方案1】:

    使用代码:

    var data = "username=" + user.email + "&password=" + encodeURIComponent(user.password) + "&grant_type=password&scope=read%20write&" +
                        "client_secret=secret&client_id=frontend";
    
    
    
                $http.post(portalResources.login, data, {
                    headers: {
                        "Content-Type": "application/x-www-form-urlencoded",
                        "Accept": "application/json",
                        "Authorization": "Basic " + $base64.encode("frontend" + ':' + "secret")
                    }
                });
    

    【讨论】:

      【解决方案2】:

      您可以使用以下库来管理令牌 angular-oauth2

      步骤:

      1. 将登录凭据发送到 /oauth/token
      2. 捕获响应,提取令牌并将其添加到 $http 默认标头中,以便每次请求都会发送令牌

      【讨论】:

        猜你喜欢
        • 2018-02-08
        • 2017-02-19
        • 2015-06-10
        • 2015-03-17
        • 2017-10-27
        • 2012-12-31
        • 2016-06-09
        • 1970-01-01
        • 2015-11-13
        相关资源
        最近更新 更多