SpringSecurity(九)退出管理

实现退出


SpringSecurityConfig

.and()
    .logout()
    .logoutUrl("/user/logout")
     // .logoutSuccessUrl("") // 设置退出成功后跳转的页面
    .logoutSuccessHandler(new LzcLogoutSuccessHandler()) // 设置退出处理器
.and()
LzcLogoutSuccessHandler
@Slf4j
public class LzcLogoutSuccessHandler implements LogoutSuccessHandler {
    private ObjectMapper objectMapper = new ObjectMapper();
    @Override
    public void onLogoutSuccess(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Authentication authentication) throws IOException, ServletException {
        log.info("退出登录");
        // 根据需要返回html页面或者json


        httpServletResponse.sendRedirect("/login/index");


//        Map<String, Object> map = new HashMap<>();
//        map.put("code", 0);
//        map.put("msg", "退出成功");
//        httpServletResponse.setContentType("application/json;charset=UTF-8");
//        httpServletResponse.getWriter().write(objectMapper.writeValueAsString(map));
    }
}

登录按钮

<form th:action="@{/user/logout}" method="post">
     <button type="submit" class="button btn-primary">退出登录</button>
</form>

代码地址   https://github.com/923226145/SpringSecurity/tree/master/chapter9  

猜你喜欢

转载自blog.csdn.net/lizc_lizc/article/details/84137087
今日推荐