首页 > 解决方案 > MalformedJwtException:JWT 字符串必须恰好包含 2 个句点字符

问题描述

我正在创建一个带有集成了 jwt 的 spring boot 后端的 react 项目。虽然我可以让我的注册 api 在 POSTMAN 上工作。当我通过反应应用程序(通过 axios)调用我的 api 时,它会发出MalformedJwtException 。甚至在我将 Spring Security 集成到后端之前,一个 GET api 调用就可以正常工作。我认为这与我创建此问题的 HTTPClient.js 文件有关。这是下面

import axios from 'axios'

const id_token = "secret";
axios.defaults.headers.post['Content-Type'] = 'application/json';

var instance = null;

export const setAuth = () => {
instance = axios.create({
    baseURL: '',
    timeout: 30000,

    headers: {
        'Authorization': 'Bearer ' + localStorage.jwt,
        'Content-Type': 'application/json'
    }
}
)

instance.interceptors.response.use(function (response) {
    return response;
}, function (error) {
    if (error.response.status === 401) {
        localStorage.removeItem('jwt');
        localStorage.removeItem('user');
        window.location = '/'
    }
    else {
        return Promise.reject(error);
    }
});
}

 export const Get=(route, data)=>{
    instance|| setAuth()

    return instance.get(route,data)
 }

 export const Post = (route, data) => {
  instance || setAuth()
  return instance.post(route, JSON.stringify(data))
 }

export const Put = (route, data) => {
instance || setAuth()
return instance.put(route, JSON.stringify(data))
}

export const Delete = (route, data) => {
instance || setAuth()
return instance.delete(route, JSON.stringify(data))
}

编辑

cache-control: no-cache, no-store, max-age=0, must-revalidate
connection: close
content-length: 0
date: Sat, 14 Dec 2019 14:47:21 GMT
expires: 0
pragma: no-cache
x-content-type-options: nosniff
x-frame-options: DENY
X-Powered-By: Express
x-xss-protection: 1; mode=block
POST /register HTTP/1.1
Host: localhost:3000
Connection: keep-alive
Content-Length: 193
Accept: application/json, text/plain, */*
Origin: http://localhost:3000
Authorization: Bearer undefined
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.79 Safari/537.36
Content-Type: application/json
Sec-Fetch-Site: same-origin
Sec-Fetch-Mode: cors
Referer: http://localhost:3000/Welcome
Accept-Encoding: gzip, deflate, br
Accept-Language: en-US,en;q=0.9

这是我的请求标头

标签: reactjsspring-bootreact-reduxjwtaxios

解决方案


我尝试在请求中根本不发送授权标头。不能将其视为未定义,它根本不应该存在。


推荐阅读