struts 用拦截器进行用户权限隔离,未登录用户跳到登录界面 *** 最爱那水货

时间:2023-11-22 16:06:20

一般,我们的web应用都是只有在用户登录之后才允许操作的,也就是说我们不允许非登录认证的用户直接访问某些页面或功能菜单项。对于个别页面来说,可能不需要进行拦截,此时,如果项目采用struts view框架来做的话,我们可以用strut的拦截器做一个intercepter类。

首先在项目中独立一个包出来

struts 用拦截器进行用户权限隔离,未登录用户跳到登录界面  *** 最爱那水货

  我们写一个拦截器类,让它继承 MethodFilterInterceptor。

 /**
* 登陆拦截器
* @author Administrator
*
*/
public class LoginIntercepter extends MethodFilterInterceptor{ @Override
protected String doIntercept(ActionInvocation invoke) throws Exception {
Map session = invoke.getInvocationContext().getSession(); //從上下文中獲取session會話,結果為MAP集合,裏面是以json格式保存的相關信息
Object user = session.get("user"); //獲取session會話中的會員名稱
if(user != null){
return invoke.invoke(); //對該操作不攔截
}else{
invoke.getInvocationContext().put("tip","您还没有登录,请登陆系统");
return "loginUI"; //返回登陸界面
}
} }

加入配置文件interceoper-struts.xml ,其中 name="intercepter" 作为独立的一个全局的package 其他的配置文件只需加入extends="intercepter"即可。尤其

      <param name="excludeMethods">xx,xxx</param> 这一代码是过滤的方法 ,即不进行拦截的方法。把对应的方法名放入即可,中间用逗号分隔。
      <param name="includeMethods">xx,xxx</param> 这行代码意思,进行拦截的方法,把对应的方法名放入即可,中间用逗号分隔。
 <?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="intercepter" extends="struts-default">
<interceptors>
<!-- 配置未登录进行操作的拦截器 -->
<interceptor name="loginInterceptor" class="com.chinawaf.intercepter.action.LoginIntercepter" />
<!-- 重新封装一个默认的拦截器栈 -->
<interceptor-stack name="myStack">
<interceptor-ref name="loginInterceptor" >
<!-- 设置方法名 ,对应的方法不做拦截 -->
<param name="excludeMethods">login,getVerriCode</param>
</interceptor-ref>
<interceptor-ref name="defaultStack" />
</interceptor-stack>
</interceptors>
<!-- 为这个包设置默认的拦截器栈 -->
<default-interceptor-ref name="myStack" /> <global-results>
24 <result name="loginUI">/WEB-INF/jsp/afterend/login.jsp</result>
25 </global-results> </package> </struts>

然后在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.enable.DynamicMethodInvocation" value="false" />
<!-- 配置成开发模式 -->
<constant name="struts.devMode" value="false" />
<!-- 配置拓展名为action -->
<constant name="struts.action.extention" value="action"/>
<!-- 把主题配置成simple -->
<constant name="struts.ui.theme" value="simple" /> <include file="com/chinawaf/intercepter/config/intercepter-struts.xml"></include>
<include file="com/chinawaf/backLogin/conf/backLogin-struts.xml"></include>
<include file="com/chinawaf/home/conf/home-struts.xml"></include>
<include file="com/chinawaf/rtmp/log/conf/rtmpLog-struts.xml"></include>
<include file="com/chinawaf/rtmp/video/conf/rtmpVideo-struts.xml"></include>
<include file="com/chinawaf/rtmp/pvw/conf/rtmpPvw-struts.xml"></include>
<include file="com/chinawaf/rtmp/waf/conf/rtmpWaf-struts.xml"></include>
<include file="com/chinawaf/login/conf/login-struts.xml"></include>
<include file="com/chinawaf/backstage/user/conf/user-struts.xml"></include>
<include file="com/chinawaf/backstage/servicePage/conf/serve-struts.xml"></include>
<include file="com/chinawaf/rtmp/video/conf/rtmpVideo-struts.xml"></include>
<include file="com/chinawaf/backstage/api/conf/api-struts.xml"></include>
<include file="com/chinawaf/rtmp/monitoring/conf/rtmpMonitoring-struts.xml"></include>
<include file="com/chinawaf/cdn/cdn/conf/cdnCdn-struts.xml"></include>
<include file="com/chinawaf/cdn/list/conf/cdnList-struts.xml"></include>
<include file="com/chinawaf/cdn/cache/conf/cdnCache-struts.xml"></include>
<include file="com/chinawaf/cdn/flow/conf/cdnFlow-struts.xml"></include>
<include file="com/chinawaf/cdn/manage/conf/cdnManage-struts.xml"></include>
<include file="com/chinawaf/cdn/waf/conf/cdnWaf-struts.xml"></include>
<include file="com/chinawaf/cdn/visit/conf/cdnVisit-struts.xml"></include>
<include file="com/chinawaf/cdn/visitor/conf/cdnVisitor-struts.xml"></include>
</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> <package name="sysLoginAction" namespace="/" extends="intercepter">
<action name="randPic" class="com.chinawaf.login.action.RandomAction" method="getVerriCode">
<result type="stream">
<param name="contentType">image/jpeg</param>
<param name="inputName">inputStream</param>
</result>
</action> <action name="login_*" class="com.chinawaf.login.action.LoginAction" method="{1}">
<result name="loginUI">/WEB-INF/jsp/client/login.jsp</result>
<result name="home" type="redirectAction">
<param name="actionName">home</param>
</result>
</action>
</package> </struts>

最后,struts 还可有其他的拦截器类,如:AbstractInterceptor  ,可以继承此类进行url地址的拦截。

关于 struts2 拦截器的详细介绍,可以参考这篇文章:http://wenku.baidu.com/link?url=GPQkMm2UkyFeRPMWH7RMTqJD1xVV2DWU90b5FwuNzDQjuAnx300jMePNb1IVgxMkM3dek3Irixm-kuTOmClbSQ8vdyEjRPwmAdXqZOlG_GK