themeleaf配置;错误拦截器

This commit is contained in:
xwolf
2018-03-27 14:00:46 +08:00
parent 7022537b70
commit f586fa55b9
10 changed files with 191 additions and 7 deletions
+4
View File
@@ -5,3 +5,7 @@
>* Hibernate validator 集成
>* jackson 日期时间格式化
>* swagger2 api管理
>* 自定义Filter,避免XSS攻击和SQL注入攻击
>* 自定义AOP配合redis解决重复提交问题
>* 自定义拦截器,拦截referer来避免CSRF攻击
>* 自定义拦截器根据不同的响应状态码跳转不同的页面
+18 -1
View File
@@ -14,7 +14,7 @@
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.1.RELEASE</version>
<version>1.5.9.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
@@ -25,6 +25,7 @@
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
@@ -44,6 +45,22 @@
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!--html-->
<dependency>
<groupId>net.sourceforge.nekohtml</groupId>
<artifactId>nekohtml</artifactId>
<version>1.9.17</version>
</dependency>
<!--commons-->
<dependency>
<groupId>org.apache.commons</groupId>
@@ -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);
}
}
@@ -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";
}
}
@@ -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 {
}
}
+18 -5
View File
@@ -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
+13
View File
@@ -0,0 +1,13 @@
<!DOCTYPE html>
<html lang="en" >
<head>
<meta charset="UTF-8"/>
<title>302</title>
</head>
<body>
<h3>
无权限访问.
</h3>
</body>
</html>
+13
View File
@@ -0,0 +1,13 @@
<!DOCTYPE html>
<html lang="en" >
<head>
<meta charset="UTF-8"/>
<title>400</title>
</head>
<body>
<h3>
请求参数错误.
</h3>
</body>
</html>
+13
View File
@@ -0,0 +1,13 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8"/>
<title>404</title>
</head>
<body>
<h3>
访问的资源不存在
</h3>
</body>
</html>
+13
View File
@@ -0,0 +1,13 @@
<!DOCTYPE html>
<html lang="en" >
<head>
<meta charset="UTF-8" />
<title>500</title>
</head>
<body>
<h3>
服务器内部错误.
</h3>
</body>
</html>