From f586fa55b9c3f011f5cc4a52813714e0cf63ed8c Mon Sep 17 00:00:00 2001
From: xwolf <2324808561@qq.com>
Date: Tue, 27 Mar 2018 14:00:46 +0800
Subject: [PATCH] =?UTF-8?q?themeleaf=E9=85=8D=E7=BD=AE;=E9=94=99=E8=AF=AF?=
=?UTF-8?q?=E6=8B=A6=E6=88=AA=E5=99=A8?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
README.md | 4 ++
pom.xml | 19 ++++++-
.../xwolf/boot/config/InterceptorConfig.java | 16 +++++-
.../boot/controller/ErrorController.java | 34 +++++++++++++
.../boot/interceptor/ErrorInterceptor.java | 50 +++++++++++++++++++
src/main/resources/application.yml | 23 +++++++--
src/main/resources/templates/302.html | 13 +++++
src/main/resources/templates/400.html | 13 +++++
src/main/resources/templates/404.html | 13 +++++
src/main/resources/templates/500.html | 13 +++++
10 files changed, 191 insertions(+), 7 deletions(-)
create mode 100644 src/main/java/com/xwolf/boot/controller/ErrorController.java
create mode 100644 src/main/java/com/xwolf/boot/interceptor/ErrorInterceptor.java
create mode 100644 src/main/resources/templates/302.html
create mode 100644 src/main/resources/templates/400.html
create mode 100644 src/main/resources/templates/404.html
create mode 100644 src/main/resources/templates/500.html
diff --git a/README.md b/README.md
index 8c62368..2371688 100644
--- a/README.md
+++ b/README.md
@@ -5,3 +5,7 @@
>* Hibernate validator 集成
>* jackson 日期时间格式化
>* swagger2 api管理
+>* 自定义Filter,避免XSS攻击和SQL注入攻击
+>* 自定义AOP配合redis解决重复提交问题
+>* 自定义拦截器,拦截referer来避免CSRF攻击
+>* 自定义拦截器根据不同的响应状态码跳转不同的页面
\ No newline at end of file
diff --git a/pom.xml b/pom.xml
index 494efcb..ad77f5f 100644
--- a/pom.xml
+++ b/pom.xml
@@ -14,7 +14,7 @@
org.springframework.boot
spring-boot-starter-parent
- 1.5.1.RELEASE
+ 1.5.9.RELEASE
@@ -25,6 +25,7 @@
+
org.springframework.boot
spring-boot-starter-aop
@@ -44,6 +45,22 @@
spring-boot-starter-test
test
+
+
+ org.springframework.boot
+ spring-boot-starter-data-redis
+
+
+
+ org.springframework.boot
+ spring-boot-starter-thymeleaf
+
+
+
+ net.sourceforge.nekohtml
+ nekohtml
+ 1.9.17
+
org.apache.commons
diff --git a/src/main/java/com/xwolf/boot/config/InterceptorConfig.java b/src/main/java/com/xwolf/boot/config/InterceptorConfig.java
index 54de9ac..8b58240 100644
--- a/src/main/java/com/xwolf/boot/config/InterceptorConfig.java
+++ b/src/main/java/com/xwolf/boot/config/InterceptorConfig.java
@@ -1,6 +1,7 @@
package com.xwolf.boot.config;
import com.xwolf.boot.interceptor.CsrfInterceptor;
+import com.xwolf.boot.interceptor.ErrorInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
@@ -24,8 +25,21 @@ public class InterceptorConfig extends WebMvcConfigurerAdapter {
return new CsrfInterceptor();
}
+ /**
+ * 错误拦截
+ * @return
+ */
+ @Bean
+ ErrorInterceptor errorInterceptor(){
+ return new ErrorInterceptor();
+ }
+
@Override
public void addInterceptors(InterceptorRegistry registry) {
- registry.addInterceptor(csrfInterceptor()).addPathPatterns("/");
+
+ registry.addInterceptor(csrfInterceptor()).addPathPatterns("/**");
+
+ registry.addInterceptor(errorInterceptor()).addPathPatterns("/**");
+ super.addInterceptors(registry);
}
}
diff --git a/src/main/java/com/xwolf/boot/controller/ErrorController.java b/src/main/java/com/xwolf/boot/controller/ErrorController.java
new file mode 100644
index 0000000..78d8cf1
--- /dev/null
+++ b/src/main/java/com/xwolf/boot/controller/ErrorController.java
@@ -0,0 +1,34 @@
+package com.xwolf.boot.controller;
+
+import org.springframework.stereotype.Controller;
+import org.springframework.web.bind.annotation.GetMapping;
+
+/**
+ * 对应响应码跳转
+ * @author xwolf
+ * @version 1.0
+ * @since 1.8
+ */
+@Controller
+public class ErrorController {
+
+ @GetMapping("500")
+ public String error500(){
+ return "500";
+ }
+
+ @GetMapping("404")
+ public String error404(){
+ return "404";
+ }
+
+ @GetMapping("400")
+ public String error400(){
+ return "400";
+ }
+
+ @GetMapping("302")
+ public String error302(){
+ return "302";
+ }
+}
diff --git a/src/main/java/com/xwolf/boot/interceptor/ErrorInterceptor.java b/src/main/java/com/xwolf/boot/interceptor/ErrorInterceptor.java
new file mode 100644
index 0000000..00ef6dc
--- /dev/null
+++ b/src/main/java/com/xwolf/boot/interceptor/ErrorInterceptor.java
@@ -0,0 +1,50 @@
+package com.xwolf.boot.interceptor;
+
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.web.servlet.HandlerInterceptor;
+import org.springframework.web.servlet.ModelAndView;
+
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+/**
+ * 不同的状态码跳转不同的页面
+ * @author xwolf
+ * @version 1.0
+ * @since 1.8
+ */
+@Slf4j
+public class ErrorInterceptor implements HandlerInterceptor {
+
+
+ @Override
+ public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
+ return true;
+ }
+
+ @Override
+ public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {
+ String uri = httpServletRequest.getRequestURI();
+ int status = httpServletResponse.getStatus();
+ log.info("uri={},status={}",uri,status);
+ switch (status){
+ case 500:
+ modelAndView.setViewName("500");
+ break;
+ case 400:
+ modelAndView.setViewName("400");
+ break;
+ case 404:
+ modelAndView.setViewName("404");
+ break;
+ case 302:
+ modelAndView.setViewName("302");
+ break;
+ }
+ }
+
+ @Override
+ public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {
+
+ }
+}
diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml
index f40755a..8995416 100644
--- a/src/main/resources/application.yml
+++ b/src/main/resources/application.yml
@@ -14,6 +14,19 @@ spring:
date-format: YYYY-MM-DD
joda-date-time-format: YYYY-MM-DD HH:mm:ss
+ redis:
+ database: 0
+ host: localhost
+ password:
+ port: 6379
+
+ thymeleaf:
+ mode: HTML5
+ encoding: UTF-8
+ prefix: classpath:/templates/
+ suffix: .html
+ cache: false
+ content-type: text/html
mybatis:
mapper-locations: classpath:mapper/*.xml
type-aliases-package: com.xwolf.boot.entity
@@ -21,8 +34,8 @@ mybatis:
config-location: classpath:mybatis.xml
logging:
- config: classpath:logback.xml
-
+ level: debug
+ #config: classpath:logback.xml
server:
port: 8082
session:
@@ -35,9 +48,9 @@ spring:
profiles: dev
datasource:
driver-class-name: com.mysql.jdbc.Driver
- username: eop
- url: jdbc:mysql://127.0.0.1:3306/wifi2?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&createDatabaseIfNotExist=true
- password: eop@eop
+ username: boot
+ url: jdbc:mysql://127.0.0.1:3306/boot?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&createDatabaseIfNotExist=true
+ password: 123456
type: com.alibaba.druid.pool.DruidDataSource
druid:
filters: config
diff --git a/src/main/resources/templates/302.html b/src/main/resources/templates/302.html
new file mode 100644
index 0000000..1fe6573
--- /dev/null
+++ b/src/main/resources/templates/302.html
@@ -0,0 +1,13 @@
+
+
+
+
+ 302
+
+
+
+
+ 无权限访问.
+
+
+
\ No newline at end of file
diff --git a/src/main/resources/templates/400.html b/src/main/resources/templates/400.html
new file mode 100644
index 0000000..ee7906a
--- /dev/null
+++ b/src/main/resources/templates/400.html
@@ -0,0 +1,13 @@
+
+
+
+
+ 400
+
+
+
+
+ 请求参数错误.
+
+
+
\ No newline at end of file
diff --git a/src/main/resources/templates/404.html b/src/main/resources/templates/404.html
new file mode 100644
index 0000000..2e4e3a3
--- /dev/null
+++ b/src/main/resources/templates/404.html
@@ -0,0 +1,13 @@
+
+
+
+
+ 404
+
+
+
+
+ 访问的资源不存在
+
+
+
\ No newline at end of file
diff --git a/src/main/resources/templates/500.html b/src/main/resources/templates/500.html
new file mode 100644
index 0000000..f7a48c7
--- /dev/null
+++ b/src/main/resources/templates/500.html
@@ -0,0 +1,13 @@
+
+
+
+
+ 500
+
+
+
+
+ 服务器内部错误.
+
+
+
\ No newline at end of file