Spring security with AngularJS - 404 on logout

前端 未结 3 1565
梦如初夏
梦如初夏 2020-12-19 10:08

I\'m working with tutorial that describes how to write simple single-page app using Spring Boot, Spring Security and AngularJS: https://spring.io/guides/tutorials/spring-sec

3条回答
  •  醉话见心
    2020-12-19 10:43

    In fact what you need is just to add a logout success handler

    @Component
    public class LogoutSuccess 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();
                // you can add more codes here when the user successfully logs
                // out,
                // such as updating the database for last active.
            } catch (Exception e) {
                e.printStackTrace();
                e = null;
            }
        }
    
        httpServletResponse.setStatus(HttpServletResponse.SC_OK);
    
    }
    
    }
    

    and add a success handler to your security config

    http.authorizeRequests().anyRequest().authenticated().and().logout().logoutSuccessHandler(logoutSuccess).deleteCookies("JSESSIONID").invalidateHttpSession(false).permitAll();
    

提交回复
热议问题