1.创建项目
2.点击Next
3.选择web
4.pom如下:
<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.take</groupId>
<artifactId>filter-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>filter-demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<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>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
5.创建controller
包,包下创建两个类:HelloController
,WorldController
package com.take.filterdemo.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@RequestMapping("hello")
public String hello(){
return "hello";
}
}
package com.take.filterdemo.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class WorldController {
@RequestMapping("world")
public String world(){
return "world";
}
}
6.添加application.yml
,加入配置
server:
port: 8080
spring:
main:
allow-bean-definition-overriding: true
7.创建filter
包,包下创建HelloFilter
类,并实现Fillter
接口,注意是javax.servlet.*;
下的包
只拦截了/hello
的资源,并没有拦截/world
资源
package com.take.filterdemo.filter;
import org.springframework.stereotype.Component;
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* urlPatterns: 拦截的路径,可以写多个,如 @WebFilter(urlPatterns = {"/hello","/world"},filterName = "helloFilter")
*/
@Component
@WebFilter(urlPatterns = "/hello",filterName = "helloFilter")
public class HelloFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
HttpServletRequest request=(HttpServletRequest)servletRequest;
HttpServletResponse response = (HttpServletResponse) servletResponse;
//逻辑省略。。。
// filterChain.doFilter(servletRequest,servletResponse);//放行
}
@Override
public void destroy() {
}
}
8.在启动类上加上注解@ServletComponentScan
,会把过滤器扫描到spring
中
package com.take.filterdemo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
@SpringBootApplication
@ServletComponentScan
public class FilterDemoApplication {
public static void main(String[] args) {
SpringApplication.run(FilterDemoApplication.class, args);
}
}
9.启动访问
/hello
资源被拦截
/world
资源访问成功
参考链接:
https://www.cnblogs.com/ideal-20/p/11496091.html