8

在传递令牌和用户名的初始握手期间,我发现了这个奇怪的错误-

    { handle: 10,
      type: 'error',
      className: 'Error',
      constructorFunction: { ref: 11 },
      protoObject: { ref: 12 },
      prototypeObject: { ref: 3 },
      properties: 
      [ { name: 'stack',
          attributes: 2,
          propertyType: 3,
          ref: 3 },
        { name: 'arguments',
          attributes: 2,
          propertyType: 1,
          ref: 3 },
        { name: 'type',
          attributes: 2,
          propertyType: 1,
          ref: 3 },
        { name: 'message',
          attributes: 2,
          propertyType: 1,
          ref: 13 } ],
        text: 'Error: Not enough or too many segments' }

JWT 格式错误?初始令牌格式错误?

4

4 回答 4

12

如果您使用的是 JWT-simple,通过查看源代码,我们可以看到此错误是由于令牌的格式不正确引起的。

//...

var segments = token.split('.');
if (segments.length !== 3) {
  throw new Error('Not enough or too many segments');
}
于 2016-11-02T21:54:24.837 回答
6

据我所知,此错误是解析 JWT 时未捕获的异常的结果,该 JWT 引用不再在 db 中的用户 - 更常见的情况是当 bcrypt 比较或您使用的任何东西发现哈希的比较是错误——我已经考虑到这一点——没有找到我没有找到的用户。当我考虑到这一点时,错误消失了。

于 2015-05-29T14:32:22.690 回答
1

检查您的令牌或加密文本是否具有三段。对于前。

var segments = token.split('.');

如果段长度为 3,则令牌是正确的。但如果不是,您必须检查您的令牌在创建和验证之间是否已被修改。

于 2016-08-02T05:23:34.703 回答
0

当我将一个混乱的回调传递给我的“then”语句时,这发生在我的 Angular 应用程序中。

// in my Auth Service

this.register = function (email, password) {
  return $http.post(API_URL + 'register', {
    email: email,
    password: password
  }).then(authSuccessful)
    .catch(authError);
};

function authSuccessful(res) {
  alert('success', 'Joy!', 'Welcome, ' + res.data.user.email + '.');
  // authToken.setToken just puts the token in local storage.
  authToken.setToken(res.token); // <- WRONG!!
  $state.go("connections");
}

它应该是:

function authSuccessful(res) {
  alert('success', 'Joy!', 'Welcome, ' + res.data.user.email + '.');
  authToken.setToken(res.data.token); // <- Yay!
  $state.go("connections");
}
于 2016-04-26T20:02:09.223 回答