简体   繁体   English

获取控制器中Spring启动/安全性的会话令牌

[英]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.

相关问题 Spring Boot,会话安全 - Spring Boot, Session Security 如何使用Spring Boot Security从Facebook获取访问令牌 - How to get access token from facebook using spring boot security 使用 JWT 令牌安全性进行 Spring Boot 单元测试 - Spring Boot Unit Tests with JWT Token Security Spring 具有自定义身份验证和 session 存储的引导安全性 - Spring Boot Security with custom auth & session storage 如何使用 Spring Security 5 在 Spring Boot 应用程序(不是 Web 应用程序)中获取 oauth2 访问令牌 - How to get oauth2 access token in a spring boot application (not a web application) using spring security 5 Spring Session,Websocket,REST令牌安全 - Spring Session, Websocket, REST Token Security 在 Spring Security 中获取访问令牌 - Get Access token in Spring Security Spring boot security oauth2 从 cookie 中获取 access_token - Spring boot security oauth2 get access_token from cookie 无法在 spring 引导安全中跳过登录 url (基本上是在初始登录期间获取 JWT 令牌)的 OncePerRequestFilter 过滤器 - Unable to skip the OncePerRequestFilter filter for a login url (basically to get the JWT token during the initial login)in spring boot security 弹簧启动与春季会议和安全。 RMI因401失败 - Spring boot with spring session and security. RMI fails with 401
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM