我正在尝试使用SpringBoot和Reactjs进行Web应用程序 . 我在管理JWT时遇到了麻烦 . 通过使用Postman我可以发送我的 Headers 'jwt'并测试我的应用程序,它的工作原理 . 但是当我尝试使用React时
axios.get('URL/logout', { headers: { jwt: localvariable } } )
.then(response => {
console.log('Response of logout', response);
this.setState({isLogged: false});
console.log('Status after logout', this.state);
})
.catch(error => {
console.log(error);
});
我无法从客户端收到任何 Headers 'jwt' . 处理客户请求的代码是:
@CrossOrigin("*")
@RestController
public class LoginController {
@RequestMapping("/logout")
public ResponseEntity<JsonResponseBody> logoutUser(@RequestHeader(value="jwt") String jwt,HttpServletRequest request){
System.out.println("My jwt is: "+jwt);
return ResponseEntity.status(HttpStatus.OK)
}
}
而我刚刚收到:
{
"timestamp": 1526311750256,
"status": 400,
"error": "Bad Request",
"exception": "org.springframework.web.bind.ServletRequestBindingException",
"message": "Missing request header 'jwt' for method parameter of type String",
"path": "/logout"
}
请问,有什么建议吗?也许客户端需要授权才能发送标头?我被卡住了 . 谢谢
1 回答
我成功了我必须创建一个axios istance,现在它的工作原理 . 谢谢大家