博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
spring方法拦截器 MethodInterceptor
阅读量:4188 次
发布时间:2019-05-26

本文共 1417 字,大约阅读时间需要 4 分钟。

使用到spring方法拦截器 MethodInterceptor实现权限控制,MethodInterceptor可以使用通配符,并且是基于注解的。
简单例子代码如下:
1、定义需要拦截的类
Java代码 收藏代码
public class LoginAction{
//没有权限限制
@RequestMapping(value = "/login")
public void login(HttpServletRequest req, HttpServletResponse res) {
//登录功能.
}
//需要登录完成后才可访问
@LoginMethod
@RequestMapping(value = "/userList")
public void userList(HttpServletRequest req, HttpServletResponse res) {
//获取用户列表
}
}
注意上面的@LoginMethod是我自定义的注解
2、定义LoginMethod注解
Java代码 收藏代码
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface LoginMethod {
}
3、定义MethodInterceptor拦截器
Java代码 收藏代码
public class SystemMethodInterceptor implements MethodInterceptor {
@Override
public Object invoke(MethodInvocation methodInvocation) throws Throwable {
Method method = methodInvocation.getMethod();
if(method.isAnnotationPresent(LoginMethod.class)){//加了@LoginMethod注解,被拦截
User user = sessionUtil.getCurrUser();
if(user == null){//未登录
//proceed方法不调用,方法被拦截
return null;
}else{
return methodInvocation.proceed();//该方法不调用,则被拦截的方法不会被执行
}
}else{
return methodInvocation.proceed();
}
}
}
4、配置文
Xml代码 收藏代码
<bean id="systemMethodInterceptor" class="com.tzz.interceptor.SystemMethodInterceptor" >
</bean>
<aop:config>
<!--切入点-->
<aop:pointcut id="methodPoint" expression="execution(* com.tzz.controllor.web.*.*(..)) "/><!--在该切入点使用自定义拦截器-->
<aop:advisor pointcut-ref="methodPoint" advice-ref="systemMethodInterceptor"/>
</aop:config>

转载地址:http://tznoi.baihongyu.com/

你可能感兴趣的文章
ERwin Data Modeler 建模实践
查看>>
性能调优:JDK5.0自带工具
查看>>
认识软件估算 (7)
查看>>
项目控制 (sky)
查看>>
DWR 与 SPRING 集成配置
查看>>
JSTL 语法及参数
查看>>
懒加载
查看>>
DWR 几种使用方法
查看>>
DWR 学习及深入
查看>>
OA系统的技术发展
查看>>
EXT 基本使用
查看>>
网站架构收集(I)(转)
查看>>
JFreeChart教程 -- 入门
查看>>
JFreeChart各种图形的制作
查看>>
JFreeChart 应用全过程
查看>>
Spring 架构
查看>>
给你感情保鲜
查看>>
ORCALE 优化常青树
查看>>
Java获得CPU序列号和网卡Mac地址
查看>>
如何在Windows XP以上的版本中得知一个进程所使用的端口
查看>>