在項目中只有用戶登陸後才能訪問整個網站,即使你知道了網址比如(假設的https://blog.51cto.com/userupdate?id=5),也是進不去的,這時候就需要攔截器。
怎麼實現攔截器呢,原理去網站找,我把我的代碼貼出來。以供參考。
第一步:寫一個攔截器,繼承AbstractInterceptor,重寫public String intercept(ActionInvocation invocation)方法
。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
package com.shuiwu.interceptor;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
import com.shuiwu.entity.User;
/**
* 攔截器
* @author niuhailiang
*
*/
public class CheckInterceptor extends AbstractInterceptor {
@Override
public String intercept(ActionInvocation invocation) throws Exception {
//獲取當前的用戶
User user=(User) ActionContext.getContext().getSession().get("user");
//獲取當前訪問的URL,並去掉當前應用程序的前綴(也就是namespace+actionName)
String namespace=invocation.getProxy().getNamespace();
String actionName=invocation.getProxy().getActionName();
System.out.println(namespace);
System.out.println(actionName);
String url=null;
//
if(namespace.endsWith("/")){
url=namespace+actionName;
}else{
url=namespace+"/"+actionName;
}
//如果未登陸的
if(user==null){
if(url.startsWith("/userAction_login")){
//如果正在使用登陸功能則放行
return invocation.invoke();
}
else{
return "loginUI";
}
}else{
return invocation.invoke();
}
}
}
第二部:很重要,註冊攔截器,在struts.xml中配置攔截器,代碼如下:
。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
<?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>
<!-- 配置爲開發模式 -->
<constant name="struts.devMode" value="true" />
<constant name="struts.action.extension" value="action,do," />
<constant name="struts.configuration.xml.reload" value="true"></constant>
<constant name="struts.multipart.maxSize" value="10485740"></constant>
<constant name="struts.custom.i18n.resources" value="Message"></constant>
<constant name="struts.ui.theme" value="simple"></constant>
<package name="default" namespace="/" extends="struts-default">
<interceptors>
<interceptor name="check"
class="com.shuiwu.interceptor.CheckInterceptor"></interceptor>
<interceptor-stack name="mystack">
<interceptor-ref name="check"></interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
</interceptor-stack>
</interceptors>
<default-interceptor-ref name="mystack"></default-interceptor-ref>
<global-results>
<!-- 跳轉到登錄頁面的result -->
<result name="loginUI">
/WEB-INF/jsp/loginUI.jsp
</result>
</global-results>
<!-- 用戶管理 -->
<action name="userAction_*" class="userAction" method="{1}">
<result name="loginUI">/WEB-INF/jsp/loginUI.jsp</result>
<result name="index">toIndex.jsp</result>
<result name="changePasswordUI">/WEB-INF/jsp/changePassword.jsp</result>
</action>
<!-- 稅率管理 -->
<action name="taxRateAction_*" class="taxRateAction" method="{1}">
<result name="list">/WEB-INF/jsp/rate/list.jsp</result>
<result name="updateUI">/WEB-INF/jsp/rate/updateUI.jsp</result>
<result name="toList" type="redirectAction">taxRateAction_list.action</result>
</action>
<!-- 保險管理 -->
<action name="insuranceAction_*" class="insuranceAction" method="{1}">
<result name="list">/WEB-INF/jsp/insurance/list.jsp</result>
<result name="return">toIndex.jsp</result>
<result name="print">/WEB-INF/jsp/tax/print.jsp</result>
<result name="findByDateUI">/WEB-INF/jsp/insurance/findByDateUI.jsp</result>
<result name="findByDate">/WEB-INF/jsp/insurance/findByDateUI.jsp</result>
<result name="printAll">/WEB-INF/jsp/insurance/printAll.jsp</result>
</action>
<!-- 稅務管理 -->
<action name="taxAction_*" class="taxAction" method="{1}">
<result name="list">/WEB-INF/jsp/tax/list.jsp</result>
<result name="print">/WEB-INF/jsp/tax/print.jsp</result>
<result name="return" >toIndex.jsp</result>
<result name="findByDateUI">/WEB-INF/jsp/tax/findByDateUI.jsp</result>
<result name="findByDate">/WEB-INF/jsp/tax/findByDateUI.jsp</result>
<result name="printAll">/WEB-INF/jsp/tax/printAll.jsp</result>
<result name="redirect" type="redirectAction">taxAction_findByDateUI</result>
</action>
</package>
</struts>
。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
這樣就完成了。以後訪問必須有用戶登陸驗證後纔可以