Spring Boot从入门到放弃-自定义拦截器

目录:

第一个程序 hello World

Spring Boot 关闭某些自动配置

Spring Boot 修改banner

Spring Boot 全局配置文件

Spring Boot 从入门到放弃-获取自定义配置

Spring Boot 整合测试

Spring Boot Application与Controller分离

Spring Boot 日志文件

Spring Boot 自定义日志文件

Spring Boot 开发模式

Spring Boot 访问静态资源(以HTML为例)

Spring Boot 消息转换器

Spring Boot 配置FastJson

摘要:

    有些时候我们需要自己配置SpringMVC而不是采用默认配置,比如说增加一个拦截器,这个时候就得通过继承WebMvcConfigurerAdapter然后重写父类中的方法进行扩展。

项目结构:

为了验证是否执行了拦截,所以我们除了默认访问8080还增加了http://localhost:8080/interceptor/show路径,拦截interceptor/**。

MyInterceptor.java:

package com.edu.usts.interceptor;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

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

/**
 * 自定义拦截器
 */
@Configuration //声明这是一个配置
public class MyInterceptor extends WebMvcConfigurerAdapter {
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        super.addInterceptors(registry);
        HandlerInterceptor interceptor = new HandlerInterceptor() {
            @Override
            public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
                System.out.println("自定义拦截器====》preHandle");
                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 {

            }
        };
        registry.addInterceptor(interceptor).addPathPatterns("/interceptor/**");
    }
}

MyInterceptorController.java:

package com.edu.usts.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("interceptor")
public class MyInterceptorController {

    @ResponseBody
    @RequestMapping("show")
    public String myInterceptorShow(){

        return "Interceptor Show !";
    }
}

MyApplication.java:

@SpringBootApplication
public class MyApplication  {
    //    启动springboot项目
    public static void main(String[] args) {
        SpringApplication.run(MyApplication.class,args);
    }
}

注:由于下面拦截器类在MyApplication的子包下故不需要扫描器,如果非子包需要添加扫描如:

@SpringBootApplication(scanBasePackages = "com.edu.usts.interceptor")
这里同时要把controller一起扫描,字符串按逗号隔开。

测试截图:

未输出任何信息。

访问测试路径:

拦截成功!

仅用输出进行测试,可按需要进行拦截。

源码gitee地址:

https://gitee.com/jockhome/springboot

发布了41 篇原创文章 · 获赞 108 · 访问量 10万+

猜你喜欢

转载自blog.csdn.net/qq_37857921/article/details/103545938