Skip to content

Commit

Permalink
Spring Boot 2.x 配置拦截器
Browse files Browse the repository at this point in the history
  • Loading branch information
weiwosuoai committed Mar 1, 2019
1 parent d856bdf commit cd8bdcf
Show file tree
Hide file tree
Showing 8 changed files with 192 additions and 0 deletions.
43 changes: 43 additions & 0 deletions spring-boot-interceptor/pom.xml
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>
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";
}
}
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);
}

}
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/**");
}
}
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;
}
}
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 ...");
// 页面渲染完成后调用此方法, 一般用来清除某些资源等
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
server.port=8081
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() {
}

}

0 comments on commit cd8bdcf

Please sign in to comment.