一、使用反射动态添加权限

在该系统中,我使用struts2的时候非常规范,访问的Action的形式都是"ActionClassName_MethodName.action?参数列表",所以可以非常方便的使用反射初始化权限表。

比较关键的是获取所有Action类所在的包的方法:

URL url=ClassLoader.getSystemResource("com/kdyzm/struts/action");
File dir=new File(url.toURI());

  然后获取所有文件(有不是class后缀名的文件):

File[] fiels=dir.listFiles();

  接下来遍历文件列表,获取文件名即可获取Action名字,之后就是反射的问题了,略,详细代码:

 package com.kdyzm.init;

 import java.io.File;
import java.lang.reflect.Method;
import java.net.URL; import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.kdyzm.domain.security.Right;
import com.kdyzm.service.RightService;
public class InitRight {
public static void main(String[] args) throws Exception {
ApplicationContext context=new ClassPathXmlApplicationContext("spring/applicationContext.xml");
RightService rightService=(RightService) context.getBean("rightService"); // InitRight.class.getClassLoader();
URL url=ClassLoader.getSystemResource("com/kdyzm/struts/action");
File dir=new File(url.toURI());
File[] fiels=dir.listFiles();
for(File file:fiels){
if(file.getName().endsWith("class"))
processClass(file,rightService);
}
System.out.println("完成初始化任务!");
}
private static void processClass(File file, RightService rightService) throws Exception {
String basePackage="com.kdyzm.struts.action.";
String className=basePackage+file.getName().substring(0,file.getName().indexOf("."));
Class clazz=Class.forName(className);
Method[]methods=clazz.getDeclaredMethods();
String methodName="";
Class returnType=null;
Class[] parameters=null;
String url="";
for(Method method:methods){
methodName=method.getName();
returnType=method.getReturnType();
parameters=method.getParameterTypes();
if(returnType.equals(String.class)
&&(parameters==null||parameters.length==0)){
         url="/"+file.getName().substring(0,file.getName().indexOf("."))+"_"+methodName+".action";
Right right=new Right();
right.setRightUrl(url);
rightService.saveOrUpateRight(right);
}
}
}
}

二、使用权限拦截器动态添加权限

  这是为了方便在开发阶段使用的方法,在实际使用的时候需要将该拦截器去掉。

  这里涉及到一个技术点,即如何获取ApplicationConext对象,spring容器也在application作用域中,所以如果想要获取ApplicationContext对象,一定要获取ServletContext,通过WebApplicationContextUtils.getWebApplicationContext(ServletContext sc)方法就能够获取ApplicationContext了。

 package com.kdyzm.struts.interceptors;

 import javax.servlet.ServletContext;

 import org.apache.struts2.ServletActionContext;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils; import com.kdyzm.service.RightService;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.ActionProxy;
import com.opensymphony.xwork2.interceptor.Interceptor;
/**
* 该拦截在发布之后应当删除掉
* @author kdyzm
*
*/
public class CatchUrlInterceptor implements Interceptor{
private static final long serialVersionUID = 6747245610234756713L; @Override
public void destroy() {
System.out.println("捕获URL拦截器被销毁!");
} @Override
public void init() {
System.out.println("捕获URL拦截器初始化!");
}
@Override
public String intercept(ActionInvocation invocation) throws Exception {
ActionProxy actionProxy=invocation.getProxy();
String namespace=actionProxy.getNamespace();
String actionName=actionProxy.getActionName();
if(namespace==null||"/".equals(namespace)){
namespace="";
}
String url=namespace+"/"+actionName;
ServletContext sc=ServletActionContext.getServletContext();
ApplicationContext context=WebApplicationContextUtils.getWebApplicationContext(sc);
RightService rightService=(RightService) context.getBean("rightService");
rightService.appendRightByUrl(url+".action");
return invocation.invoke();
}
}

  struts.xml文件中的配置:

 <interceptors>
<interceptor name="loginInterceptor" class="com.kdyzm.struts.interceptors.LoginInterceptor"></interceptor>
<interceptor name="catchUrlInterceptor" class="com.kdyzm.struts.interceptors.CatchUrlInterceptor"></interceptor>
<interceptor-stack name="surveyparkStack">
<interceptor-ref name="loginInterceptor"></interceptor-ref>
<!-- url捕获拦截器应当放到登录拦截器之后,完成项目之后应当将该拦截器拿掉 -->
<interceptor-ref name="catchUrlInterceptor"></interceptor-ref>
<interceptor-ref name="defaultStack">
<param name="modelDriven.refreshModelBeforeResult">true</param>
</interceptor-ref>
</interceptor-stack>
</interceptors>
<!-- 定义默认栈 -->
<default-interceptor-ref name="surveyparkStack"></default-interceptor-ref>

三、删除权限和修改权限略

  修改权限和增加权限使用的是同一个页面,对应的dao中的实现使用的是saveOrUpdate方法,其它略。

【Java EE 学习 75 下】【数据采集系统第七天】【二进制运算实现权限管理】【使用反射初始化权限表】【权限捕获拦截器动态添加权限】的更多相关文章

  1. 【Java EE 学习 69 下】【数据采集系统第一天】【实体类分析和Base类书写】

    之前SSH框架已经搭建完毕,现在进行实体类的分析和Base类的书写.Base类是抽象类,专门用于继承. 一.实体类关系分析 既然是数据采集系统,首先调查实体(Survey)是一定要有的,一个调查有多个 ...

  2. 【Java EE 学习 74 下】【数据采集系统第六天】【使用Jfreechart的统计图实现】【将JFreechart整合到项目中】

    之前说了JFreechart的基本使用方法,包括生成饼图.柱状统计图和折线统计图的方法.现在需要将其整合到数据采集系统中根据调查结果生成三种不同的统计图. 一.统计模型的分析和设计 实现统计图显示的流 ...

  3. 【Java EE 学习 70 下】【数据采集系统第二天】【Action中User注入】【设计调查页面】【Action中模型赋值问题】【编辑调查】

    一.Action中User注入问题 Action中可能会经常用到已经登陆的User对象,如果每次都从Session中拿会显得非常繁琐.可以想一种方法,当Action想要获取User对象的时候直接使用, ...

  4. 【Java EE 学习 67 下】【OA项目练习】【SSH整合JBPM工作流】【JBPM项目实战】

    一.SSH整合JBPM JBPM基础见http://www.cnblogs.com/kuangdaoyizhimei/p/4981551.html 现在将要实现SSH和JBPM的整合. 1.添加jar ...

  5. 【Java EE 学习 35 下】【struts2】【struts2文件上传】【struts2自定义拦截器】【struts2手动验证】

    一.struts2文件上传 1.上传文件的时候要求必须使得表单的enctype属性设置为multipart/form-data,把它的method属性设置为post 2.上传单个文件的时候需要在Act ...

  6. 【Java EE 学习 24 下】【注解在数据库开发中的使用】【反射+注解+动态代理在事务中的应用service层】

    一.使用注解可以解决JavaBean和数据库中表名不一致.字段名不一致.字段数量不一致的问题. 1.Sun公司给jdbc提供的注解 @Table.@Column.@Id.@OneToMany.@One ...

  7. 【Java EE 学习 77 下】【数据采集系统第九天】【使用spring实现答案水平分库】【未解决问题:分库查询问题】

    之前说过,如果一个数据库中要存储的数据量整体比较小,但是其中一个表存储的数据比较多,比如日志表,这时候就要考虑分表存储了:但是如果一个数据库整体存储的容量就比较大,该怎么办呢?这时候就需要考虑分库了, ...

  8. 【Java EE 学习 72 下】【数据采集系统第四天】【移动/复制页分析】【使用串行化技术实现深度复制】

    一.移动.复制页的逻辑实现 移动.复制页的功能是在设计调查页面的时候需要实现的功能.规则是如果在同一个调查中的话就是移动,如果是在不同调查中的就是复制. 无论是移动还是复制,都需要注意一个问题,那就是 ...

  9. 【Java EE 学习 71 下】【数据采集系统第三天】【分析答案实体】【删除问题】【删除页面】【删除调查】【清除调查】【打开/关闭调查】

    一.分析答案实体 分析答案实体主要涉及到的还是设计上的问题,技术点几乎是没有的.首先需要确定一下答案的格式才能最终确定答案实体中需要有哪些属性. 答案格式的设计是十分重要的,现设计格式如下: 在表单中 ...

随机推荐

  1. History lives on in this distinguished Polish city 2017/1/4

    原文 History lives on in this distinguished Polish city Though it may be ancient. KraKow, Poland, is a ...

  2. Linux虚拟机中配置JDK环境变量

    前提准备: 1,安装好Linux系统 2,下载好可以将文件传输到Linux系统工具例如:WinSCP 3,在windows中下载Linux版JDK: http://download.oracle.co ...

  3. UML大战需求与分析--阅读笔记4

    今天阅读了UML大战需求与分析第五.六章. 第五章,状态机图(State Machine Diagram),状态机图是通过描述某事物状态的改变来展现流程的.一般适用于流程围绕某个事物展开,例如请假的流 ...

  4. mysql 基础操作一

    1.登录数据库 mysql -u root -p 2.查看数据库 show databases; 3.进入数据库 use  数据库名 4.查看该数据库中的表 show tables; 5.查看某一表中 ...

  5. DX 系列之 ComboBoxEdit

    参考资料: ComboBoxEdit.Properties.Items=ComboBoxItemCollection

  6. Linux2.6.11版本:classic RCU的实现

    转载自:http://www.wowotech.net/kernel_synchronization/linux2-6-11-RCU.html 一.前言 无论你愿意或者不愿意,linux kernel ...

  7. Ubuntu下安装QQ22013

    近期闲来无事,把退役的笔记本系统换成了Ubuntu. 系统安装异常的顺利,神速的安装完成.玩弄一会发现总是缺少了点什么,呆了半天发现缺少了企鹅. 由于对Ubuntu系统不了解,安装QQ着实让我头疼了半 ...

  8. Format 函数示例

    Format 函数示例本示例显示用 Format 函数做格式化输出的不同用法.对于日期分隔号(/),时间分隔号(:),以及 AM/ PM 等文本而言,其真正的显示格式会因计算机上的国际标准不同而有所差 ...

  9. [mark] Linux下如何批量删除空文件

    可以使用 xargs 命令来批量处理,代码如下: $ find . -name '*' -type f -size 0c | xargs rm -f

  10. rsa密钥文件转化为tortoise认可的pak密钥文件

    原贴地址: http://www.vectorns.com/blog/technical-articles/1-tortoisesvn-over-ssh-on-windows-via-putty Ne ...