NodeJS 为什么我在头的授权部分传递JWT令牌时会收到431状态错误?

voj3qocg  于 2023-10-17  发布在  Node.js
关注(0)|答案(1)|浏览(129)

所以我正在使用Express和Node创建一个全栈React应用程序。我已经实现了auth 0的身份验证和授权,但我得到了以下错误,有人有任何建议如何解决它?谢谢你,谢谢

"Failed to load resource: the server responded with a status of 431 (Request Header Fields Too Large)"

下面是我调用API/tasks路由并从Auth 0传入jwt访问令牌的类组件。

import React, { Component } from 'react';
import axios from "axios";
import {withAuth0} from "@auth0/auth0-react";
import config from "../auth_config.json";
const {apiOrigin = "http://localhost:3001"} = config

componentDidMount() {
        const getProtectedTasks = async () => {
            const {getAccessTokenSilently} = this.props.auth0;
            const accessToken = await getAccessTokenSilently();
            try {
                const token = accessToken;
                const self = this;
                const response = await axios.get(`${apiOrigin}/api/tasks`, {
                    headers: {
                        Authorization: `Bearer ${token}`
                    }
                })
                self.setState({
                    allTasks: response.data
                })

            } catch {

            }
        }
        getProtectedTasks()

    }

下面是我处理get API/tasks端点的代码

const db = require("../models");
const jwt = require("express-jwt");
const jwksRsa = require("jwks-rsa");

module.exports = function (router) {
    const authorizeAccessToken = jwt({
        secret: jwksRsa.expressJwtSecret({
            cache:true,
            rateLimit:true,
            jwksRequestsPerMinute:5,
            jwksUri: `https://task-motivator.us.auth0.com/.well-known/jwks.json`
        }),
        audience: "https://quickstarts/api",
        issuer: "https://task-motivator.us.auth0.com",
        algorithms: ["RS256"]
    });

    router.get("/api/tasks", authorizeAccessToken, (req, res) => {
        db.Task.findAll({}).then(data => {
            res.json(data);
        });
    });
ui7jx7zq

ui7jx7zq1#

由于JWT令牌的长度,我遇到了同样的问题。如果你不提供更多的数据,很难知道原因(正如人们在评论中所说的那样),但是试着让你的数据到jwt-sign更短一点!

相关问题