JWT签名验证失败,java到php

我与你分享第一个代码</ p>

php代码
http://pastebin.com/b8TNfyzq 第22行</ p>

JwtTester.java
http://pastebin.com/TsF0wsCX 第22行</ p>

创建的令牌 我在php服务器上写的java代码与令牌不匹配。
虽然我无法在双方验证相同的密钥</ p>

我在java代码中使用github.com/jwtk/jjwt php代码中的
和github.com/firebase/php-jwt </ p>

与密码中的java代码和数据相同,当我仅在PHP中创建令牌时,由不同的令牌组成</ p>

</ div>

展开原文

原文

I share with you the first codes

php codes http://pastebin.com/b8TNfyzq line 22

JwtTester.java http://pastebin.com/TsF0wsCX line 22

Token created in java code I wrote in php server does not match the token. Although I can not verify the same key on both sides

I'm using github.com/jwtk/jjwt in java code and github.com/firebase/php-jwt in php codes

same with java code and data in key , consists of different token when I create the token in only PHP

duanjia6959
duanjia6959 不要链接代码。复制代码
大约 4 年之前 回复

2个回答



格式转换问题。 jjwt需要在base64中编码的密钥,php-jwt使用普通字符串</ strong> </ p>

Jjwt JwtBuilder </ p>

  JwtBuilder signWith(SignatureAlgorithm alg,  String base64EncodedSecretKey); 
</ code> </ pre>

Php-jwt JWT </ p>

  / ** 
*将JWT字符串解码为PHP对象。
  • @ param string $ jwt JWT
  • @param string | array $ key键或键映射。
    *如果使用的算法是非对称的,这是公钥
    公共静态函数解码($ jwt,$ key) ,$ allowed_algs = array()
    </ code> </ pre>

    在调用 JwtBuilder.signWith </ code> </ p>

之前,在base64中对密码进行编码 pre> builder.signWith(SignatureAlgorithm.HS256,
DatatypeCon verter.printBase64Binary(key));
</ code> </ pre>
</ div>

展开原文

原文

Is a format conversion issue. jjwt requires a key encoded in base64 and php-jwt uses a plain string

Jjwt JwtBuilder

JwtBuilder signWith(SignatureAlgorithm alg, String base64EncodedSecretKey);

Php-jwt JWT

 /**
 * Decodes a JWT string into a PHP object.
 *
 * @param string        $jwt            The JWT
 * @param string|array  $key            The key, or map of keys.
 *                                      If the algorithm used is asymmetric, this is the public key
public static function decode($jwt, $key, $allowed_algs = array()

Encode your key in base64 before invoking JwtBuilder.signWith

builder.signWith(SignatureAlgorithm.HS256, 
                               DatatypeConverter.printBase64Binary(key));

for people still facing the issue. if you are creating jwt from java then add the "typ" header to it which is checked by PHP-jwt. Also jjwt encodes the key to base64 so from Java

String jwtSecret = "yoursecret";

Map<String, Object> header = new HashMap<>();
header.put("typ", Header.JWT_TYPE);

String jwt = Jwts.builder()
                    .setHeader(header)
                    .setSubject("someclaim")
                    .setIssuedAt(new Date())
                    .setExpiration(expiryDate)
                    .signWith(SignatureAlgorithm.HS512,TextCodec.BASE64.encode(jwtSecret))
                    .compact();

in php

define('SECRET', 'yoursecret');
$decoded = (array) JWT::decode($jwt,SECRET, array('HS512'));

Also it doesn't seems to decode correctly if the secret contains special characters.

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐