Spring security Spring引导安全注销不会使会话无效

Spring security Spring引导安全注销不会使会话无效,spring-security,spring-boot,Spring Security,Spring Boot,我的增强型宠物诊所应用程序需要安全性 目前,注销功能似乎不起作用。我有一个GET版本(简单链接)和一个POST版本(链接提交的隐藏表单) 登录后,无论我使用哪种方法注销,一旦我再次尝试登录,新的登录是不允许的 我认为这与本节有关: .sessionManagement() .maximumSessions(1) .maxSessionsPreventsLogin(true) .expiredUrl("/login?expired") .logout() .log

我的增强型宠物诊所应用程序需要安全性

目前,注销功能似乎不起作用。我有一个GET版本(简单链接)和一个POST版本(链接提交的隐藏表单)

登录后,无论我使用哪种方法注销,一旦我再次尝试登录,新的登录是不允许的

我认为这与本节有关:

.sessionManagement()
    .maximumSessions(1)
    .maxSessionsPreventsLogin(true)
    .expiredUrl("/login?expired")
.logout()
    .logoutRequestMatcher(new AntPathRequestMatcher("/logout"))
    .logoutSuccessUrl("/")
    .permitAll()
但我认为这一部分:

.sessionManagement()
    .maximumSessions(1)
    .maxSessionsPreventsLogin(true)
    .expiredUrl("/login?expired")
.logout()
    .logoutRequestMatcher(new AntPathRequestMatcher("/logout"))
    .logoutSuccessUrl("/")
    .permitAll()
将使我的
HttpSession
无效,以便允许下次登录,但这不会发生

当我查看日志时,这些是我第二次登录时不同的行:

s.CompositeSessionAuthenticationStrategy : Delegating to org.springframework.security.web.authentication.session.ConcurrentSessionControlAuthenticationStrategy@2cc9f3de
w.a.UsernamePasswordAuthenticationFilter : Authentication request failed: org.springframework.security.web.authentication.session.SessionAuthenticationException: Maximum sessions of 1 for this principal exceeded
w.a.UsernamePasswordAuthenticationFilter : Updated SecurityContextHolder to contain null Authentication
w.a.UsernamePasswordAuthenticationFilter : Delegating to authentication failure handler org.springframework.security.web.authentication.SimpleUrlAuthenticationFailureHandler@16c670c3
.a.SimpleRulAuthenticationFailureHandler:重定向到/登录?错误

欢迎提供任何建议

我的申请可以在以下网址找到

下面是我的
websecurityConfigureAdapter
子类中的代码:

private static final String[] UNSECURED_RESOURCE_LIST =
    new String[] {"/", "/resources/**", "/assets/**", "/css/**", "/webjars/**",
        "/images/**", "/dandelion-assets/**", "/unauthorized", "/error*"};

@Override
public void configure(WebSecurity web) throws Exception {
    web
        .ignoring()
            .antMatchers(UNSECURED_RESOURCE_LIST);
}

@Override
protected void configure(HttpSecurity http) throws Exception {
    //@formatter:off
    http
        .authorizeRequests()
            .antMatchers(UNSECURED_RESOURCE_LIST)
                .permitAll()
            .antMatchers("/owners/**", "/vets/**", "/vets*").hasRole("USER")
            .antMatchers("/manage/**").hasRole("ADMIN")
            .anyRequest()
                .permitAll()
        .and()
            .formLogin()
                .loginPage("/login")
                    .failureUrl("/login?error")
                    .permitAll()
        .and()
            .logout()
                .logoutRequestMatcher(new AntPathRequestMatcher("/logout"))
                .logoutSuccessUrl("/")
                .permitAll()
        .and()
            .requiresChannel()
                .antMatchers("/login", "/owners/**", "/vets/**", "/vets*", "/manage/**")
                    .requiresSecure()
        .and()
            .exceptionHandling()
                .accessDeniedPage("/router?q=unauthorized")
        .and()
            .sessionManagement()
                .maximumSessions(1)
                .maxSessionsPreventsLogin(true)
                .expiredUrl("/login?expired")
        ;
    //@formatter:on
}

我在spring boot上也遇到了同样的问题,我通过实现HttpSessionEventPublisher解决了这个问题

// Register HttpSessionEventPublisher
    @Bean
    public static ServletListenerRegistrationBean httpSessionEventPublisher() {
        return new ServletListenerRegistrationBean(new HttpSessionEventPublisher());
    }

你确定你的注销被调用了吗。您同时设置了
logoutUrl
logoutRequestMatcher
,这可能会造成干扰。我会删除后者。此外,您的配置(
invalidateHttpSession
deleteCookies
基本上是默认配置。还要确保使用相同的协议(https)要调用注销,http和https通常不共享会话…谢谢。我对代码进行了适当的更改并更新了帖子。再次感谢您提供的见解。您注册了吗?这是触发会话清理的组件(另请参见。当我为
logoutUrl
切换
logoutRequestMatcher
时,我建议使用
logoutUrl(“/logout”)
而不是
logoutRequestMatcher
,在日志中返回一个404与
请求“GET/logout”不匹配的POST/logout
。似乎
logoutUrl()
只定义了一个POST URL。关于
HttpSessionEventPublisher
,我用Spring Boot尝试了很多不同的方法,但仍然无法让它编译或表现出任何不同。将
HttpSessionEventPublisher
添加到SpringBoot应用程序的方法是什么?我目前没有扩展
SpringBootServletInitializer
。该应用程序是一个可执行的JAR,但最终可能是。@M.Deinum,除了我之前的评论外,我在Spring Boot提供的任何示例中都没有发现这一点。
HttpSessionEventPublisher
在所有示例中都缺失。关键是我正在尝试使用JavaConfig和Spring Boot来实现这一点,没有XML。