【问题标题】:Why spring security logout does not work?为什么弹簧安全注销不起作用?
【发布时间】:2019-08-12 22:34:46
【问题描述】:

我的spring security有问题,我尝试在spring security中注销,但似乎不起作用。我请求注销url,但会话和身份验证不明确。

这是一个 spring cloud 应用程序,运行 spring cloud Finchley.RELEASE。使用 zuul、spring security 和 oauth2。

@Override
protected void configure(HttpSecurity http) throws Exception {
    http
            .authorizeRequests()
            .antMatchers("/login","/login.html").permitAll()
            .anyRequest().authenticated()
            .and()
            .formLogin()
            .loginPage("/login")
            .successHandler(loginSuccessHandler)
            .failureHandler(loginFailHandler)
            .permitAll()
            .and()
            .logout()
            .logoutRequestMatcher(new AntPathRequestMatcher("/logout"))
            .logoutSuccessHandler(logoutHandler)
            .clearAuthentication(true)
            .deleteCookies("JSESSIONID")
            .invalidateHttpSession(true);

    http .cors().and().csrf().disable();

}

我希望在请求注销 url 后,身份验证和会话无效

【问题讨论】:

    标签: java spring-security spring-security-oauth2


    【解决方案1】:

    在您的 logoutHandler 中使用以下代码。

    @Service
    @Scope(scopeName = BeanDefinition.SCOPE_SINGLETON)
    @Transactional(readOnly=false)
    public class CustomLogoutSuccessHandler implements LogoutSuccessHandler{
    
        @Override
        public void onLogoutSuccess(HttpServletRequest httpServletRequest,
                HttpServletResponse httpServletResponse, Authentication authentication)
                throws IOException, ServletException {
            if (authentication != null && authentication.getDetails() != null) {
                try {
                    httpServletRequest.getSession().invalidate();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
    
            httpServletResponse.setStatus(HttpServletResponse.SC_OK);
            httpServletResponse.sendRedirect("/");
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2012-10-05
      • 2016-11-22
      • 2012-08-04
      • 2011-06-22
      • 1970-01-01
      • 2019-04-27
      • 2015-06-06
      相关资源
      最近更新 更多