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

2024-02-14

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... https://github.com/auth0/angularjs-jwt-authentication-tutorial


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

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Angular-jwt 如何在没有秘密的情况下解码我的 JWT? 的相关文章

随机推荐