首页 > 编程语言 > 详细

springboot配置拦截器

时间:2020-12-22 10:28:02      阅读:26      评论:0      收藏:0      [点我收藏+]

首先创建拦截器:

技术分享图片

 代码:

package com.qf.springnew.interceptor;

import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *  Created by XuYinShan on 2020/12/22
 */
@Component
public class MyInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("MyInterceptor preHandle...");
        return true;
    }
}

注意,演示的preHandle是return true,放行了。

配置拦截器:

技术分享图片

 代码:

package com.qf.springnew.config;

import com.qf.springnew.interceptor.MyInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 *  Created by XuYinShan on 2020/12/22
 */
@Configuration
public class MyWebMVCConfig implements WebMvcConfigurer {
    @Autowired
    private MyInterceptor myInterceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 添加我们的拦截器
        registry.addInterceptor(myInterceptor).addPathPatterns("/**");
    }
}

结束。

springboot配置拦截器

原文:https://www.cnblogs.com/xuyinshan/p/14171464.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!