forked from weiwosuoai/spring-boot-tutorial
-
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
d856bdf
commit cd8bdcf
Showing
8 changed files
with
192 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,43 @@ | ||
<?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> | ||
<parent> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-parent</artifactId> | ||
<version>2.1.2.RELEASE</version> | ||
<relativePath/> <!-- lookup parent from repository --> | ||
</parent> | ||
<groupId>site.exception</groupId> | ||
<artifactId>spring-boot-interceptor</artifactId> | ||
<version>0.0.1-SNAPSHOT</version> | ||
<name>spring-boot-interceptor</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> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-maven-plugin</artifactId> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
|
||
</project> |
20 changes: 20 additions & 0 deletions
20
...rceptor/src/main/java/site/exception/springbootinterceptor/Controller/TestController.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,20 @@ | ||
package site.exception.springbootinterceptor.Controller; | ||
|
||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
/** | ||
* @author www.exception.site (exception 教程网) | ||
* @date 2019/2/28 | ||
* @time 19:21 | ||
* @discription | ||
**/ | ||
@RestController | ||
public class TestController { | ||
|
||
@GetMapping("/admin/test") | ||
public String test() { | ||
System.out.println("test"); | ||
return "success"; | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
.../src/main/java/site/exception/springbootinterceptor/SpringBootInterceptorApplication.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,13 @@ | ||
package site.exception.springbootinterceptor; | ||
|
||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
|
||
@SpringBootApplication | ||
public class SpringBootInterceptorApplication { | ||
|
||
public static void main(String[] args) { | ||
SpringApplication.run(SpringBootInterceptorApplication.class, args); | ||
} | ||
|
||
} |
26 changes: 26 additions & 0 deletions
26
...t-interceptor/src/main/java/site/exception/springbootinterceptor/config/WebMvcConfig.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,26 @@ | ||
package site.exception.springbootinterceptor.config; | ||
|
||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.web.servlet.config.annotation.InterceptorRegistry; | ||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; | ||
import site.exception.springbootinterceptor.interceptor.LoginValidationInterceptor; | ||
|
||
/** | ||
* @author www.exception.site (exception 教程网) | ||
* @date 2019/2/27 | ||
* @time 12:17 | ||
* @discription | ||
**/ | ||
@Configuration | ||
public class WebMvcConfig implements WebMvcConfigurer { | ||
|
||
/** | ||
* 配置拦截器 | ||
* @param interceptorRegistry | ||
*/ | ||
@Override | ||
public void addInterceptors(InterceptorRegistry interceptorRegistry) { | ||
// 拦截所有 /admin/** 的访问地址 | ||
interceptorRegistry.addInterceptor(new LoginValidationInterceptor()).addPathPatterns("/admin/**"); | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
spring-boot-interceptor/src/main/java/site/exception/springbootinterceptor/entity/User.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,30 @@ | ||
package site.exception.springbootinterceptor.entity; | ||
|
||
import java.io.Serializable; | ||
|
||
/** | ||
* @author www.exception.site (exception 教程网) | ||
* @date 2019/3/1 | ||
* @time 21:15 | ||
* @discription | ||
**/ | ||
public class User implements Serializable { | ||
private String usernmae; | ||
private String password; | ||
|
||
public String getUsernmae() { | ||
return usernmae; | ||
} | ||
|
||
public void setUsernmae(String usernmae) { | ||
this.usernmae = usernmae; | ||
} | ||
|
||
public String getPassword() { | ||
return password; | ||
} | ||
|
||
public void setPassword(String password) { | ||
this.password = password; | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
...ain/java/site/exception/springbootinterceptor/interceptor/LoginValidationInterceptor.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,43 @@ | ||
package site.exception.springbootinterceptor.interceptor; | ||
|
||
import org.springframework.lang.Nullable; | ||
import org.springframework.web.servlet.HandlerInterceptor; | ||
import org.springframework.web.servlet.ModelAndView; | ||
import site.exception.springbootinterceptor.entity.User; | ||
|
||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
|
||
/** | ||
* @author www.exception.site (exception 教程网) | ||
* @date 2019/2/27 | ||
* @time 12:19 | ||
* @discription 登录验证拦截器 | ||
**/ | ||
public class LoginValidationInterceptor implements HandlerInterceptor { | ||
|
||
@Override | ||
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { | ||
System.out.println("preHandle ..."); | ||
User user = (User) request.getSession().getAttribute("user"); | ||
if (user == null) { | ||
// 若未登录,重定向到登录页面 | ||
response.sendRedirect("login.html"); | ||
return false; | ||
} | ||
// 若已登录,继续往下执行 | ||
return true; | ||
} | ||
|
||
@Override | ||
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable ModelAndView modelAndView) throws Exception { | ||
System.out.println("postHandle ..."); | ||
// controller 方法调用完毕后,执行此方法 | ||
} | ||
|
||
@Override | ||
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable Exception ex) throws Exception { | ||
System.out.println("afterCompletion ..."); | ||
// 页面渲染完成后调用此方法, 一般用来清除某些资源等 | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
spring-boot-interceptor/src/main/resources/application.properties
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 @@ | ||
server.port=8081 |
16 changes: 16 additions & 0 deletions
16
...test/java/site/exception/springbootinterceptor/SpringBootInterceptorApplicationTests.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 site.exception.springbootinterceptor; | ||
|
||
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 SpringBootInterceptorApplicationTests { | ||
|
||
@Test | ||
public void contextLoads() { | ||
} | ||
|
||
} |