您的位置:首页 > 编程语言 > Java开发

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

2015-12-19 09:48 836 查看

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

在该系统中,我使用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方法,其它略。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: