webwork簡單的權限攔截器

在這裏,我們通過一個簡單的例子來演示如何用Interceptor來驗證權限。
我們假設你的程序中已經寫好了每個用戶的權限,如A用戶的權限是InsertUser.action,,,,或者更多,一個權限字符串。
在xwork.xml配置Interceptor,配置InsertUser.action.
OK !
下面是驗證步驟:
1.用戶訪問InsertUser.action,觸發Interceptor
2.在Interceptor中我們需要獲取HttpSession來取得當前用戶,查詢當前用戶具有的權限String.將它轉換爲String[],
3.獲取當前用戶訪問的url :通過HttpServletRequest來獲取url ,再處理以後的url爲xxx.action.
4.判斷該url是否與當前用戶權限String[]中某一個String 一致。
5.一致 就繼續執行該Action,否則就return "error";

好 ,下面是Interceptor的代碼
[code]
public class SecurityInterceptor implements Interceptor {


private static final long serialVersionUID = 1L;

private Logger log = Logger.getLogger(this.getClass());

public void destroy() {
log.info(">>>>>>>>>>>>>>>>>>>>>>>結束攔截器!!!!<<<<<<<<<<<<<<<<<<<<<");
}

public void init() {
log.info(">>>>>>>>>>>>>>>>>>>>>>>開始初始化攔截器!!!!<<<<<<<<<<<<<<<<<<<<<");
}

public String intercept(ActionInvocation invocation) throws Exception {
String strFullPath = ServletActionContext.getServletContext()
.getRealPath("/");
strFullPath = strFullPath + "WEB-INF//applicationContext.xml";
ApplicationContext ac = new FileSystemXmlApplicationContext(strFullPath);

SecurityDAOImpl security = (SecurityDAOImpl) ac.getBean("securityDAO");
// Bean容器中無法配置Interceptor,所以SecurityDAOImpl中無法獲得SessionFactory 必須讀取配置文件
HttpServletRequest request = ServletActionContext.getRequest();
HttpSession session = request.getSession();
UserInfo user = (UserInfo) session.getAttribute("UserLoginInfo");// 獲取session中存放的用戶信息
boolean flag = false;
if (user != null) {
log
.info(">>>>>>>>>>>>>>>>>>>>>>>當前用戶" + user.getUserName()
+ " 進入" + invocation.getAction()
+ ",開始操作!!!!!<<<<<<<<<<<<<<<<<<<<");
log
.info(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>判斷當前用戶權限開始<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<!!!!!");

String url = "";
url = Constant.ChangeUrl.getUrl(request.getRequestURL().toString());
List list = null;
if (url != null) {
list = security.getSecurityByUser(user);// 獲取用戶權限 如1,2,3,4
if (list != null) {
if (list.size() > 0 && list.get(0) != null) {
String[] str = ((String) list.get(0)).split(";");// 獲得用戶權限數組爲數字字符串
String[] strTemp = null;
for (int i = 0; i < str.length; i++) {
if (str[i] != null && str[i] != "") {
strTemp = Constant.SecurityList
.getNameBySecurity(
Integer.parseInt(str[i]))
.split(",");// 這裏是分割每個權限字符串
if (strTemp != null) {
for (int j = 0; j < strTemp.length; j++) {
//就只有這1句是來驗證url是否相等
if (url.equalsIgnoreCase(strTemp[j])) {
flag = true;
break;
}
}
}

}
}
}
}
}
} else {
log.info("您尚未登錄或者登錄失效!,請重新登錄");
request.setAttribute("noLogin", "noLogin");
}
if (flag) {
log
.info(">>>>>>>>>>>>>>>>>>驗證成功,你有訪問權限!!!!開始操作!!!<<<<<<<<<<<<<<<<<<<<<");
return invocation.invoke();

} else {
log.info(">>>>>>>>>>>>>>>>>>您沒有訪問權限!!<<<<<<<<<<<<<<<<<<<<<");
return "error";
}

}

}
[/code]

中間部分都是查詢數據庫中用戶權限,再進行分割。只有最後2句是最重要的
return invocation.invoke(); 這裏指繼續執行action
return "error"; 直接跳轉到錯誤頁面。

OK! 現在我們來看看 xwork.xml的配置。

[code]
<xwork>
<include file="webwork-default.xml"></include>
<package name="default" extends="webwork-default">


<interceptors>
<interceptor name="params"
class="com.opensymphony.xwork.interceptor.ParametersInterceptor" />
<interceptor name="SecurityInterceptor"
class="com.xxx.xxx.SecurityInterceptor" />
<interceptor name="logger"
class="com.opensymphony.xwork.interceptor.LoggingInterceptor" />
<interceptor-stack name="testStack">
<interceptor-ref name="SecurityInterceptor" />
<interceptor-ref name="params" />
<interceptor-ref name="logger" />
</interceptor-stack>

</interceptors>
<global-results>
<result name="login" type="dispatcher">
<param name="location">/login.jsp</param>
</result>
<result name="error" type="dispatcher">
<param name="location">/error.jsp</param>
</result>
</global-results>
<action name="InsertUser.action" class="xxx"
method="xxx">
<result name="success">/user/addSecurityGroup.jsp</result>
<interceptor-ref name="testStack"></interceptor-ref>
</action>
[/code]

這裏也用了webwork自帶的Interceptor ,其中params是必須的,由於採用了Interceptor,可能你提交給Action的參數也攔截掉了,無法獲得到參數,而params可以將參數帶給Action.
logger是做日誌的,在Action進入之前和結束以後均會寫一條日誌,如:before xxx action , after xxx action.
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章