Struts2配置拦截器

本文详细介绍了如何在Java应用中实现登录拦截和权限检查,确保只有经过验证的用户才能访问特定页面。通过定制的拦截器,实现了针对不同操作方法的权限控制,有效提升系统的安全性。
=============================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>
基于遗传算法的新的异构分布式系统任务调度算法研究(Matlab代码实现)内容概要:本文档围绕基于遗传算法的异构分布式系统任务调度算法展开研究,重点介绍了一种结合遗传算法的新颖优化方法,并通过Matlab代码实现验证其在复杂调度问题中的有效性。文中还涵盖了多种智能优化算法在生产调度、经济调度、车间调度、无人机路径规划、微电网优化等领域的应用案例,展示了从理论建模到仿真实现的完整流程。此外,文档系统梳理了智能优化、机器学习、路径规划、电力系统管理等多个科研方向的技术体系与实际应用场景,强调“借力”工具与创新思维在科研中的重要性。; 适合人群:具备一定Matlab编程基础,从事智能优化、自动化、电力系统、控制工程等相关领域研究的研究生及科研人员,尤其适合正在开展调度优化、路径规划或算法改进类课题的研究者; 使用场景及目标:①学习遗传算法及其他智能优化算法(如粒子群、蜣螂优化、NSGA等)在任务调度中的设计与实现;②掌握Matlab/Simulink在科研仿真中的综合应用;③获取多领域(如微电网、无人机、车间调度)的算法复现与创新思路; 阅读建议:建议按目录顺序系统浏览,重点关注算法原理与代码实现的对应关系,结合提供的网盘资源下载完整代码进行调试与复现,同时注重从已有案例中提炼可迁移的科研方法与创新路径。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值