上一篇介紹了使用springmvc集成shiro登陸過程,通過FormAuthenticationFilter過濾器獲取到用戶輸入的賬號密碼。
shiro是一個被廣泛使用的安全層框架,通過xml配置方式與spring無縫對接,用戶的登陸/退出/權限控制/Cookie等管理系統基礎功能交給shiro來管理。
一般,在JavaWEB管理平臺系統時,用戶退出系統之前沒需要清除用戶數據和關閉連接,防止垃圾數據堆積,shiro提供了LogoutFilter過濾器,我們可以繼承LogoutFilter,重寫preHandle方法,實現清除緩存功能。
spring-shiro.xml:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
<!-- 安全認證過濾器 --> < bean id = "shiroFilter" class = "org.apache.shiro.spring.web.ShiroFilterFactoryBean" > < property name = "securityManager" ref = "securityManager" /> < property name = "loginUrl" value = "/b/login" /> < property name = "successUrl" value = "/b" /> < property name = "filters" > < map > <!--退出過濾器--> < entry key = "logout" value-ref = "systemLogoutFilter" /> </ map > </ property > < property name = "filterChainDefinitions" > < value > /b/login = authc /b/logout = logout /b/** = user </ value > </ property > </ bean > |
當調用的路徑匹配到/b/logout,會進入到SystemLogoutFilter過濾器,SystemLogoutFilter繼承了LogoutFilter,并重寫了preHandle方法,在preHandle方法執行需要清空的數據。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
@Service public class SystemLogoutFilter extends LogoutFilter { @Override protected boolean preHandle(ServletRequest request, ServletResponse response) throws Exception { //在這里執行退出系統前需要清空的數據 Subject subject = getSubject(request, response); String redirectUrl = getRedirectUrl(request, response, subject); try { subject.logout(); } catch (SessionException ise) { ise.printStackTrace(); } issueRedirect(request, response, redirectUrl); //返回false表示不執行后續的過濾器,直接返回跳轉到登錄頁面 return false ; } } |
注意,需要通過@Service注解,使用spring容器來管理,在spring-shiro.xml中配置shiro過濾器直接使用
1
|
< entry key = "logout" value-ref = "systemLogoutFilter" /> |
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:http://www.cnblogs.com/nosqlcoco/p/5587294.html