简体   繁体   中英

Autowiring HttpSession give different object than HttpServletRequest

I use Spring Security, and I found strange behavior of framework while login. Spring Security WebAuthenticationDetails has parameter sessionId which is getting from HTTP request, and it all should be good, but in fact REST request gives me another session id. If I will autowire HttpSession and then get session id from it, I will get Spring-like id. So it seems that I have two ids for one user. Is it correct? Or I missed something?

EDITED:

For example this class will gave some session id

public class AuthenticationEventListener implements ApplicationListener<AbstractAuthenticationEvent> {

    @Autowired
    HttpSession httpSession;

    @Override
    public void onApplicationEvent(AbstractAuthenticationEvent event) {
        if (event instanceof AuthenticationSuccessEvent) {
            LoggedUser loggedUser = (LoggedUser) event.getAuthentication().getPrincipal();
            loggedUser.initSessionParams(event.getAuthentication());
            String sessionId = httpSession.getId();
        }
    }
}

and this method will give another one:

@RequestMapping(value = "/chart")
public Map getTestStatusesChart(HttpServletRequest request) {
    String sessionId= request.getSession(false).getId();
    return null;
}

So the answer is next: with condition of security Spring change session id by default. To prevent such behavior you need to disable session-fixation-protection in Spring Security config. more info by link

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM