当前位置 : 主页 > 编程语言 > java >

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

来源:互联网 收集:自由互联 发布时间:2021-12-01
   SpringBoot中的过滤器拦截器操作与springmvc中的几乎一样所以这里也不过多介绍了,下面举两个简单的栗子演示一下 1、过滤器         1 创建过滤器类LoginFilter,实现servlet包下的Filte

   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 过滤器与拦截器内容请搜索自由互联以前的文章或继续浏览下面的相关文章希望大家以后多多支持自由互联!

上一篇:JPA-JpaRepository方法命名语法说明
下一篇:没有了
网友评论