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. 在需要使用的类或方法上加上注解就好了
分享到:
相关推荐
structs2拦截器实例2-权限验证拦截器,根据是否登录,跳转到后台页面,未登录,跳转到登录页面
一个简单使用structs2拦截器实现使用自定义拦截器来完成用户权限的控制:当浏览者需要请求执行某个操作时,应用需要先检查浏览者是否登录,以及是否有足够的权限来执行该操作。 系统登陆之前点击查询自动跳入登陆...
structs2拦截器实例计算action执行的时间,涉及拦截器,structs.xml的配置等等。。
jsp自定义标签 structs2 JUNIT structs2自定义拦截器
拦截器实例2-权限验证拦截器
structs2拦截器做的阻止未登录的用户访问
使用spring structs hibernate 实现的权限管理系统,功能挺强大的,提供人员管理、模块管理、角色管理、用户管理相关的代码。 同时,需注意,在分页中实现了动态更改每页显示行数的功能(请参考机构管理的实例)
Structs2核心过滤器,用于Structs2开发框架开发的必配置的核心配置过滤器。<filter></filter>标签内容。只需要下载导入入WEB-INF文件夹即可。
使用structs2快速开发 教你怎样使用structs2快速开发web 项目
获得structs的相对权限 url实现权限控制
Structs学习学习资料Structs学习Structs学习Structs学习Structs学习Structs学习Structs学习Structs学习Structs学习
NULL 博文链接:https://13lhqiang.iteye.com/blog/865800
是structs源代码中控制器部分的类图,很详细
structs实例structs实例structs实例structs实例
导入structs的jar包可使用structs框架
structs的登录数据库及数据添加,这是STRUCTS学习课程的的资源
Structs2开发必用 structs2.3.1官方JAR包 structs2.3.1官方JAR包 structs2.3.1官方JAR包 structs2.3.1官方JAR包 structs2.3.1官方JAR包
精通structs精通structs精通structs精通structs精通structs
构建structs2所需的基本jar包
structs1structs1structs1structs1structs1structs1structs1structs1structs1structs1structs1structs1structs1structs1structs1structs1structs1structs1structs1structs1structs1structs1structs1structs1structs1...