大多數(shù)網(wǎng)站會設(shè)置用戶權(quán)限,如過濾非法用戶,用戶不登錄時不能進行訪問,或者設(shè)置訪問的權(quán)限,如部分內(nèi)容僅對VIP開放等等,這些權(quán)限的控制都可以用struts2中的攔截器來實現(xiàn)。
下面通過一個簡單的Demo來模擬這種用戶權(quán)限控制的實現(xiàn)流程,設(shè)定三種不同身份的用戶,commen為普通用戶,VIP為會員用戶,還有一種admin為管理員。
先看一下Demo的整體結(jié)構(gòu):
首先搭建struts2框架的開發(fā)環(huán)境(前面博客中有介紹),環(huán)境搭建完之后又再看一看如何配置struts.xml:
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
29
30
31
32
33
34
35
|
<? xml version = "1.0" encoding = "UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> < struts > < package name = "hello" extends = "struts-default" namespace = "/" > < interceptors > < interceptor name = "testInterceptor" class = "org.interceptor.InterceptorTest" ></ interceptor > <!-- 一個攔截器棧中可以定義多個攔截器 --> < interceptor-stack name = "testStack" > < interceptor-ref name = "testInterceptor" /> < interceptor-ref name = "defaultStack" /> </ interceptor-stack > </ interceptors > <!--全局結(jié)果處理 --> < global-results > < result name = "error" >/Error.jsp</ result > </ global-results > < action name = "login" class = "org.interceptor.LoginAction" > < result >/WEB-INF/pages/index.jsp</ result > </ action > < action name = "admin" class = "org.interceptor.LoginAction" method = "AdminExecute" > < interceptor-ref name = "testStack" ></ interceptor-ref > < result >/WEB-INF/pages/admin.jsp</ result > </ action > < action name = "vip" class = "org.interceptor.LoginAction" method = "vipExecute" > < interceptor-ref name = "testStack" ></ interceptor-ref > < result >/WEB-INF/pages/vipUser.jsp</ result > </ action > < action name = "commen" class = "org.interceptor.LoginAction" method = "commenExecute" > < interceptor-ref name = "testStack" ></ interceptor-ref > < result >/WEB-INF/pages/commen.jsp</ result > </ action > </ package > </ struts > |
其中,<global-results></global-results>是全局的result,有很多時候一個<result>可供很多<action>使用,這時可以使用<global-results>標簽來定義全局的<result>。執(zhí)行順序:當一個Action返回的String沒有相應(yīng)的<result>與之對應(yīng),Struts2就會查找全局的<result>,所以本次模擬測試中不符合條件被攔截的請求都會轉(zhuǎn)到error.jsp。
Action類,不做處理,全部放行,讓攔截器處理:
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
|
public class LoginAction implements SessionAware{ @SuppressWarnings ( "unused" ) private String username; private Map<String,Object> session; public void setUsername(String username) { this .username = username; session.put( "username" , username); } public void setSession(Map<String, Object> session) { // TODO Auto-generated method stub this .session = session; } public String AdminExecute(){ return "success" ; } public String vipExecute(){ return "success" ; } public String commenExecute(){ return "success" ; } public String execute(){ return "success" ; } } |
Inteceptor(攔截器類):
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
|
public class LoginAction implements SessionAware{ @SuppressWarnings ( "unused" ) private String username; private Map<String,Object> session; public void setUsername(String username) { this .username = username; session.put( "username" , username); } public void setSession(Map<String, Object> session) { // TODO Auto-generated method stub this .session = session; } public String AdminExecute(){ return "success" ; } public String vipExecute(){ return "success" ; } public String commenExecute(){ return "success" ; } public String execute(){ return "success" ; } } |
只是 模擬攔截器的實現(xiàn)思路,沒有持久層的數(shù)據(jù),這里的方法是使用invocation.getProxy().getActionName()方法來獲取struts.xml中配置的action名稱,和用戶表單提交的名稱做對比,如果輸入的用戶名是以action名開頭的,就放行,否則攔截。
登錄jsp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
<%@ page language= "java" import = "java.util.*" pageEncoding= "UTF-8" %> <% String path = request.getContextPath(); String basePath = request.getScheme()+ "://" +request.getServerName()+ ":" +request.getServerPort()+path+ "/" ; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" > <html> <head> <base href= "<%=basePath%>" rel= "external nofollow" rel= "external nofollow" > <title>login</title> </head> <body> <form action= "login.action" > <input type= "text" name= "username" /> <input type= "password" name= "password" /> <input type= "submit" value= "login" > </form> </body> </html> |
攔截后跳轉(zhuǎn)頁:
1
2
3
|
< body > < h4 >你的權(quán)限不足,請先升級權(quán)限...</ h4 > </ body > |
訪問資源代碼:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
<%@ page language= "java" import = "java.util.*" pageEncoding= "UTF-8" %> <% String path = request.getContextPath(); String basePath = request.getScheme()+ "://" +request.getServerName()+ ":" +request.getServerPort()+path+ "/" ; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" > <html> <head> <base href= "<%=basePath%>" rel= "external nofollow" rel= "external nofollow" > <title>index</title> </head> <body> <a href= "admin.action" rel= "external nofollow" >admin</a><br/> <a href= "vip.action" rel= "external nofollow" >vip</a><br/> <a href= "commen.action" rel= "external nofollow" >commen</a> </body> </html> |
其余admin.jsp等界面沒有內(nèi)容,只是為了區(qū)分實現(xiàn)跳轉(zhuǎn)頁面不同。
運行結(jié)果:
使用commen角色登錄:
點擊VIP以及admin跳轉(zhuǎn)鏈接時:
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習有所幫助,也希望大家多多支持服務(wù)器之家。
原文鏈接:http://blog.csdn.net/weixin_36380516/article/details/71429800?utm_source=tuicool&utm_medium=referral