angular-jwt 如何在没有秘密的情况下解码我的 JWT? [英] How is angular-jwt decoding my JWT without a secret?

查看:52
本文介绍了angular-jwt 如何在没有秘密的情况下解码我的 JWT?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

The Auth0 team created something called "angular-jwt" which has a jwtHelper class. This thing successfully decodes a local JWT without the secret I used on the server. How did this happen? If they are not secure, then what is the point of using a secret to sign/encrypt them?

服务器上加密令牌的功能(使用jsonwebtoken"):

Function on the server that encrypts the token (using "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...

推荐答案

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

A JWT uses encoding, not encryption. The data that the token contains is not a secret, anyone can decode it and view. What the server does, is it signs the token using a secret (in your case, config.secret), which effectively makes it impossible to modify the token without knowing the secret. Hence, only the server will be able to change the contents of the token, but anyone can read it.

这篇关于angular-jwt 如何在没有秘密的情况下解码我的 JWT?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆