spring MethodInterceptor方法拦截
引用别的的:https://blog.csdn.net/u010739551/article/details/47754731
最近项目里加上了用户权限,有些操作需要登录,有些操作不需要,之前做项目做权限,喜欢使用过滤器,但在此使用过滤器比较死板,如果用的话,就必须在配置文件里加上所有方法,而且 不好使用通配符。所以想了想,之前在人人用过的一种比较简单灵活的权限判断,是采用Spring 的 methhodInterceptor拦截器完成的,并且是基于注解的
@LoginRequired
@RequestMapping(value = "/comment")
public void comment(HttpServletRequest req, HttpServletResponse res) {
doSomething,,,,,,,,
}
我是在Spring mvc 的controller层的方法上拦截的,注意上面的@LoginRequired 是我自定义的注解。这样的话,该方法被拦截后,如果有该 注解,则表明该 方法需要用户登录后才能执行某种操作,于是乎,我们可以判断request里的session或者Cookie是否包含用户已经登录的身份,然后判断是否执行该方法;如果没有,则执行另一种操作。
-------------------------------------------------------------------------
下面是自定义注解的代码:
package com.qunar.wireless.ugc.controllor.web;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface LoginRequired {
}
-----------------------------------------------------------------------------
下面是自定义的方法拦截器,继续自aop的MethodInterceptor
import javax.servlet.http.HttpServletRequest;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import com.qunar.wireless.ugc.controllor.web.LoginRequired;
/**
* @author tao.zhang
* @create-time 2012-2-31
*/
public class LoginRequiredInterceptor1 implements MethodInterceptor {
@Override
public Object invoke(MethodInvocation mi) throws Throwable {
Object[] ars = mi.getArguments();
for(Object o :ars){
if(o instanceof HttpServletRequest){
System.out.println("------------this is a HttpServletRequest Parameter------------ ");
}
}
// 判断该方法是否加了@LoginRequired 注解
if(mi.getMethod().isAnnotationPresent(LoginRequired.class)){
System.out.println("----------this method is added @LoginRequired-------------------------");
}
//执行被拦截的方法,切记,如果此方法不调用,则被拦截的方法不会被执行。
return mi.proceed();
}
}
------------------------------------------------------------------------
配置文件:
<bean id="springMethodInterceptor" class="com.qunar.wireless.ugc.interceptor.LoginRequiredInterceptor1" ></bean>
<aop:config>
<!--切入点-->
<aop:pointcut id="loginPoint"
expression="execution(public * com.qunar.wireless.ugc.controllor.web.*.*(..)) "/>
<!--在该切入点使用自定义拦截器-->
<aop:advisor pointcut-ref="loginPoint" advice-ref="springMethodInterceptor"/>
</aop:config>
spring MethodInterceptor方法拦截的更多相关文章
- 使用方法拦截机制在不修改原逻辑基础上为 spring MVC 工程添加 Redis 缓存
首先,相关文件:链接: https://pan.baidu.com/s/1H-D2M4RfXWnKzNLmsbqiQQ 密码: 5dzk 文件说明: redis-2.4.5-win32-win64.z ...
- 关于spring的aop拦截的问题 protected方法代理问题
看到一篇很好的Spring aop 拦截方法的问题, 原文地址. 问题 貌似不能拦截私有方法? 试了很多次,都失败了,是不是不行啊? 我想了一下,因为aop底层是代理, jdk是代理接口,私有方法必 ...
- (转)spring中的拦截器(HandlerInterceptor+MethodInterceptor)
1. 过滤器跟拦截器的区别 在说拦截器之前,不得不说一下过滤器,有时候往往被这两个词搞的头大. 其实我们最先接触的就是过滤器,还记得web.xml中配置的<filter>吗~ 你应该知道 ...
- 实战CGLib系列之proxy篇(一):方法拦截MethodInterceptor
实战CGLib系列文章 本篇介绍通过MethodInterceptor和Enhancer实现一个动态代理. 一.首先说一下JDK中的动态代理: JDK中的动态代理是通过反射类Proxy以及Invoca ...
- spring---aop(3)---Spring AOP的拦截器链
写在前面 时间断断续续,这次写一点关于spring aop拦截器链的记载.至于如何获取spring的拦截器,前一篇博客已经写的很清楚(spring---aop(2)---Spring AOP的JDK动 ...
- spring mvc +cookie+拦截器功能 实现系统自动登陆
先看看我遇到的问题: @ResponseBody @RequestMapping("/logout") public Json logout(HttpSession session ...
- Spring Security @PreAuthorize 拦截无效
1. 在使用spring security的时候使用注解,@PreAuthorize("hasAnyRole('ROLE_Admin')") 放在对方法的访问权限进行控制失效,其中 ...
- 玩转spring MVC(七)----拦截器
继续在前边的基础上来学习spring MVC中拦截器的使用,下面通过一个例子来实现(完整项目在这里下载:http://download.csdn.net/detail/u012116457/84334 ...
- Spring Boot配置拦截器及实现跨域访问
拦截器功能强大,能够深入方法前后,常应用于日志记录.权限检查和性能检测等,几乎是项目中不可或缺的一部分,本文就来实现Spring Boot自定义拦截器的配置. 理论指导 问:Spring Boot怎么 ...
随机推荐
- Android 导入 aar 库文件
1. 在需要导入 aar 的 module 目录下创建一个名叫 "aars" 的目录,并把 aar 文件复制到这里. 2. 在项目的 build.gradle 文件里添加 allp ...
- Spring配置项<context:annotation-config>的解释说明
今天在闲逛CSDN论坛时,看到一位博主写的一篇关于<Spring中IOC的Annotation的实现>的文章, 于是点击进去看了下, 发现在说明中对Spring配置文件中的有些配置节点模凌 ...
- svn update 产生Node remains in conflict的问题
输入:sudo svn revert --depth=infinity frontend/web/js/workplace/organization.js 最后在执行 svn up 就ok了
- vue复选框选中值获取
<div id="d5"> <p>{{box5.toString()}}</p> <input type="checkbox&q ...
- Andrew Ng机器学习第五章——多变量线性回归
一.多变量线性回归的技巧之一——特征缩放 1.为什么要使用特征缩放? 特征缩放用来确保特征值在相似的范围之内. 设想这样一种情况(房价预测),两个特征值分别是房子的大小和卧室的数量.每个特征值所处的范 ...
- jmeter -- 在beanshell中拿到请求body参数和header参数
beanshell: import org.apache.jmeter.config.Arguments; import org.apache.jmeter.protocol.http.control ...
- ASP.NET 下使用特定身份完成windows服务的功能操作
今天部署项目的发现一个问题: 在本地Win7系统下利用Web页面完成Windows服务的功能操作(启动.停止.安装.卸载)都是正常的,而部署到Server2008系统下,再使用Web页面完成windo ...
- spring boot整合RabbitMQ(Topic模式)
1.Topic交换器介绍 Topic Exchange 转发消息主要是根据通配符. 在这种交换机下,队列和交换机的绑定会定义一种路由模式,那么,通配符就要在这种路由模式和路由键之间匹配后交换机才能转发 ...
- 【数组】Product of Array Except Self
题目: iven an array of n integers where n > 1, nums, return an array output such that output[i] is ...
- Okhttp3上传多张图片同时传递参数
之前上传图片都是直接将图片转化为io流传给服务器,没有用框架传图片. 最近做项目,打算换个方法上传图片. Android发展到现在,Okhttp显得越来越重要,所以,这次我选择用Okhttp上传图片. ...