Spring Boot使用拦截器Interceptor

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_38095094/article/details/78534406

Spring Boot使用拦截器Interceptor

过滤器Filter属于Servlet范畴的API
拦截器Interceptor属于spring范畴

拦截器的功能

HandlerInterceptor 的功能跟过滤器类似,但是提供更精细的的控制能力:在request被响应之前、request被响应之后、视图渲染之前以及request全部结束之后。我们不能通过拦截器修改request内容,但是可以通过抛出异常(或者返回false)来暂停request的执行。

实现自定义拦截器的方法

1、创建我们自己的拦截器类并实现 HandlerInterceptor 接口。
2、创建一个Java类继承WebMvcConfigurerAdapter,并重写 addInterceptors 方法。
3、实例化我们自定义的拦截器,然后将对像手动添加到拦截器链中(在addInterceptors方法中添加)。

创建拦截器实现HandlerInterceptor接口

    public class MyInterceptorFirst implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o)
            throws Exception {
        //当执行的语句抛出异常时,这个请求将被拦截也就是没有执行
        String requestString = httpServletRequest.getParameter("name");
        System.out.println("拿到的请求参数为"+requestString);
        if (requestString.equalsIgnoreCase("hello")) {
            throw new RuntimeException("辣鸡");
        }
        System.out.println("调用MyInterceptorFirst的 preHandle");
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {
        System.out.println(">>>MyInterceptor1>>>>>>>请求处理之后进行调用,但是在视图被渲染之前(Controller方法调用之后)");
    }

    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {
        System.out.println(">>>MyInterceptor1>>>>>>>在整个请求结束之后被调用,也就是在DispatcherServlet 渲染了对应的视图之后执行(主要是用于进行资源清理工作)");
    }
}

我们可以看到,假如请求的参数name对应的值为hello时,将会抛出异常,拦截器便将这个请求进行拦截,通过抛出异常来进行request的暂停。

创建java类继承WebMvcConfigurerAdapter,并重写addInterceptors方法

@Configuration  //注意一定要添加这个注解
public class MyWebAppConfigurer extends WebMvcConfigurerAdapter {
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //拦截器拦截所有
        registry.addInterceptor(new MyInterceptorFirst()).addPathPatterns("/**");
        //当需要多个拦截器的时候就使用多个addInterceptor方法
        super.addInterceptors(registry);
    }
}

其中需要注意的就是@Configuration这个注解的书写

SpringBoot自定义目录

Spring Boot默认静态资源目录

在启动springBoot时可以看到如下信息

2017-10-22 15:30:50.873  INFO 8560 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]  
2017-10-22 15:30:50.873  INFO 8560 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]  
2017-10-22 15:30:50.911  INFO 8560 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]

其中默认配置的 /** 映射到 /static(或/public、/resources、/META-INF/resources)
其中默认配置的 /webjars/** 映射到 classpath:/META-INF/resources/webjars/

image
当读取fengjing.jpg时,spring Boot的检测顺序为

META-INF/resources
resource
static
public

自定义资源目录

实现类继承 WebMvcConfigurerAdapter 并重写方法 addResourceHandlers

@Configuration
public class MyWebAppConfigurer 
        extends WebMvcConfigurerAdapter {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/myres/**").addResourceLocations("classpath:/myres/");
        super.addResourceHandlers(registry);
    }

}

上面代码的意思是,当访问url为 …/myres/..时,程序将会从resources/myres下寻找相应的资源,注意classpath关键字不能丢

扫描二维码关注公众号,回复: 3121688 查看本文章

此外,还可以使用外部目录

    // 可以直接使用addResourceLocations 指定磁盘绝对路径,同样可以配置多个位置,注意路径写法需要加上file:
registry.addResourceHandler("/myimgs/**").addResourceLocations("file:F/img");

也可以采用配置文件的方式来进行配置(配置application.yml文件)

      mvc:
    static-path-pattern: /myimgs/**
  resources:
    static-locations: file:F/img

这样通过访问…/myimgs/…就可以访问到F/img路径下的所有内容

猜你喜欢

转载自blog.csdn.net/qq_38095094/article/details/78534406