Skip to content

Commit 890f678

Browse files
committed
springboot_filter
1 parent 5205184 commit 890f678

5 files changed

Lines changed: 186 additions & 0 deletions

File tree

springboot_filter/pom.xml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>springboot_chowder</artifactId>
7+
<groupId>com.oven</groupId>
8+
<version>1.0-SNAPSHOT</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
12+
<artifactId>springboot_filter</artifactId>
13+
14+
<dependencies>
15+
<dependency>
16+
<groupId>org.springframework.boot</groupId>
17+
<artifactId>spring-boot-starter-web</artifactId>
18+
</dependency>
19+
</dependencies>
20+
21+
</project>

springboot_filter/readme.md

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
# springboot炖filter
2+
### 1. 先睹为快
3+
### 2. 实现原理
4+
#### 2.1 新建项目
5+
#### 2.2 创建maven目录结构,以及pom.xml文件
6+
#### 2.3 pom.xml文件中加入依赖
7+
```xml
8+
<parent>
9+
<groupId>org.springframework.boot</groupId>
10+
<artifactId>spring-boot-starter-parent</artifactId>
11+
<version>2.0.5.RELEASE</version>
12+
<relativePath/>
13+
</parent>
14+
```
15+
#### 2.4 pom.xml文件中加入springboot-starter依赖
16+
```xml
17+
<dependencies>
18+
<dependency>
19+
<groupId>org.springframework.boot</groupId>
20+
<artifactId>spring-boot-starter-web</artifactId>
21+
</dependency>
22+
</dependencies>
23+
```
24+
#### 2.5 pom.xml文件中加入maven-springboot打包插件
25+
```xml
26+
<build>
27+
<plugins>
28+
<plugin>
29+
<groupId>org.springframework.boot</groupId>
30+
<artifactId>spring-boot-maven-plugin</artifactId>
31+
</plugin>
32+
</plugins>
33+
</build>
34+
```
35+
#### 2.6 开发启动类
36+
```java
37+
import org.springframework.boot.SpringApplication;
38+
import org.springframework.boot.autoconfigure.SpringBootApplication;
39+
import org.springframework.boot.web.servlet.ServletComponentScan;
40+
41+
@ServletComponentScan
42+
@SpringBootApplication
43+
public class Application {
44+
45+
public static void main(String[] args) {
46+
SpringApplication.run(Application.class, args);
47+
}
48+
49+
}
50+
```
51+
#### 2.7 开发测试接口
52+
```java
53+
import org.springframework.web.bind.annotation.RequestMapping;
54+
import org.springframework.web.bind.annotation.RestController;
55+
56+
@RestController
57+
public class DemoController {
58+
59+
@RequestMapping("/test")
60+
public Object test(String name) {
61+
return "hello " + name;
62+
}
63+
64+
@RequestMapping("/test2")
65+
public Object test2(String name) {
66+
return "hello " + name;
67+
}
68+
69+
}
70+
```
71+
#### 2.8 开发过滤器
72+
```java
73+
import javax.servlet.*;
74+
import javax.servlet.annotation.WebFilter;
75+
import java.io.IOException;
76+
77+
@WebFilter(filterName = "myFilter", urlPatterns = "/test/*")
78+
public class MyFilter implements Filter {
79+
80+
@Override
81+
public void init(FilterConfig filterConfig) {
82+
System.out.println("初始化过滤器。。。");
83+
}
84+
85+
@Override
86+
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
87+
System.out.println("进入到过滤器。。。");
88+
String name = servletRequest.getParameter("name");
89+
if ("Oven".equals(name)) {
90+
filterChain.doFilter(servletRequest, servletResponse);
91+
}
92+
}
93+
94+
@Override
95+
public void destroy() {
96+
System.out.println("过滤器销毁。。。");
97+
}
98+
99+
}
100+
```
101+
#### 2.9 编译打包运行
102+
### 3. 应用场景
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.oven;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
import org.springframework.boot.web.servlet.ServletComponentScan;
6+
7+
@ServletComponentScan
8+
@SpringBootApplication
9+
public class Application {
10+
11+
public static void main(String[] args) {
12+
SpringApplication.run(Application.class, args);
13+
}
14+
15+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.oven.controller;
2+
3+
import org.springframework.web.bind.annotation.RequestMapping;
4+
import org.springframework.web.bind.annotation.RestController;
5+
6+
@RestController
7+
public class DemoController {
8+
9+
@RequestMapping("/test")
10+
public Object test(String name) {
11+
return "hello " + name;
12+
}
13+
14+
@RequestMapping("/test2")
15+
public Object test2(String name) {
16+
return "hello " + name;
17+
}
18+
19+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.oven.filter;
2+
3+
import javax.servlet.*;
4+
import javax.servlet.annotation.WebFilter;
5+
import java.io.IOException;
6+
7+
@WebFilter(filterName = "myFilter", urlPatterns = "/test/*")
8+
public class MyFilter implements Filter {
9+
10+
@Override
11+
public void init(FilterConfig filterConfig) {
12+
System.out.println("初始化过滤器。。。");
13+
}
14+
15+
@Override
16+
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
17+
System.out.println("进入到过滤器。。。");
18+
String name = servletRequest.getParameter("name");
19+
if ("Oven".equals(name)) {
20+
filterChain.doFilter(servletRequest, servletResponse);
21+
}
22+
}
23+
24+
@Override
25+
public void destroy() {
26+
System.out.println("过滤器销毁。。。");
27+
}
28+
29+
}

0 commit comments

Comments
 (0)