自动装配HttpSession提供与HttpServletRequest不同的对象

时间:2017-03-07 10:47:38

标签: java spring-mvc spring-security

我使用Spring Security,我在登录时发现了奇怪的框架行为。 Spring Security WebAuthenticationDetails具有从HTTP请求获取的参数sessionId,并且一切都应该是好的,但实际上REST请求给了我另一个会话ID。如果我将自动装配HttpSession然后从中获取会话ID,我将获得类似Spring的ID。所以我似乎有一个用户的两个id。这是对的吗?或者我错过了什么?

编辑:

例如,此类将提供一些会话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();
        }
    }
}

这个方法将给出另一个方法:

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

1 个答案:

答案 0 :(得分:0)

所以答案是下一个:默认情况下安全条件Spring更改会话ID。要防止此类行为,您需要在Spring Security配置中禁用会话固定保护。更多信息link

相关问题