Carlos Bribiescas
Carlos Bribiescas

Reputation: 4407

Get session token for Spring boot/security in controller

We are using spring boot with spring security to implement a querying interface. 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?

Upvotes: 8

Views: 19853

Answers (2)

Borgy Manotoy
Borgy Manotoy

Reputation: 2038

I find it easier to add the parameter 'HttpSession session' in your request mapping:

@GetMapping(value = "/hello")  
public String home(HttpSession session) {  
    String sessionId = session.getId();  
    System.out.println("[session-id]: " + sessionId);  

    ...

    return "anyPage";  
}

Upvotes: 13

Sainik Kumar Singhal
Sainik Kumar Singhal

Reputation: 801

If you want to get sessionId in controllers you can use RequestContextHolder.currentRequestAttributes().getSessionId();

Upvotes: 18

Related Questions