公共數據權限過濾 (使用 structs 拦截器)

1.先定义一个注解

package com.efoxconn.ipebg.protoline.annotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface DataAuth {
public String[] types() default {"project", "site"};
}


2.创建拦截器

package com.efoxconn.ipebg.protoline.interceptor;

import java.lang.reflect.Method;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.efoxconn.ipebg.protoline.annotation.DataAuth;
import com.efoxconn.ipebg.protoline.util.CheckDataUtil;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
@Component
@SuppressWarnings("unchecked")
public class DataAuthInterceptor extends AbstractInterceptor {

/**
* 公共數據權限過濾
*/
private static final long serialVersionUID = -4013438146040038934L;
@Autowired
CheckDataUtil checkDataUtil;

private String transName(String name){
return name+"_id";
}

@Override
public String intercept(ActionInvocation invocation) throws Exception {
String methodName=invocation.getProxy().getMethod();
Class<?> clazz=invocation.getAction().getClass();
Method method=clazz.getMethod(methodName);
DataAuth dataAuth = null;

if(clazz.isAnnotationPresent(DataAuth.class)){
dataAuth=clazz.getAnnotation(DataAuth.class);
}else if (method.isAnnotationPresent(DataAuth.class)) {
dataAuth=method.getAnnotation(DataAuth.class);
}
if (dataAuth!=null) {
Method getMethod=clazz.getMethod("getParams");
Method setMethod=clazz.getMethod("setParams", Map.class);
Map params = (Map) getMethod.invoke(invocation.getAction());
String[] types = dataAuth.types();
for(String type : types){
String name = transName(type);
if(params.containsKey(name)){
params.put(name, checkDataUtil.checkAuth((String) params.get(name), "auth_"+type));
}
}
setMethod.invoke(invocation.getAction(), params);
}
return invocation.invoke();
}

}


3. 配置拦截器
在struts的配置文件里

<interceptors>
<interceptor name="dataAuth" class="dataAuthInterceptor"/>
<interceptor-stack name="dataAuthStack" >
<interceptor-ref name="actionStack"/>
<interceptor-ref name="dataAuth"/>
</interceptor-stack>
</interceptors>
<default-interceptor-ref name="dataAuthStack"/>

4. 在需要使用的类或方法上加上注解就好了
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值