angular-jwt如何在没有秘密的情况下解码我的JWT?

时间:2015-08-11 22:53:59

标签: node.js express jwt auth0 express-jwt

Auth0团队创建了一个名为“angular-jwt”的东西,它有一个jwtHelper类。这个东西成功地解码了一个本地JWT而没有我在服务器上使用的秘密。这怎么发生的?如果它们不安全,那么使用秘密签署/加密它们有什么意义呢?

加密令牌的服务器上的函数(使用“jsonwebtoken”):

function createToken (user) {
    return jwt.sign(_.omit(user, 'password'), config.secret, { expiresInMinutes: 60*5 });
}

来自客户的代码:

angular
    .module('sample.home', [
        'ui.router',
        'angular-storage',
        'angular-jwt'
    ])
    .config(function ($stateProvider) {
        $stateProvider
            .state('home', {
                url: '/',
                controller: 'HomeCtrl',
                templateUrl: 'modules/home/home.html',
                data: { requiresLogin: true }
            })
    })
    .controller('HomeCtrl', function homeController ($scope, $http, store, jwtHelper) {

        $scope.jwt = store.get('jwt');
        $scope.decodedJwt = $scope.jwt && jwtHelper.decodeToken($scope.jwt);

    });

以下是完整示例的链接:http://github.com/auth0/ang...

1 个答案:

答案 0 :(得分:11)

JWT使用编码,而不是加密。令牌包含的数据不是秘密,任何人都可以对其进行解码和查看。服务器的作用是,它使用秘密(在您的情况下为config.secret)对令牌进行签名,这实际上使得在不知道秘密的情况下无法修改令牌。因此,只有服务器才能更改令牌的内容,但任何人都可以阅读它。

相关问题