我想将一个身份验证令牌从 reactjs(前端)传递到 springboot(后端)应用程序。 我已经使用以下代码验证了我的前端。
现在我在 cookie(at_origin: "some random value") 中有值,我需要传递它来调用这个 auth_status api。在 React 中,我执行了以下操作并且有效。
export const userAuthStatus = async () => {
let authStatus = [];
authStatus = axios.post('https://../auth_status', { resources: [{ classpath: 'test }] },
{ withCredentials: true }).then(response => response.data.user);
return authStatus;
};
从 react(front end) 代码,我正在调用后端,我需要传递上述 cookie 来检查身份验证状态。在从 springboot(后端)调用 auth_status 之前,我需要将 cookie(我只需要“at_origin”键值)设置为 X-auth。
我通过以下方式将所有 cookie 值从前端传递到后端。
try {
const url = `https://backend api url`;
const res = await axios.get(url, { withCredentials: true }).then(res => res);
return res;
} catch (error) {
console.log(error);
}
但我不确定如何在后端获取值 (at_origin)。 可以请我帮忙吗!
提前致谢,
答案 0 :(得分:0)
最简单的方法是在带有 @CookieValue
注释的控制器中使用它:
@RequestMapping("/hello")
public String hello(@CookieValue("foo") String fooCookie) {
// ...
}
否则,您可以使用 Spring org.springframework.web.util.WebUtils
WebUtils.getCookie(HttpServletRequest request, String cookieName)