拦截器与过滤器(二)拦截器集成与使用

一、集成

自定义拦截器类实现HandlerInterceptor接口,再把这个拦截器类加载到全文配置中。

(1)自定义拦截器类:



import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;
@Component
public class ActionHandle implements HandlerInterceptor {

    @Autowired
    private UserActionLogService userActionLogService;

    @Override
    public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
        try {
            String userId = SpringContextUtil.getCurrentUserId();
            String requestUrl = httpServletRequest.getRequestURI();
            Map<String, String[]> originRequestMap = httpServletRequest.getParameterMap();
            Map<String,String> requestMap = new HashMap<String,String>();
            for (String key : originRequestMap.keySet()) {
                String[] values = originRequestMap.get(key);
                requestMap.put(key,values[0]);
            }
            String requestParam = JSON.toJSONString(requestMap);
            String requestIp = httpServletRequest.getRemoteAddr();
            UserActionLog userActionLog = new UserActionLog(userId, requestUrl, requestParam, requestIp, "tableAdmin");
            userActionLogService.insert(userActionLog);
        } catch (Exception e) {
        }
        return true;

    }

    @Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {

    }
}

(2)加载(编程式加载) :

package com.demo.config;


import com.demo.interceptor.MyInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

@SpringBootConfiguration
public class MySpringMVCConfig extends WebMvcConfigurerAdapter {

    @Autowired
    private ActionHandle myInterceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(myInterceptor).addPathPatterns("/**");
    }


}

 也可以使用xml式加载。

拦截器过滤器是在开发中常用的两种设计模式,用于在请求处理的不同阶段对请求进行处理和拦截。它们的区别主要体现在使用场景和功能上。 拦截器(Interceptor)是一种在请求处理的不同阶段进行拦截并插入自定义逻辑的机制。它通常用于对请求进行预处理或后处理,例如记录日志、验证权限、设置上下文环境等。拦截器可以在请求的开始、结束或异常抛出等时机进行拦截,并且可以被链式调用,即一个拦截器可以调用多个其他拦截器拦截器通常与框架或中间件紧密结合,在请求的处理过程中按照一定的顺序依次执行。 过滤器(Filter)是一种在请求到达目标处理程序之前或之后对请求进行过滤和处理的机制。它通常用于对请求进行预处理、过滤或修改,例如字符编码转换、参数校验、防止跨站点脚本攻击等。过滤器可以在请求被处理前进行处理(前置过滤器),也可以在请求被处理后进行处理(后置过滤器)。过滤器通常与Web容器(如Servlet容器)紧密结合,在请求的处理过程中按照一定的顺序依次执行。 总结来说,拦截器过滤器的主要区别在于使用场景和功能。拦截器更适合进行请求的预处理和后处理,而过滤器更适合对请求进行过滤和修改。拦截器一般与框架紧密结合,可以链式调用,而过滤器一般与Web容器紧密结合,按照一定的顺序依次执行。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

w_t_y_y

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值