[英]Get session token for Spring boot/security in controller
We are using spring boot with spring security to implement a querying interface. 我们使用spring boot和spring security来实现查询接口。 What I want to do is to only allow a fixed number of queries per user to run at a time.
我想要做的是只允许每个用户一次运行固定数量的查询。 Queries may take a long time and users may send repeated requests faster than we can respond.
查询可能需要很长时间,用户可能会比我们的响应更快地发送重复请求。 I want the controller to only ever be calculating a subset request at a time and I'll have to implement some logic as to which requests to respond to.
我希望控制器一次只计算一个子集请求,我将不得不实现一些逻辑来确定响应的请求。
To do this, I need to know the session token for the given user. 为此,我需要知道给定用户的会话令牌。 Is there an easy way to get this in the controller's methods?
有没有一种简单的方法可以在控制器的方法中得到它?
I find it easier to add the parameter 'HttpSession session' in your request mapping: 我发现在请求映射中添加参数'HttpSession session'更容易:
@GetMapping(value = "/hello")
public String home(HttpSession session) {
String sessionId = session.getId();
System.out.println("[session-id]: " + sessionId);
...
return "anyPage";
}
如果要在控制器中获取sessionId,可以使用RequestContextHolder.currentRequestAttributes().getSessionId();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.