我有spring(4.0.6) web应用程序。我注意到,当用户登录然后注销并尝试再次登录时,会出现关于用户pass的错误。我没有web.xml文件,因为我使用SpringBootServletInitializer
类来配置我的应用程序。我在配置中添加了bean
@Bean
public ServletListenerRegistrationBean<HttpSessionEventPublisher> httpSessionEventPublisher()
{
return new ServletListenerRegistrationBean<HttpSessionEventPublisher>(new HttpSessionEventPublisher());
}
和在安全配置中我有这个:
http.sessionManagement()
.maximumSessions(1).expiredUrl("/login?expired")
.maxSessionsPreventsLogin(true)
.and()
.sessionCreationPolicy(SessionCreationPolicy.IF_REQUIRED)
.invalidSessionUrl("/");
据我所知,以前的会话是活的,因为最大会话= 1用户不能再次登录。我该怎么做才能避免呢?
您应该在注销时invalidate the user session
。下面是这段代码。需要传递用户id
UserInfo
为POJO
public boolean invalidateUserSession(Long userId) {
List<Object> principalsList = sessionRegistry.getAllPrincipals();
Object targetPrincipal = null;
for (Object principal : principalsList) {
if (principal instanceof UserInfo) {
if (((UserInfo) principal).getId().equals(userId)) {
targetPrincipal = principal;
break;
}
}
}
if (targetPrincipal == null) {
return false;
}
List<SessionInformation> userSessionsList = sessionRegistry.getAllSessions(targetPrincipal, false);
for (SessionInformation x : userSessionsList) {
x.expireNow();
}
return true;
}