反射和注解的妙用——struts2基于方法的权限控制

好文章,找的就是这个,哈哈

转自:http://haohaoxuexi.iteye.com/blog/1217885

权限控制是每一个系统都应该有的一个功能,有些只需要简单控制一下就可以了,然而有些却需要进行更加深入和细致的权限控制,尤其是对于一些MIS类系统,基于方法的权限控制就更加重要了。

用反射和自定义注解来实现基于struts2的方法级别的权限控制的主要思想是这样的。

 

1.先定义一个用于识别在进行action调用的时候标注该方法调用是否需要权限控制,需要什么样的权限的注解类。该注解类一般会包括两个属性,一个是需要的权限,一个是对应的action。

 

2.然后就是在需要进行权限控制的action方法上加上该注解类,并标明其应该拥有的权限和对应的action。这样一来在进行action调用的时候可以实现一个 自己定义的interceptor来拦截所有的请求,这样在拦截到请求的时候就可以通过ActionInvocation获取到对应的action类的类文件和对应请求的方法名称,然后利用反射来取得action类文件里面对应的请求方法Method,这样就可通过该Method来判断其是否拥有对应的权限控制注解,即看其是否需要进行权限控制,如果需要进行权限控制,就取得该注解并取得其对应的action名称和需要的权限,然后通过session取得当前的用户,并判断当前用户是否拥有对应的某种权限,如果其拥有该权限则继续往下执行,否则,转到自己处理无权限的机制上去。

 

下面是一段示例代码:

Authority注解:

Java代码   收藏代码
  1. import java.lang.annotation.ElementType;  
  2. import java.lang.annotation.Retention;  
  3. import java.lang.annotation.RetentionPolicy;  
  4. import java.lang.annotation.Target;  
  5.   
  6. @Target(ElementType.METHOD)  
  7. @Retention(RetentionPolicy.RUNTIME)  
  8. public @interface Authority {  
  9.   
  10.     String action();  
  11.       
  12.     String privilege();  
  13.       
  14. }  

 

 

用于拦截请求判断是否拥有权限的拦截器AuthorityInterceptor:

Java代码   收藏代码
  1. import java.lang.reflect.Method;  
  2. import java.util.Date;  
  3.   
  4. import org.apache.struts2.ServletActionContext;  
  5.   
  6.   
  7. import com.opensymphony.xwork2.ActionInvocation;  
  8. import com.opensymphony.xwork2.interceptor.Interceptor;  
  9.   
  10. public class AuthorityInterceptor implements Interceptor {  
  11.   
  12.     /** 
  13.      *  
  14.      */  
  15.     private static final long serialVersionUID = -4637261955156527951L;  
  16.   
  17.     @Override  
  18.     public void destroy() {  
  19.         // TODO Auto-generated method stub  
  20.   
  21.     }  
  22.   
  23.     @Override  
  24.     public void init() {  
  25.         // TODO Auto-generated method stub  
  26.   
  27.     }  
  28.   
  29.     @Override  
  30.     public String intercept(ActionInvocation invocation) throws Exception {  
  31.         // TODO Auto-generated method stub  
  32.         String methodName = invocation.getProxy().getMethod();  
  33.         Method currentMethod = invocation.getAction().getClass().getMethod(methodName);  
  34.         //如果该方法请求是需要进行验证的时候执行以下逻辑  
  35.         if (currentMethod.isAnnotationPresent(Authority.class)) {  
  36. //          String currentUser = Util.getCurrentUser(ServletActionContext.getRequest());  
  37.             //这里可以从session里面取得当前的用户  
  38.             String currentUser = (String)ServletActionContext.getRequest().getSession().getAttribute("currentUser");  
  39.             //取得权限验证的注解  
  40.             Authority authority = currentMethod.getAnnotation(Authority.class);  
  41.             //取得当前请求的注解的action  
  42.             String actionName = authority.action();  
  43.             //取得当前请求需要的权限  
  44.             String privilege = authority.privilege();  
  45.             /** 
  46.              * 然后可以在此判断当前用户是否拥有对应的权限,如果没有可以跳到指定的无权限提示页面,如果拥有则可以 
  47.              * 继续往下执行。 
  48.              * ............... 
  49.              * if (拥有对应的权限) { 
  50.              *      return invocation.invoke(); 
  51.              * } else { 
  52.              *      return "无权限"; 
  53.              * } 
  54.              */  
  55.             System.out.println("++++++++++++++++++++++++++++++++++++++++++++++++++++++");  
  56.             System.out.println("用户" + currentUser + "在" + new Date() + actionName);  
  57.             System.out.println("++++++++++++++++++++++++++++++++++++++++++++++++++++++");  
  58.         }  
  59.         return invocation.invoke();  
  60.     }  
  61.   
  62. }  

 

需要进行权限控制的Action:

Java代码   收藏代码
  1. import com.opensymphony.xwork2.ActionSupport;  
  2.   
  3. public class TestAction extends ActionSupport {  
  4.   
  5.     /** 
  6.      *  
  7.      */  
  8.     private static final long serialVersionUID = 4679109342743365308L;  
  9.   
  10.     @Override  
  11.     @Authority(action="test", privilege="find")//请求该方法的时候需要拥有对test的find权限  
  12.     public String execute() throws Exception {  
  13.         // TODO Auto-generated method stub  
  14.         return super.execute();  
  15.     }  
  16.       
  17.     //请求该方法需要拥有对test的test权限  
  18.     @Authority(action="test", privilege="test")  
  19.     public String test() {  
  20.         return "test";  
  21.     }  
  22.   
  23. }  

 

 

在struts2的配置文件里面配置自定义的权限控制拦截器:

Xml代码   收藏代码
  1. <!-- 定义拦截器 -->  
  2.      <interceptors>  
  3.      <!-- 申明自定义的权限控制拦截器 -->  
  4.         <interceptor name="authorityInterceptor" class="com.test.AuthorityInterceptor"/>  
  5.         <!-- 把自定义的权限控制拦截器和默认的拦截器栈加到新的自定义的拦截器栈 -->  
  6.         <interceptor-stack name="myInterceptors">  
  7.             <interceptor-ref name="authorityInterceptors"/>  
  8.             <interceptor-ref name="defaultStack"/>  
  9.         </interceptor-stack>  
  10.      </interceptors>  
  11.      <!-- 指定新的自定义的拦截器栈为默认的拦截器栈,这样自定义的权限控制拦截器就可以发挥作用了 -->  
  12.      <default-interceptor-ref name="myInterceptors"></default-interceptor-ref>  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值