欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

SpringBoot攔截器Filter的使用方法詳解

 更新時(shí)間:2020年03月13日 13:45:39   作者:玉天恒  
這篇文章主要介紹了SpringBoot攔截器Filter的使用方法詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下

前言:

最新Servlet 3.0攔截器的使用

1.pom.xml添加需要使用的依賴(lài)

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 <modelVersion>4.0.0</modelVersion>
 <groupId>top.ytheng</groupId>
 <artifactId>springboot-demo</artifactId>
 <version>0.0.1</version>
 <packaging>jar</packaging>
 
 <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.5.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
  </parent>
  

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
  </properties>

  <dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-test</artifactId>
      <scope>test</scope>
    </dependency>
    
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>
    
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-devtools</artifactId>
      <optional>true</optional>
       <scope>true</scope>
    </dependency>
  </dependencies>

  <build>
    <!-- 打包的名稱(chēng) -->
    <finalName>myspringboot</finalName>
    <plugins>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
      </plugin>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

2.添加Filter攔截器

package top.ytheng.demo.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

//Servlet3.0特性
//urlPatterns:攔截的url地址
//filterName:攔截器名稱(chēng)
@WebFilter(urlPatterns="/api/*", filterName="loginFilter")
public class LoginFilter implements Filter{

  /*
   * 容器加載完成調(diào)用
   * */
  @Override
  public void init(FilterConfig filterConfig) throws ServletException {
    // TODO Auto-generated method stub
    System.out.println("filter init...");
  }
  
  /*
   *  請(qǐng)求被攔截的時(shí)候調(diào)用
   * */
  @Override
  public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
      throws IOException, ServletException {
    // TODO Auto-generated method stub
    System.out.println("doFilter...");
    
    HttpServletRequest req = (HttpServletRequest)request;
    HttpServletResponse resp = (HttpServletResponse)response;
    
    String username = req.getParameter("username");
    if(username.equals("theng")) {
      chain.doFilter(request, response);
    } else {
      //重定向
      resp.sendRedirect("/filter.html");
      return;
    }
    
  }

  /*
   * 容器被銷(xiāo)毀的時(shí)候調(diào)用
   * */
  @Override
  public void destroy() {
    // TODO Auto-generated method stub
    System.out.println("filter destroy...");
  }
  
}

3.添加測(cè)試控制器

package top.ytheng.demo.controller;

import java.util.HashMap;
import java.util.Map;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/v1/filter")
public class FilterController {

  @RequestMapping("/test")
  public Object testFilter() {
    Map<String, Object> map = new HashMap<>();
    map.put("name", "theng");
    map.put("pwd", "123456");
    return map;
  }
}

4.添加啟動(dòng)類(lèi)

package top.ytheng.demo;

import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;

@SpringBootApplication //等于下面3個(gè)
//@SpringBootConfiguration
//@EnableAutoConfiguration
//@ComponentScan
//攔截器用到
@ServletComponentScan
public class DemoApplication {

  public static void main(String[] args) {
    SpringApplication.run(DemoApplication.class, args);
  }
  
}

5.添加攔截后調(diào)整的頁(yè)面filter.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
  <h4>hello theng</h4>
  <h3>filter success</h3>
</body>
</html>

6.右鍵項(xiàng)目Run As啟動(dòng)項(xiàng)目,測(cè)試地址

http://localhost:8080/api/v1/filter/test?username=theng
http://localhost:8080/api/v1/filter/test?username=ytheng

另附:

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • MyBatis mapping類(lèi)基本用法

    MyBatis mapping類(lèi)基本用法

    這篇文章主要為大家介紹了MyBatis mapping類(lèi)基本用法示例詳解,
    有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-09-09
  • 詳解spring boot 使用application.properties 進(jìn)行外部配置

    詳解spring boot 使用application.properties 進(jìn)行外部配置

    這篇文章主要介紹了詳解spring boot 使用application.properties 進(jìn)行外部配置,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-03-03
  • Java 如何解析key為動(dòng)態(tài)的json操作

    Java 如何解析key為動(dòng)態(tài)的json操作

    這篇文章主要介紹了Java 如何解析key為動(dòng)態(tài)的json操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2021-02-02
  • Javaweb使用cors完成跨域ajax數(shù)據(jù)交互

    Javaweb使用cors完成跨域ajax數(shù)據(jù)交互

    本文由跨域、cors的概念開(kāi)始,進(jìn)而向大家介紹了Javaweb使用cors完成跨域ajax數(shù)據(jù)交互的相關(guān)內(nèi)容,需要的朋友可以了解下。
    2017-09-09
  • Mybatis分頁(yè)的4種方式實(shí)例

    Mybatis分頁(yè)的4種方式實(shí)例

    這篇文章主要介紹了Mybatis分頁(yè)的4種方式實(shí)例,包括數(shù)組分頁(yè),sql分頁(yè),攔截器分頁(yè),RowBounds分頁(yè),需要的朋友可以參考下
    2022-04-04
  • java 抓取網(wǎng)頁(yè)內(nèi)容實(shí)現(xiàn)代碼

    java 抓取網(wǎng)頁(yè)內(nèi)容實(shí)現(xiàn)代碼

    這篇文章主要介紹了java 抓取網(wǎng)頁(yè)內(nèi)容實(shí)現(xiàn)代碼,需要的朋友可以參考下
    2014-02-02
  • SpringBoot打包發(fā)布到linux上(centos 7)的步驟

    SpringBoot打包發(fā)布到linux上(centos 7)的步驟

    這篇文章主要介紹了SpringBoot打包發(fā)布到linux上(centos 7)的步驟,幫助大家更好的理解和使用springboot框架,感興趣的朋友可以了解下
    2020-12-12
  • SpringBoot 緩存 Caffeine使用解析

    SpringBoot 緩存 Caffeine使用解析

    這篇文章主要介紹了SpringBoot 緩存 Caffeine使用解析,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-09-09
  • Java全面解析IO流相關(guān)知識(shí)

    Java全面解析IO流相關(guān)知識(shí)

    這篇文章主要介紹了IO流相關(guān)知識(shí),包括File,字節(jié)流,字符流,特殊操作流(標(biāo)準(zhǔn)輸入流,標(biāo)準(zhǔn)輸出流,對(duì)象序列化與反序列化,properties與IO流結(jié)合)相關(guān)知識(shí)的總結(jié)
    2021-08-08
  • SpringBoot中定時(shí)任務(wù)的使用方法解析

    SpringBoot中定時(shí)任務(wù)的使用方法解析

    這篇文章主要介紹了SpringBoot中定時(shí)任務(wù)的使用方法解析,@EnableScheduling?注解,它的作用是發(fā)現(xiàn)注解?@Scheduled的任務(wù)并由后臺(tái)執(zhí)行,沒(méi)有它的話將無(wú)法執(zhí)行定時(shí)任務(wù),需要的朋友可以參考下
    2024-01-01

最新評(píng)論