最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

SpringBoot 過(guò)濾器與攔截器實(shí)例演示

 更新時(shí)間:2021年11月23日 14:57:55   作者:宇智波波奶茶  
本文通過(guò)示例代碼給大家講解SpringBoot 過(guò)濾器與攔截器的相關(guān)知識(shí),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧

???SpringBoot中的過(guò)濾器攔截器操作與springmvc中的幾乎一樣所以這里也不過(guò)多介紹了,下面舉兩個(gè)簡(jiǎn)單的栗子演示一下

1、過(guò)濾器 ? ? ? ?

1 創(chuàng)建過(guò)濾器類(lèi)LoginFilter,實(shí)現(xiàn)servlet包下的Filter接口(包不要導(dǎo)錯(cuò)),加入注解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("攔截器銷(xiāo)毀");
    }
}

?????2 創(chuàng)建測(cè)試類(lèi)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 在啟動(dòng)類(lèi)上加注解

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可以直接通過(guò)@WebServlet、@WebFilter、@WebListener注解自動(dòng)注冊(cè)
public class DemoApplication {
 
    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
 
}

? 4 啟動(dòng)測(cè)試,啟動(dòng)的時(shí)候可以看到Filter調(diào)用init方法初始化

?接著訪問(wèn)可以看到頁(yè)面輸出

?控制臺(tái)打印出攔截器中的語(yǔ)句

2、攔截器 ? ? ? ?

1 創(chuàng)建自定義攔截器

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 {
   //進(jìn)入controller方法之前調(diào)用的
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("preHandle");
        return true;//true表示放行 false表示不放行
    }
    //調(diào)用完controller之后,視圖渲染層之前
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        System.out.println("postHandle");
    }
    //頁(yè)面跳轉(zhuǎn)之后,整個(gè)流程執(zhí)行之后,一般用于資源的清理
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        System.out.println("afterCompletion");
    }
}

? ? ? 2 創(chuàng)建攔截器配置類(lèi),注意要加上配置類(lèi)的注解

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) {
        //設(shè)置攔截器并指定攔截路徑
        registry.addInterceptor(new MyInterceptor()).addPathPatterns("/interceptor/*");
        //registry.addInterceptor(new MyInterceptor()).addPathPatterns("/**");//攔截所有
        //registry.addInterceptor(new MyInterceptor()).addPathPatterns("/**").excludePathPatterns("/test");//指定不攔截
 
        //添加自定義攔截器
        WebMvcConfigurer.super.addInterceptors(registry);
    }
}

? ? ? ? 3 創(chuàng)建LoginController測(cè)試類(lèi)

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 運(yùn)行訪問(wèn),查看效果

控制臺(tái)輸出如下

到此這篇關(guān)于SpringBoot 過(guò)濾器與攔截器的文章就介紹到這了,更多相關(guān)SpringBoot 過(guò)濾器與攔截器內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 如何使用Springfox?Swagger實(shí)現(xiàn)API自動(dòng)生成單元測(cè)試

    如何使用Springfox?Swagger實(shí)現(xiàn)API自動(dòng)生成單元測(cè)試

    Springfox是一個(gè)使用Java語(yǔ)言開(kāi)發(fā)開(kāi)源的API Doc的框架,它的前身是swagger-springmvc,可以將我們的Controller中的方法以文檔的形式展現(xiàn),這篇文章主要介紹了如何使用Springfox?Swagger實(shí)現(xiàn)API自動(dòng)生成單元測(cè)試,感興趣的朋友跟隨小編一起看看吧
    2024-04-04
  • 基于LinkedHashMap實(shí)現(xiàn)LRU緩存

    基于LinkedHashMap實(shí)現(xiàn)LRU緩存

    LinkedHashMap是Java集合中一個(gè)常用的容器,它繼承了HashMap, 是一個(gè)有序的Hash表。那么該如何基于LinkedHashMap實(shí)現(xiàn)一個(gè)LRU緩存呢?本文將介紹LinkedHashMap的實(shí)現(xiàn)原理,感興趣的同學(xué)可以參考一下
    2023-05-05
  • 解決maven項(xiàng)目tomcat啟動(dòng)失敗war exploded:Error during artifact deploym問(wèn)題

    解決maven項(xiàng)目tomcat啟動(dòng)失敗war exploded:Error during 

    在SpringMVC項(xiàng)目中,使用war和warexploded兩種部署方式可能會(huì)導(dǎo)致不同的路徑問(wèn)題,從而出現(xiàn)404錯(cuò)誤,war模式將項(xiàng)目打包上傳,而warexploded模式則保持文件夾結(jié)構(gòu)上傳,開(kāi)發(fā)時(shí)建議使用warexploded模式,方便利用Update classes and resources功能自動(dòng)更新
    2024-10-10
  • SpringBoot+vue實(shí)現(xiàn)登錄圖片驗(yàn)證碼功能

    SpringBoot+vue實(shí)現(xiàn)登錄圖片驗(yàn)證碼功能

    這篇文章主要給大家介紹一下如何SpringBoot+vue實(shí)現(xiàn)登錄圖片驗(yàn)證碼功能,文中有詳細(xì)的代碼示例,具有一定的參考價(jià)值,需要的朋友可以參考下
    2023-07-07
  • 詳解java如何實(shí)現(xiàn)帶RequestBody傳Json參數(shù)的GET請(qǐng)求

    詳解java如何實(shí)現(xiàn)帶RequestBody傳Json參數(shù)的GET請(qǐng)求

    在調(diào)試Fate平臺(tái)時(shí),遇到了一個(gè)奇葩的接口類(lèi)型,該接口為Get方式,入?yún)⑹且粋€(gè)json類(lèi)型在body中傳遞,使用body中傳參的話(huà)為什么不用POST請(qǐng)求而使用了GET請(qǐng)求,下面我們就來(lái)深入研究一下
    2024-02-02
  • 關(guān)于java 圖形驗(yàn)證碼的解決方法

    關(guān)于java 圖形驗(yàn)證碼的解決方法

    本篇文章小編為大家介紹,在java中,使用圖形驗(yàn)證碼的解決方法。需要的朋友參考下
    2013-04-04
  • mybatis中注解映射SQL示例代碼

    mybatis中注解映射SQL示例代碼

    這篇文章主要給大家介紹了關(guān)于mybatis中注解映射SQL的相關(guān)資料,文中給出了詳細(xì)的示例代碼供大家參考學(xué)習(xí),對(duì)大家的學(xué)習(xí)或者共組具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面跟著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。
    2017-08-08
  • Spring Boot啟動(dòng)端口修改方法

    Spring Boot啟動(dòng)端口修改方法

    spring boot是個(gè)好東西,可以不用容器直接在main方法中啟動(dòng),而且無(wú)需配置文件,方便快速搭建環(huán)境。下面通過(guò)本文給大家分享Spring Boot修改啟動(dòng)端口的方法,感興趣的的朋友一起看看吧
    2017-07-07
  • Java8如何使用Lambda表達(dá)式簡(jiǎn)化代碼詳解

    Java8如何使用Lambda表達(dá)式簡(jiǎn)化代碼詳解

    這篇文章主要給大家介紹了關(guān)于Java8如何使用Lambda表達(dá)式簡(jiǎn)化的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-11-11
  • Linux服務(wù)器Java進(jìn)程消失問(wèn)題解決

    Linux服務(wù)器Java進(jìn)程消失問(wèn)題解決

    這篇文章主要介紹了Linux服務(wù)器Java進(jìn)程消失問(wèn)題解決,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-11-11

最新評(píng)論

抚顺市| 运城市| 容城县| 镇坪县| 瑞金市| 志丹县| 晋州市| 瑞昌市| 长宁县| 英德市| 长白| 垦利县| 绵阳市| 固安县| 安新县| 和硕县| 巍山| 肃宁县| 绥德县| 江北区| 漳浦县| 婺源县| 怀宁县| 上杭县| 泗洪县| 通河县| 彰武县| 苗栗市| 贵南县| 肇庆市| 嘉祥县| 平原县| 孟村| 墨竹工卡县| 班玛县| 德州市| 鞍山市| 嘉黎县| 南平市| 昌都县| 正镶白旗|