简体   繁体   English

解密/验证Firebase令牌

[英]Decrypt / Verify Firebase token

I'm using Firebase with a Email & Password authentication. 我正在使用Firebase进行电子邮件和密码身份验证。 Once the user has been signed in successfully I'll receive an AuthData object which contains a token. 用户成功登录后,我将收到一个包含令牌的AuthData对象。
I wanna send this token to my backend, verify it and extract the uid from it - unfortunately I don't know how to do this. 我想将此令牌发送到我的后端,进行验证并从中提取uid不幸的是,我不知道该怎么做。

I'm aware of the Firebase secret and if I go to jwt.io , enter the token and the secret the signature is verified and I see the correct payload - so this actually works. 我知道Firebase机密,如果我转到jwt.io ,请输入令牌,并且机密会验证签名,并且我会看到正确的有效负载-因此,这实际上可行
Since I'm running Java on my backend I've been using jjwt for the decryption process unfortunately it always throws a SignatureException : 由于我在后端运行Java,因此我一直在将jjwt用于解密过程,不幸的是,它总是抛出SignatureException

io.jsonwebtoken.SignatureException: JWT signature does not match locally computed signature. io.jsonwebtoken.SignatureException:JWT签名与本地计算的签名不匹配。 JWT validity cannot be asserted and should not be trusted. JWT的有效性不能断言,不应该被信任。

Following the code I've been using: 按照我一直在使用的代码:

SignatureAlgorithm signatureAlgorithm = SignatureAlgorithm.HS256;
byte[] encodedKey = Base64.decode("my-firebase-secret", Base64.DEFAULT);
Key k = new SecretKeySpec(encodedKey, signatureAlgorithm.getJcaName());
Claims claims = Jwts.parser()
                    .setSigningKey(k)
                    .parseClaimsJws("the-token").getBody();

I've also tried it with the following snippet: 我还尝试了以下代码段:

Claims claims = Jwts.parser()
                    .setSigningKey(DatatypeConverter.parseBase64Binary("my-firebase-secret"))
                    .parseClaimsJws(jwt).getBody();

But got the same exception. 但是有同样的例外。 So what am I doing wrong? 那我在做什么错? Thanks in advance. 提前致谢。

Have you tried to use getBytes instead of parseBase64Binary? 您是否尝试过使用getBytes代替parseBase64Binary? Here is the example: 这是示例:

Claims claims = Jwts.parser().setSigningKey("my-firebase-secret".getBytes("UTF-8")).parseClaimsJws(jwt).getBody();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM