Spring Boot 过滤器测试通过
This commit is contained in:
25
.gitignore
vendored
Normal file
25
.gitignore
vendored
Normal file
@@ -0,0 +1,25 @@
|
||||
/target/
|
||||
!.mvn/wrapper/maven-wrapper.jar
|
||||
|
||||
### STS ###
|
||||
.apt_generated
|
||||
.classpath
|
||||
.factorypath
|
||||
.project
|
||||
.settings
|
||||
.springBeans
|
||||
.sts4-cache
|
||||
|
||||
### IntelliJ IDEA ###
|
||||
.idea
|
||||
*.iws
|
||||
*.iml
|
||||
*.ipr
|
||||
|
||||
### NetBeans ###
|
||||
/nbproject/private/
|
||||
/build/
|
||||
/nbbuild/
|
||||
/dist/
|
||||
/nbdist/
|
||||
/.nb-gradle/
|
||||
56
pom.xml
Normal file
56
pom.xml
Normal file
@@ -0,0 +1,56 @@
|
||||
<?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>top.fjy8018</groupId>
|
||||
<artifactId>springboot</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>springboot</name>
|
||||
<description>Demo project for Spring Boot</description>
|
||||
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>2.0.3.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>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,14 @@
|
||||
package top.fjy8018.springboot;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.boot.web.servlet.ServletComponentScan;
|
||||
|
||||
@ServletComponentScan
|
||||
@SpringBootApplication
|
||||
public class SpringbootApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(SpringbootApplication.class, args);
|
||||
}
|
||||
}
|
||||
11
src/main/java/top/fjy8018/springboot/constant/LoginCons.java
Normal file
11
src/main/java/top/fjy8018/springboot/constant/LoginCons.java
Normal file
@@ -0,0 +1,11 @@
|
||||
package top.fjy8018.springboot.constant;
|
||||
|
||||
/**
|
||||
* @author F嘉阳
|
||||
* @date 2018-07-21 09:53
|
||||
*/
|
||||
public interface LoginCons {
|
||||
String LOGIN_USER_NAME = "admin";
|
||||
|
||||
String SESSION_ATTRIBUTE = "user";
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
package top.fjy8018.springboot.controller;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.ResponseBody;
|
||||
import top.fjy8018.springboot.constant.LoginCons;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpSession;
|
||||
|
||||
/**
|
||||
* @author F嘉阳
|
||||
* @date 2018-07-21 09:48
|
||||
*/
|
||||
@Slf4j
|
||||
@Controller
|
||||
public class LoginDemo {
|
||||
|
||||
@Resource
|
||||
HttpServletRequest request;
|
||||
|
||||
@ResponseBody
|
||||
@RequestMapping("/loginPage")
|
||||
public String loginPage() {
|
||||
return "登录页";
|
||||
}
|
||||
|
||||
@RequestMapping("/login")
|
||||
public String toLogin(@RequestParam String username) {
|
||||
HttpSession session = request.getSession();
|
||||
StringBuilder url = new StringBuilder()
|
||||
.append("http://")
|
||||
.append(request.getServerName())
|
||||
.append(":")
|
||||
.append(request.getServerPort())
|
||||
.append(request.getContextPath());
|
||||
log.info("【toLogin】URL:{}", url);
|
||||
|
||||
if (username.equals(LoginCons.LOGIN_USER_NAME)) {
|
||||
session.setAttribute(LoginCons.SESSION_ATTRIBUTE, username);
|
||||
return "redirect:" + url + "/loginsuccess";
|
||||
} else {
|
||||
return "redirect:" + url + "/loginfail";
|
||||
}
|
||||
}
|
||||
|
||||
@RequestMapping("/loginsuccess")
|
||||
@ResponseBody
|
||||
public String loginSuccess() {
|
||||
return "登录成功";
|
||||
}
|
||||
|
||||
@RequestMapping("/loginfail")
|
||||
@ResponseBody
|
||||
public String loginFail() {
|
||||
return "登录失败";
|
||||
}
|
||||
|
||||
@RequestMapping("/home/index")
|
||||
@ResponseBody
|
||||
public String toIndex() {
|
||||
return "index";
|
||||
}
|
||||
|
||||
@RequestMapping("/logout")
|
||||
public String logout() {
|
||||
HttpSession session = request.getSession();
|
||||
session.removeAttribute(LoginCons.SESSION_ATTRIBUTE);
|
||||
StringBuilder url = new StringBuilder()
|
||||
.append("http://")
|
||||
.append(request.getServerName())
|
||||
.append(":")
|
||||
.append(request.getServerPort())
|
||||
.append(request.getContextPath());
|
||||
log.info("【logout】用户登出");
|
||||
return "redirect:" + url + "/loginPage";
|
||||
}
|
||||
}
|
||||
91
src/main/java/top/fjy8018/springboot/filter/LoginFilter.java
Normal file
91
src/main/java/top/fjy8018/springboot/filter/LoginFilter.java
Normal file
@@ -0,0 +1,91 @@
|
||||
package top.fjy8018.springboot.filter;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import top.fjy8018.springboot.constant.LoginCons;
|
||||
|
||||
import javax.servlet.*;
|
||||
import javax.servlet.annotation.WebFilter;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import javax.servlet.http.HttpSession;
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* @author F嘉阳
|
||||
* @date 2018-07-21 09:51
|
||||
*/
|
||||
@Slf4j
|
||||
@WebFilter(filterName = "sessionFilter", urlPatterns = "/*")
|
||||
public class LoginFilter implements Filter {
|
||||
|
||||
/**
|
||||
* 标示符:表示当前用户未登录(可根据自己项目需要改为json样式)
|
||||
*/
|
||||
String logout = "您还未登录";
|
||||
|
||||
/**
|
||||
* 不需要登录就可以访问的路径(比如:注册登录等)
|
||||
*/
|
||||
private String[] includeUrls = new String[]{"/loginPage", "/login", "/loginfail"};
|
||||
|
||||
@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;
|
||||
HttpSession session = request.getSession();
|
||||
String uri = request.getRequestURI();
|
||||
|
||||
log.info("【doFilter】filterURI:{}", uri);
|
||||
|
||||
//是否需要过滤
|
||||
boolean needFilter = isNeedFilter(uri);
|
||||
|
||||
|
||||
//不需要过滤直接传给下一个过滤器
|
||||
if (!needFilter) {
|
||||
filterChain.doFilter(servletRequest, servletResponse);
|
||||
} else { //需要过滤器
|
||||
// session中包含user对象,则是登录状态
|
||||
if (session != null && session.getAttribute(LoginCons.SESSION_ATTRIBUTE) != null) {
|
||||
log.info("【doFilter】user:{}", session.getAttribute(LoginCons.SESSION_ATTRIBUTE));
|
||||
filterChain.doFilter(request, response);
|
||||
} else {
|
||||
String requestType = request.getHeader("X-Requested-With");
|
||||
//判断是否是ajax请求
|
||||
if (requestType != null && "XMLHttpRequest".equals(requestType)) {
|
||||
response.getWriter().write(this.logout);
|
||||
} else {
|
||||
//重定向到登录页(需要在static文件夹下建立此html文件)
|
||||
response.sendRedirect(request.getContextPath() + "/loginPage");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void destroy() {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断是否需要过滤
|
||||
*
|
||||
* @param uri
|
||||
* @return
|
||||
*/
|
||||
private boolean isNeedFilter(String uri) {
|
||||
|
||||
for (String includeUrl : includeUrls) {
|
||||
if (includeUrl.equals(uri)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
0
src/main/resources/application.properties
Normal file
0
src/main/resources/application.properties
Normal file
@@ -0,0 +1,16 @@
|
||||
package top.fjy8018.springboot;
|
||||
|
||||
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 SpringbootApplicationTests {
|
||||
|
||||
@Test
|
||||
public void contextLoads() {
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user