错误:错误:1E08010C:解码器例程::在 NodeJS/ExpressJS 中不受支持

问题描述 投票:0回答:1

当我尝试在请求的授权标头中执行 jwt.verify() 不记名令牌时,我在服务器上收到此错误

Error: error:1E08010C:DECODER routines::unsupported
    at Verify.verify (node:internal/crypto/sig:230:24)
    at Object.verify (.../node_modules/jwa/index.js:164:21)
    at Object.jwsVerify [as verify] (.../node_modules/jws/lib/verify-stream.js:54:15)
    at .../node_modules/jsonwebtoken/verify.js:127:19
    at getSecret (.../node_modules/jsonwebtoken/verify.js:90:14)
    at module.exports [as verify] (.../node_modules/jsonwebtoken/verify.js:94:10)
    at exports.verify (.../middleware.js:30:17)
    at Layer.handle [as handle_request] (.../node_modules/express/lib/router/layer.js:95:5)
    at next (.../node_modules/express/lib/router/route.js:137:13)
    at Route.dispatch (.../node_modules/express/lib/router/route.js:112:3) {
  library: 'DECODER routines',
  reason: 'unsupported',
  code: 'ERR_OSSL_UNSUPPORTED'
}

我尝试过使用 Node v16、v18、v20、 还尝试将 --openssl-legacy-provider 添加到我的脚本中并导出到路径中, 并将我的 M1 机器上的 OpenSSL 更新到最新的@3.2.1, 甚至尝试将公钥编码为 Base64 字符串,但没有任何效果。

这是代码:

exports.verify = async (req, res, next) => {

    const publicKey = `-----BEGIN PUBLIC KEY-----///-----END PUBLIC KEY-----`

    const accessToken = req.headers.authorization.split(" ")[1]
    
    if(!accessToken) res.sendStatus(403).json({ error: "please provide a token" })
    else {
        try {
            jwt.verify(
                accessToken,
                publicKey,
                { algorithms: ['RS256'] },
                (err, decoded) =>
                  {if(err)
                    {
                        console.log(err)
                    }
                    else {
                        resolve(decoded)
                        next()
                    }}
                    
              )
        }
        catch(err) {
            console.log(err)
        }
    }
}

相同的代码之前可以工作,但改变的是不同的身份验证服务,因此我使用不同的公钥,并且我还获得了由不同服务提供商签名的令牌。

谢谢。

node.js express authentication jwt openssl
1个回答
0
投票

在您的本地环境中,您应该将其放在一行中并带有“”:PUBLIC_KEY =“-----BEGIN PUBLIC KEY----- 你的钥匙 -----结束公钥-----"

在部署的环境中,您应该不带“”:PUBLIC_KEY=-----BEGIN PUBLIC KEY----- 你的钥匙 -----结束公钥-----

这对我有用。

© www.soinside.com 2019 - 2024. All rights reserved.