forked from wuyouzhuguli/SpringAll
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f36af92
commit cb134d5
Showing
9 changed files
with
209 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<groupId>com.springboot</groupId> | ||
<artifactId>Spring-Boot-Filter-Interceptor</artifactId> | ||
<version>0.0.1-SNAPSHOT</version> | ||
<packaging>jar</packaging> | ||
|
||
<name>Spring-Boot-Filter-Interceptor</name> | ||
<description>Demo project for Spring Boot</description> | ||
|
||
<parent> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-parent</artifactId> | ||
<version>1.5.14.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> | ||
</dependencies> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-maven-plugin</artifactId> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
|
||
|
||
</project> |
11 changes: 11 additions & 0 deletions
11
26.Spring-Boot-Filter-Interceptor/src/main/java/cc/mrbird/Application.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package cc.mrbird; | ||
|
||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
|
||
@SpringBootApplication | ||
public class Application { | ||
public static void main(String[] args) { | ||
SpringApplication.run(Application.class, args); | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
26.Spring-Boot-Filter-Interceptor/src/main/java/cc/mrbird/config/WebConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package cc.mrbird.config; | ||
|
||
import cc.mrbird.filter.TimeFilter; | ||
import cc.mrbird.interceptor.TimeInterceptor; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.web.servlet.FilterRegistrationBean; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.web.servlet.config.annotation.InterceptorRegistry; | ||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
@Configuration | ||
public class WebConfig extends WebMvcConfigurerAdapter { | ||
@Bean | ||
public FilterRegistrationBean timeFilter() { | ||
FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(); | ||
TimeFilter timeFilter = new TimeFilter(); | ||
filterRegistrationBean.setFilter(timeFilter); | ||
|
||
List<String> urlList = new ArrayList<>(); | ||
urlList.add("/*"); | ||
|
||
filterRegistrationBean.setUrlPatterns(urlList); | ||
return filterRegistrationBean; | ||
} | ||
|
||
@Autowired | ||
private TimeInterceptor timeInterceptor; | ||
|
||
@Override | ||
public void addInterceptors(InterceptorRegistry registry) { | ||
registry.addInterceptor(timeInterceptor); | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
26.Spring-Boot-Filter-Interceptor/src/main/java/cc/mrbird/controller/UserController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package cc.mrbird.controller; | ||
|
||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.PathVariable; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@RestController | ||
@RequestMapping("user") | ||
public class UserController { | ||
|
||
@GetMapping("/{id:\\d+}") | ||
public void get(@PathVariable String id) { | ||
System.out.println(id); | ||
// throw new RuntimeException("user not exist"); | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
26.Spring-Boot-Filter-Interceptor/src/main/java/cc/mrbird/filter/TimeFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package cc.mrbird.filter; | ||
|
||
import org.springframework.stereotype.Component; | ||
|
||
import javax.servlet.*; | ||
import javax.servlet.annotation.WebFilter; | ||
import java.io.IOException; | ||
import java.util.Date; | ||
|
||
// @Component | ||
// @WebFilter(urlPatterns = {"/*"}) | ||
public class TimeFilter implements Filter { | ||
@Override | ||
public void init(FilterConfig filterConfig) throws ServletException { | ||
System.out.println("过滤器初始化"); | ||
} | ||
|
||
@Override | ||
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { | ||
System.out.println("开始执行过滤器"); | ||
Long start = new Date().getTime(); | ||
filterChain.doFilter(servletRequest, servletResponse); | ||
System.out.println("【过滤器】耗时 " + (new Date().getTime() - start)); | ||
System.out.println("结束执行过滤器"); | ||
} | ||
|
||
@Override | ||
public void destroy() { | ||
System.out.println("过滤器销毁"); | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
26.Spring-Boot-Filter-Interceptor/src/main/java/cc/mrbird/interceptor/TimeInterceptor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package cc.mrbird.interceptor; | ||
|
||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.method.HandlerMethod; | ||
import org.springframework.web.servlet.HandlerInterceptor; | ||
import org.springframework.web.servlet.ModelAndView; | ||
|
||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
import java.util.Date; | ||
|
||
@Component | ||
public class TimeInterceptor implements HandlerInterceptor { | ||
@Override | ||
public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception { | ||
System.out.println("处理拦截之前"); | ||
httpServletRequest.setAttribute("startTime", new Date().getTime()); | ||
System.out.println(((HandlerMethod) o).getBean().getClass().getName()); | ||
System.out.println(((HandlerMethod) o).getMethod().getName()); | ||
return true; | ||
} | ||
|
||
@Override | ||
public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception { | ||
System.out.println("开始处理拦截"); | ||
Long start = (Long) httpServletRequest.getAttribute("startTime"); | ||
System.out.println("【拦截器】耗时 " + (new Date().getTime() - start)); | ||
} | ||
|
||
@Override | ||
public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception { | ||
System.out.println("处理拦截之后"); | ||
Long start = (Long) httpServletRequest.getAttribute("startTime"); | ||
System.out.println("【拦截器】耗时 " + (new Date().getTime() - start)); | ||
System.out.println("异常信息 " + e); | ||
} | ||
} |
Empty file.
10 changes: 10 additions & 0 deletions
10
26.Spring-Boot-Filter-Interceptor/src/main/resources/resources/error/500.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<title>500</title> | ||
</head> | ||
<body> | ||
系统内部异常 | ||
</body> | ||
</html> |
16 changes: 16 additions & 0 deletions
16
...ring-Boot-Filter-Interceptor/src/test/java/cc/mrbird/cc/mrbird/demo/ApplicationTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package cc.mrbird.cc.mrbird.demo; | ||
|
||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.test.context.junit4.SpringRunner; | ||
|
||
@RunWith(SpringRunner.class) | ||
@SpringBootTest | ||
public class ApplicationTests { | ||
|
||
@Test | ||
public void contextLoads() { | ||
} | ||
|
||
} |