具有LDAP注销功能的Spring Security无法删除会话

时间:2018-03-19 03:14:01

标签: java spring spring-security ldap logout

我使用的是spring-boot-starter-security' version:' 2.0.0用于保护REST API。 另外,我登录LDAP服务器。我的登录工作正常,当我将端点与身份验证配置放在一起时,浏览器会询问用户/通过。 问题是当我执行注销时,我可以重新进入需要身份验证的端点,我的意思是,如果请求是安全端点,则需要用户/传递。如果我登录正常,则API会显示端点的结果,但是如果我执行注销,则注销会显示成功并且我再次请求安全端点,但结果显示没有登录。 此外,我配置了2个最大会话,当我执行两次注销时,我无法再次登录,因为我收到了最大会话错误。 所以在某种程度上,我的注销效果不佳。

这是我的代码:

@Configuration 

@EnableWebSecurity 公共类SecurityConfig扩展了WebSecurityConfigurerAdapter {

@覆盖     protected void configure(HttpSecurity http)抛出异常{

    http.
            httpBasic()
            .and().authorizeRequests()
            .antMatchers("/about/**","/hello/**","/logout/**","/logout-success","/login/**").permitAll() //Allow to all to this url
            .anyRequest().fullyAuthenticated()
            .and()
            .requestCache()
            .requestCache(new NullRequestCache())
            .and()
            .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.NEVER)
            .maximumSessions(2).expiredUrl("/login").maxSessionsPreventsLogin(true)
            .and()
    ;

    http.csrf().disable();
    http.headers().frameOptions().disable();

    //logout
    http.logout().deleteCookies("JSESSIONID").invalidateHttpSession(true).logoutUrl("/logout").logoutSuccessUrl("/logout-success");

}

@Override
public void configure(AuthenticationManagerBuilder auth) throws Exception {


    String ldapURL = "ldaps://xxxx";

    auth
            .ldapAuthentication()
            .userSearchFilter("xxx")
            .userSearchBase("xxxx")
            .groupSearchBase("xxxx")
            // .groupSearchFilter("member={0}")
            .contextSource()
            .url(ldapURL)
            .port(xxx)
            .managerDn("xxxx")
            .managerPassword("xxxx")
    ;

}

1 个答案:

答案 0 :(得分:0)

我发现了不同的理论,最常见的是那个说你不能用“基本”配置执行有效注销的理论:

http.httpBasic() 

因此,解决方案是使用表单页面提供身份验证或Spring提供的.formLogin()