package com.demo.exception;
import com.demo.model.RestResp;
import org.apache.commons.collections.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.web.ErrorAttributes;
import org.springframework.boot.autoconfigure.web.ErrorController;
import org.springframework.boot.autoconfigure.web.ErrorProperties;
import org.springframework.boot.autoconfigure.web.ServerProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Collections;
import java.util.Map;
/**
* Created by YangFan on 2016/11/28 下午2:59.
* <p/>
*/
@Controller
@RequestMapping("/error")
@EnableConfigurationProperties({ServerProperties.class})
public class DemoErrorController implements ErrorController {
private ErrorAttributes errorAttributes;
@Autowired
private ServerProperties serverProperties;
/**
* 初始化ExceptionController
*
* @param errorAttributes
*/
@Autowired
public DemoErrorController(ErrorAttributes errorAttributes) {
Assert.notNull(errorAttributes, "ErrorAttributes must not be null");
this.errorAttributes = errorAttributes;
}
@RequestMapping(produces = "text/html")
public ModelAndView errorHtml(HttpServletRequest request,
HttpServletResponse response) {
HttpStatus status = getStatus(request);
Map<String, Object> model = Collections.unmodifiableMap(getErrorAttributes(
request, isIncludeStackTrace(request, MediaType.TEXT_HTML)));
response.setStatus(status.value());
return new ModelAndView("error", model);
}
/**
* 定义500的错误JSON信息
*
* @param request
* @return
*/
@RequestMapping()
@ResponseBody
public ResponseEntity<RestResp> error(HttpServletRequest request) {
Map<String, Object> body = getErrorAttributes(request,
isIncludeStackTrace(request, MediaType.TEXT_HTML));
String path = MapUtils.getString(body, "path");
String message = MapUtils.getString(body, "message");
RestResp restResp = new RestResp(RestResp.ERROR, message, path);
if (MapUtils.getInteger(body, "status") == 404) {
restResp = new RestResp(RestResp.NOT_FOUND, "访问的路径不存在", path);
}
return new ResponseEntity<>(restResp, HttpStatus.OK);
}
/**
* Determine if the stacktrace attribute should be included.
*
* @param request the source request
* @param produces the media type produced (or {@code MediaType.ALL})
* @return if the stacktrace attribute should be included
*/
protected boolean isIncludeStackTrace(HttpServletRequest request,
MediaType produces) {
ErrorProperties.IncludeStacktrace include = this.serverProperties.getError().getIncludeStacktrace();
if (include == ErrorProperties.IncludeStacktrace.ALWAYS) {
return true;
}
if (include == ErrorProperties.IncludeStacktrace.ON_TRACE_PARAM) {
return getTraceParameter(request);
}
return false;
}
/**
* 获取错误的信息
*
* @param request
* @param includeStackTrace
* @return
*/
private Map<String, Object> getErrorAttributes(HttpServletRequest request,
boolean includeStackTrace) {
RequestAttributes requestAttributes = new ServletRequestAttributes(request);
return this.errorAttributes.getErrorAttributes(requestAttributes,
includeStackTrace);
}
/**
* 是否包含trace
*
* @param request
* @return
*/
private boolean getTraceParameter(HttpServletRequest request) {
String parameter = request.getParameter("trace");
if (parameter == null) {
return false;
}
return !"false".equals(parameter.toLowerCase());
}
/**
* 获取错误编码
*
* @param request
* @return
*/
private HttpStatus getStatus(HttpServletRequest request) {
Integer statusCode = (Integer) request
.getAttribute("javax.servlet.error.status_code");
if (statusCode == null) {
return HttpStatus.INTERNAL_SERVER_ERROR;
}
try {
return HttpStatus.valueOf(statusCode);
} catch (Exception ex) {
return HttpStatus.INTERNAL_SERVER_ERROR;
}
}
/**
* 实现错误路径,暂时无用
*
* @return
* @see ExceptionMvcAutoConfiguration#containerCustomizer()
*/
@Override
public String getErrorPath() {
return "";
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
java+spring-boot-jwt + spring security集成实战项目.zip (34个子文件)
java+spring-boot-jwt + spring security集成实战项目
spring-boot-jwt-master
spring-boot-jwt-master
gradle
wrapper
gradle-wrapper.jar 52KB
gradle-wrapper.properties 231B
src
test
java
com
demo
BootApplicationTests.java 323B
main
resources
mapper
UserMapper.xml 532B
application.yml 570B
java
com
demo
mapper
UserMapper.java 2KB
security
DemoAccessDecisionManager.java 4KB
DemoSecurityInterceptor.java 2KB
DemoInvocationSecurityMetadataSourceService.java 3KB
controller
UserController.java 2KB
jwt
JsonWebTokenAuthentication.java 903B
JsonWebTokenAuthenticationFilter.java 965B
JsonWebTokenSecurityConfig.java 2KB
JsonWebTokenAuthenticationProvider.java 3KB
NoAuthenticationEntryPoint.java 1KB
JsonWebTokenUtility.java 3KB
AuthTokenDetails.java 401B
service
UserService.java 454B
impl
UserServiceImpl.java 1KB
model
RestResp.java 957B
Role.java 179B
User.java 418B
exception
DemoErrorController.java 5KB
WrongPathException.java 257B
dto
AuthTokenDTO.java 261B
util
JsonUtil.java 3KB
SessionUtil.java 670B
BootApplication.java 1KB
conf
WebSecurityConfig.java 1KB
MybatisConfig.java 2KB
gradlew.bat 2KB
build.gradle 1KB
gradlew 5KB
.gitignore 247B
共 34 条
- 1
资源评论
GeekyGuru
- 粉丝: 1601
- 资源: 1099
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功