SpringSecurity(九)退出管理
阿新 • • 發佈:2018-11-19
實現退出
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