在项目中只有用户登陆后才能访问整个网站,即使你知道了网址比如(假设的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>

。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。

这样就完成了。以后访问必须有用户登陆验证后才可以