91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

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

發布時間:2020-08-27 22:44:03 來源:腳本之家 閱讀:420 作者:玉天恒 欄目:編程語言

前言:

最新Servlet 3.0攔截器的使用

1.pom.xml添加需要使用的依賴

<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>
    <!-- 打包的名稱 -->
    <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:攔截器名稱
@WebFilter(urlPatterns="/api/*", filterName="loginFilter")
public class LoginFilter implements Filter{

  /*
   * 容器加載完成調用
   * */
  @Override
  public void init(FilterConfig filterConfig) throws ServletException {
    // TODO Auto-generated method stub
    System.out.println("filter init...");
  }
  
  /*
   *  請求被攔截的時候調用
   * */
  @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;
    }
    
  }

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

3.添加測試控制器

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.添加啟動類

package top.ytheng.demo;

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

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

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

5.添加攔截后調整的頁面filter.html

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

6.右鍵項目Run As啟動項目,測試地址

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

另附:

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

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

凭祥市| 丰原市| 肃宁县| 汶川县| 繁昌县| 台南市| 长丰县| 广汉市| 攀枝花市| 敦煌市| 霍林郭勒市| 静宁县| 柏乡县| 迁西县| 洛川县| 射洪县| 瑞安市| 湾仔区| 盐亭县| 明溪县| 霸州市| 普定县| 长垣县| 栾城县| 连州市| 甘泉县| 喀喇沁旗| 巩义市| 西昌市| 资讯| 襄樊市| 白玉县| 乌拉特中旗| 长汀县| 日照市| 眉山市| 天等县| 阳山县| 开化县| 邢台县| 内江市|