【问题标题】:Cloud Endpoints + AngularJS authentication without gapi没有 gapi 的 Cloud Endpoints + AngularJS 身份验证
【发布时间】:2014-04-01 16:20:49
【问题描述】:

我在一个项目中使用了 angular+cloud 端点,我想通过使用 promises 来保持它的 angular-y(这与云端点 js 库 as written here 不兼容)。我设法通过使用 $http 调用 api 来做到这一点。

例子

$http.get("/_ah/api/myapi/v1/mypath").success(function(data){
    //edited for clarity
}).error(function () {
    //edited for clarity
});

效果很好。

但是当我尝试在我的 api 方法中使用 User 参数时(我想让用户在我的端点中),我发现自己需要使用 OAuth 身份验证(因为通常的 web.xml 安全约束不适用于云端点)。

web.xml(安全约束)

<security-constraint>
    <web-resource-collection>
        <web-resource-name>All</web-resource-name>
        <url-pattern>/*</url-pattern>
    </web-resource-collection>
    <user-data-constraint>
        <transport-guarantee>CONFIDENTIAL</transport-guarantee>
    </user-data-constraint>
    <auth-constraint>
        <role-name>*</role-name>
    </auth-constraint>
</security-constraint>

端点示例 (java)

public Person getMyself(User user) throws OAuthRequestException {
    // edited for clarity
    return person;
}

我想知道是否有办法验证使用 Angular 的 $http 进行的 api 调用,还是我真的必须使用谷歌的端点 js 库 (gapi)?

【问题讨论】:

    标签: angularjs google-cloud-endpoints


    【解决方案1】:

    您可以使用GCloud BlogGitHub Example 中所述的登录流程

    您将获得访问令牌:

    var token = gapi.auth.getToken();
    

    那么就可以调用$http函数如下:

    $http({method: 'GET', url: PATH/getMyself, headers: {'Authorization': 'Bearer ' + token}}); 
    

    后端:

    public Person getMyself(User user) throws OAuthRequestException {
        // edited for clarity
        return person;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-10-11
      • 1970-01-01
      • 2020-06-10
      • 2016-02-03
      • 1970-01-01
      • 2017-07-06
      • 2018-12-29
      相关资源
      最近更新 更多