使用react和spring boot运行注销效果不佳

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

如果我按下React上的注销按钮来执行下面的handleLogout函数

const handleLogout = async () => {
    try {
      const response = await axios.post('http://localhost:8080/logout', {}, {
        withCredentials: true
      });
      console.log(response.data);
      window.location.href = "http://localhost:3000/login";
    } catch (error) {
      console.error('Fail to Logout', error);
    }
  };

这些请求是在下面springboot中实现的控制器上执行的

@PostMapping("/logout")
public ResponseEntity<String> logout(HttpSession session) {
    session.invalidate();
    return ResponseEntity.ok("Logout successful");
}

但是当我点击注销按钮时

HeaderEdit.js:68

   GET http://localhost:8080/login?logout 405 (Method Not Allowed)

我收到错误,刷新时看到会话值消失。有什么问题吗?

我尝试使用 axios.post 和 @PostMapping 但没有成功。

reactjs session logout
1个回答
0
投票

您的 axios 请求具有 POST 方法,而在 Spring Boot 中具有 GET 请求。更改为 POST。

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