java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > SpringBoot 过滤器与拦截器

SpringBoot 过滤器与拦截器实例演示

作者:宇智波波奶茶

本文通过示例代码给大家讲解SpringBoot 过滤器与拦截器的相关知识,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧

   SpringBoot中的过滤器拦截器操作与springmvc中的几乎一样所以这里也不过多介绍了,下面举两个简单的栗子演示一下

1、过滤器        

1 创建过滤器类LoginFilter,实现servlet包下的Filter接口(包不要导错),加入注解WebFilter

package com.example.filter;
 
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
 
@WebFilter(urlPatterns = "/filter/*")
public class LoginFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        System.out.println("初始化拦截器");
    }
 
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        System.out.println("LoginFilter");
        HttpServletRequest request = (HttpServletRequest)servletRequest;
        HttpServletResponse response = (HttpServletResponse)servletResponse;
        //放行
        filterChain.doFilter(request,response);
    }
 
    @Override
    public void destroy() {
        System.out.println("拦截器销毁");
    }
}

     2 创建测试类LoginCotroller

package com.example.controller;
 
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
 
@RestController
@RequestMapping("/filter")
public class FilterController {
 
    @RequestMapping("/login")
    public String login(){
        System.out.println("登录");
        return "login";
    }
 
}

      3 在启动类上加注解

package com.example;
 
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
 
@SpringBootApplication
@ServletComponentScan
//Servlet、Filter、Listener可以直接通过@WebServlet、@WebFilter、@WebListener注解自动注册
public class DemoApplication {
 
    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
 
}

  4 启动测试,启动的时候可以看到Filter调用init方法初始化

 接着访问可以看到页面输出

 控制台打印出拦截器中的语句

2、拦截器        

1 创建自定义拦截器

package com.example.interceptor;
 
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
public class MyInterceptor implements HandlerInterceptor {
   //进入controller方法之前调用的
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("preHandle");
        return true;//true表示放行 false表示不放行
    }
    //调用完controller之后,视图渲染层之前
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        System.out.println("postHandle");
    }
    //页面跳转之后,整个流程执行之后,一般用于资源的清理
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        System.out.println("afterCompletion");
    }
}

      2 创建拦截器配置类,注意要加上配置类的注解

package com.example.config;
 
import com.example.interceptor.MyInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
 
@Configuration
public class MyInterceptorConfig implements WebMvcConfigurer {
 
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //设置拦截器并指定拦截路径
        registry.addInterceptor(new MyInterceptor()).addPathPatterns("/interceptor/*");
        //registry.addInterceptor(new MyInterceptor()).addPathPatterns("/**");//拦截所有
        //registry.addInterceptor(new MyInterceptor()).addPathPatterns("/**").excludePathPatterns("/test");//指定不拦截
 
        //添加自定义拦截器
        WebMvcConfigurer.super.addInterceptors(registry);
    }
}

        3 创建LoginController测试类

package com.example.controller;
 
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
 
@RestController
@RequestMapping("/interceptor")
public class InterceptorController {
    @RequestMapping("login")
    public String login(){
        System.out.println("Interceptor-Login");
        return "login";
    }
}

      4 运行访问,查看效果

控制台输出如下

到此这篇关于SpringBoot 过滤器与拦截器的文章就介绍到这了,更多相关SpringBoot 过滤器与拦截器内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文