SpringBoot * Filter的使用方法详解

作者:玉天恒 时间:2023-06-09 07:33:59 

前言:

最新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>
 <h4>hello theng</h4>
 <h3>filter success</h3>
</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的使用方法详解

来源:https://www.cnblogs.com/tianhengblogs/p/9781734.html

标签:Spring,Boot, , ,Filter
0
投稿

猜你喜欢

  • C# Dynamic关键字之:调用属性、方法、字段的实现方法

    2022-09-22 04:23:39
  • Android Framework如何实现Binder

    2021-12-09 03:54:20
  • Java实现中国象棋的示例代码

    2021-08-10 21:56:03
  • java WebSocket 服务端实现代码

    2022-09-28 00:30:52
  • java IO流文件的读写具体实例

    2023-08-21 04:44:34
  • android studio library 模块中正确引用aar的实例讲解

    2021-09-28 12:46:52
  • Netty实现简易版的RPC框架过程详解

    2023-05-23 23:19:58
  • C#中析构函数、Dispose、Close方法的区别

    2022-06-11 21:20:54
  • Spark JDBC操作MySQL方式详细讲解

    2021-05-24 12:41:51
  • Java8如何从一个Stream中过滤null值

    2022-02-03 08:10:20
  • Java虚拟机JVM性能优化(二):编译器

    2023-12-03 21:21:57
  • Android MVP模式实战教程

    2023-09-05 13:16:54
  • C#开发Winform程序调用存储过程

    2023-01-11 05:23:15
  • Java数组的基本学习教程

    2021-07-14 05:08:00
  • Spring Boot全局异常处理解析

    2023-07-06 07:16:35
  • Android 中自定义Dialog样式的Activity点击空白处隐藏软键盘功能(dialog不消失)

    2022-03-10 12:15:21
  • Android自定义 WebView浏览器

    2023-07-21 00:31:34
  • Android自定义View实现投票进度条

    2022-04-02 13:57:21
  • 深入理解C# DateTime日期格式化

    2021-12-08 04:23:17
  • dialog dismiss时键盘不消失的问题浅析及解决办法

    2021-09-18 10:06:30
  • asp之家 软件编程 m.aspxhome.com