【问题标题】:Error capturing JWT捕获 JWT 时出错
【发布时间】:2016-04-04 02:49:40
【问题描述】:

我正在研究 jwt angular + java jersey 身份验证。我能够将 jwt 令牌从服务器发送回客户端,这可以看作是 Chrome 控制台中状态代码为 200 的 POST 响应。

我的问题是我无法将它保存在变量中以便我可以执行解码。我收到这样的错误:

TypeError: href is null
e/<()angular.min.js (line 107)
Ze/this.$get</<()angular.min.js (line 81)
f/<()angular.min.js (line 119)
lf/this.$get</r.prototype.$eval()angular.min.js (line 133)
lf/this.$get</r.prototype.$digest()angular.min.js (line 130)
lf/this.$get</r.prototype.$apply()angular.min.js (line 134)
g()angular.min.js (line 87)
T()angular.min.js (line 92)
Uf/</w.onload()angular.min.js (line 93)
stackFrame.js (line 357)
<System>
error is:undefined

这是我的代码:

myApp.controller('loginController', ['$scope', '$http', function ($scope, $http)
{
$scope.email = "";
$scope.password = "";

$scope.loginForm = function () {
    var data = {email: $scope.email, password: $scope.password};
    var url = 'rs/loginResource';
        $http.post(url, data).success(function (response)
        {
            console.log("token is: " + response);
        }).error(function (error)
        {
            console.log("error is:" + error);
        });
};
}]);

【问题讨论】:

    标签: javascript angularjs jwt


    【解决方案1】:

    您是否以 JSON 格式发送令牌?还是纯文本?

    我最近在 HAM 堆栈上使用了基于 jwt 的身份验证,并在对象中提供了令牌,因此响应类似于 { token: 'asdasds.sdadad.asdad' },我通常以以下形式在 Angular 中调用服务器:

    $http({
        method: 'POST',
        url: 'rs/loginResource',
        data: {
            email: $scope.email,
            password: $scope.password
        }
    })
    .then(function (resp) {
        var token = resp.data.token
    })
    .catch(function(error){
        return error;
    });
    

    【讨论】:

    • 我正在使用Response.ok(token).build(); 球衣方法发送令牌。我猜想 jax-rs jersey 的自动响应生成器功能导致了这个问题。令牌的形式如下:eyJhbGciOiJSUzI1NiJ9.eyJzdWIiOiJhbGljZSIsImlzcyI6Imh0dHBzOlwvXC9jMmlkLmNvbSIsImV4cCI6MTQ1MTQxNDU0MH0 .HvguthVdVZ8IuP6Hsko95I70jB41jpZLqAC4VAmphDJcnRwdBBkeVBVS8RNP9z1J1c7bjzeXL5gaufr6P02GhoMwh37l58Sxzwu 8dyxLZOyyEI7A03yZkmXzRKi_TPPG-vQR6Ya9omUPioG1mPU9ZOI4XTuZooRyxph8uh9JJfw
    • 您是否使用 java jersey 作为后端?
    • 不,我在 Nodejs 上使用 Hapijs 作为后端,但实际上这并不重要,重要的是当您以角度接收响应对象时,它的外观如何。我刚刚注意到您正在使用 .success(function(response)) 来接收已解决的承诺。你能用 .then(function(response)) 替换它吗?
    • 并将 .error(function(error)) 替换为 .catch(function(error))
    • 没关系,我想出了如何捕获令牌并将其保存在变量中,现在问题是我无法在客户端对其进行解码。我正在使用 nimbus jose+jwt connect2id.com/products/nimbus-jose-jwt/examples/…
    猜你喜欢
    • 2018-05-30
    • 1970-01-01
    • 2017-06-10
    • 2012-09-19
    • 2018-06-04
    • 2016-02-09
    • 1970-01-01
    • 1970-01-01
    • 2023-03-28
    相关资源
    最近更新 更多