Retrieving Session ID with Spring Security

后端 未结 2 1948
生来不讨喜
生来不讨喜 2020-12-24 02:32

For logging purposes, I\'d like to create a logger that automatically adds the current session\'s ID to logged lines.
For logged in users this isn\'t a problem:

相关标签:
2条回答
  • 2020-12-24 02:46

    More easiest way is:

    @GetMapping(path = "/foo")  
    public void foo(HttpSession session) {  
        String sessionId = session.getId();  
    }
    
    0 讨论(0)
  • 2020-12-24 02:55

    You may use

    RequestContextHolder.currentRequestAttributes().getSessionId();
    

    This relies on Spring's RequestContextHolder, so it should be used with Spring MVC's DispatcherServlet or you should have a RequestContextListener declared. Also session will be created if not exists.

    0 讨论(0)
提交回复
热议问题