Spring React 和 Sessions .. 如何保持会话 [英] Spring React and Sessions.. how to keep session
问题描述
我已经设置了我的 spring 来维护一个对象上的 HTTP 会话,如下所示:
I have set up my spring to maintain a HTTP session on an object like so:
@Component
@SessionScope
public class Basket { .. }
控制器:
@PostMapping(path="/basket/addItem/{user}", consumes = "application/json", produces = "application/json")
public Basket createBasket(@PathVariable String user, @RequestBody Item item) {
System.out.println("POSTING..................................");
return basketService.addItem(user, item);
}
现在,当我使用 REST 客户端时,在 firefox 中我可以看到会话 bean 是在持续时间内创建和维护的 - 多次调用.我可以附加到对象.如果我尝试另一个客户端,它会使用自己的 bean 获得自己的会话.太好了..
now when i use a REST client, in firefox i can see that the session bean is created and maintained for the duration - multiple calls. I can append to the object. If i try another client, it gets its own session with its own bean. great..
spring 记录以下内容:
spring logs the following:
Creation of SecureRandom instance for session ID generation using [SHA1PRNG] took [269] milliseconds.
然而,我试图在 React 中创建一个基本的前端,当 React 使用 axios 发出请求时,它每次都会获得一个新的 bean,这意味着会话必须在每次调用后结束.那是正确的吗?或者我没有将它绑定到反应应用程序...
However im trying to create a basic front end in react, when react makes a request using axios it gets a new bean every time, which means that the session must be ending after each call. IS that correct? or im not tying it to the react application...
也许我采取的方法不正确,也许我应该使用不同的方法,我正在尝试了解 spring boot,所以它是一个基本项目......现在我想维护购物车的用户会话.所以后续调用我可以附加到对象...
Maybe the approach im taking is not correct, maybe i should use a a different approach, Im trying to learn about spring boot, so its a basic project... and right now i want to maintain user session for a cart. so subsequent calls i can append to the object...
推荐答案
通过将以下内容添加到我的控制器中,一切都开始工作了.
by adding the following to my controller it all began to work.
@CrossOrigin(origins = { "http://localhost:3000" }, allowedHeaders = "*", allowCredentials = "true")
这篇关于Spring React 和 Sessions .. 如何保持会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!