=============================java==============
import java.util.Map;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;
import com.zr.pojo.aged.home.ZaghUsers;
public class UtilInterceptor implements Interceptor {
/**
* 实现登录拦截
*/
private static final long serialVersionUID = -532172353763647921L;
public void destroy() {
// TODO Auto-generated method stub
}
public void init() {
// TODO Auto-generated method stub
}
@SuppressWarnings("unchecked")
public String intercept(ActionInvocation invocation) throws Exception {
String methodName=null;
Map map = invocation.getInvocationContext().getParameters();
if (map == null || map.size() == 0) {
System.out.println("0");
} else {
String[] keys = new String[map.size()];
map.keySet().toArray(keys);
for (int i = 0; i < keys.length; i++) {
if (map.get(keys[i]) != null) {
String[] values = (String[])map.get(keys[i]);
for (int j = 0; j < values.length; j++) {
methodName=values[0];
}
}
}
}
ActionContext ctx = invocation.getInvocationContext();
Map session = ctx.getSession();
// 取出名为user的session属性
ZaghUsers user = (ZaghUsers) session.get("userbean");
// 如果没有登陆,返回重新登陆
if (user != null||(methodName!=null&&methodName.equalsIgnoreCase("User.checkUserLogin"))||(methodName!=null&&methodName.equalsIgnoreCase("Home.index"))) {
return invocation.invoke();
} else {
return "/index.jsp";
}
//return invocation.invoke();
}
// @Override
// protected String doIntercept(ActionInvocation invocation) throws Exception {
// ActionProxy proxy=invocation.getProxy();
// String methodName=proxy.getMethod();
// Method method=null;
// method = proxy.getAction().getClass().getMethod(methodName, new Class[0]);
//// String methodName=proxy.getClass().getMethod(name, parameterTypes);
// // 取得请求相关的ActionContext实例
// ActionContext ctx = invocation.getInvocationContext();
// Map session = ctx.getSession();
// // 取出名为user的session属性
// ZaghUsers user = (ZaghUsers) session.get("userbean");
// // 如果没有登陆,返回重新登陆
// if (user != null||methodName.equalsIgnoreCase("checkUserLogin")) {
// return invocation.invoke();
// } else {
// return "/tpl/html//home/Home/index.jsp";
// }
// }
}
=====================struts.xml==============================
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<!--*********************struts2 constant************************ -->
<constant name="struts.i18n.encoding" value="UTF-8"/>
<constant name="struts.custom.i18n.resources" value="messages"/>
<constant name= "struts.multipart.maxSize" value="52428800" />
<package name="default" extends="struts-default" >
<result-types>
<result-type name="direct" class="com.gctx.framework.struts2.dispatcher.DirectResultDispatcher"/>
</result-types>
<!-- 定义一个名为 authority的拦截器 -->
<interceptors>
<!-- 定义权限检查拦截器 -->
<interceptor name="requestAction"
class="com.gctx.web.action.home.UtilInterceptor" />
<!-- 定义一个包含权限检查的拦截器栈 -->
<interceptor-stack name="myDefaultStack">
<!-- 定义拦截器栈包含authority拦截器 需要过滤的方法 index 要加在此 -->
<interceptor-ref name="requestAction">
<param name="excludeMethods">*.index,*.checkUserLogin</param>
</interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
</interceptor-stack>
</interceptors>
<!-- 设置全局默认的拦截器栈-->
<default-interceptor-ref name="myDefaultStack" />
</package>
<package name="all" extends="default">
<result-types>
<result-type name="direct" class="com.gctx.framework.struts2.dispatcher.DirectResultDispatcher"/>
</result-types>
<action name="*" class="com.gctx.web.action.GenericBaseAction" >
<result name="*" type="direct">direct</result>
</action>
</package>
</struts>
分享到:
相关推荐
使用Struts2实现用户权限拦截、重点是掌握拦截器的使用和配置方法,通过案例的学习,就能掌握struts中拦截器的使用方法,让我们能快速上手
struts2,Interceptor struts2拦截器实例,两套实例,一套是针对单个的action配置的,另一套实例是针对全局的action配置的拦截器interceptor
strut2 配置自定义拦截器 包括struts2.xml 和 action对应的xml 以及拦截器 可能有点简单 但是基本配置都有
NULL 博文链接:https://chaoyi.iteye.com/blog/2158316
Struts2的拦截器的配置 Struts2的拦截器举例
该文件主要针对springMVC+spring+mybatis框架配置,struts2文件上传的代码,Struts2自定义拦截器配置
struts2拦截器的使用说明文档,配置说明文档。可以看一下
Struts2拦截器的实现原理相对简单,当请求struts2的action时,Struts 2会查找配置文件,并根据其配置实例化相对的 拦截器对象,然后串成一个列表,最后一个一个地调用列表中的拦截器 三、定义Struts2拦截器。 ...
利用登陆拦截器 源码 演示struts2自定义拦截器的使用和配置。
Struts2_拦截器详细配置过程.pdf
struts2拦截器拦截器的概念 配置拦截器 使用拦截器
解说Struts2拦截器的配置
struts2上传单个文件及多个文件,strus2拦截器的使用,里面有个user用户,先执行user.jsp即登陆,可以执行上传文件的操作,反之,不能,有个exit...主要文件PremissionInterceptor.java和struts2.xml拦截器配置文件,ok
通过web配置拦截器进行struts2漏洞拦截源码及配置
Struts2拦截器及其用法详细说明,描述了 各个拦截器配置对应的功能
struts2注解配置Action及拦截器几种不同方式写法对应的路径指向.docx
主要是有关Struts2中拦截器的配置和解析,简单的应用。
Struts2 拦截器详细配置过程 1:所有拦截器的超级接口Interceptor ,Action去实现这个接口; Interceptor 它其中有三个方法(init(),destroy() ,interceptor()): Init()方法:在服务器起动的时候加载一次,并且只加载一...
在struts2框架中主配置文件struts-defalut.xml 文件中定义了大量的拦截器和拦截器栈。通过 default-interceptor-ref元素定义了 当前应用的默认拦截器栈,对用户的每次请求都需要调用拦截器栈中的每个拦截器来拦截...