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 過濾器與攔截器內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!

推薦閱讀: