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

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

 

引用别的的:

最近项目里加上了用户权限,有些操作需要登录,有些操作不需要,之前做项目做权限,喜欢使用过滤器,但在此使用过滤器比较死板,如果用的话,就必须在配置文件里加上所有方法,而且 不好使用通配符。所以想了想,之前在人人用过的一种比较简单灵活的权限判断,是采用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> 

转载于:https://www.cnblogs.com/hjwnotes/p/10678542.html

你可能感兴趣的文章
java中Object类 源代码详解
查看>>
开源控Meteor的个人资料
查看>>
kafka在zookeeper中的存储结构
查看>>
linux上FTP服务器搭建
查看>>
.net 使用AgsXMPP与openfire连接,实现跨平台信息流通。
查看>>
DP动态规划【专辑@AbandonZHANG】
查看>>
Android TextureView简易教程
查看>>
fatal: the remote end hung up unexpectedly
查看>>
Delphi-操作剪贴板
查看>>
hdu 1029
查看>>
Docker 容器的网络连接 & 容器互联
查看>>
吾爱专题脱壳练习----压缩壳练习之三
查看>>
LeetCode -- Palindrome Linked List
查看>>
栈应用——逆波兰式表达式的值
查看>>
vscode 快速生成html
查看>>
HTML5 全屏化操作功能
查看>>
返本求源——DOM元素的特性与属性
查看>>
4、C#进阶:MD5加密、进程、线程、GDI+、XML、委托
查看>>
部署DLL webservices 若干费脑点
查看>>
zabbix监控报错zabbix server is not running解决方法
查看>>