免責事項:この記事はブロガーオリジナル記事です、続くBY-SAのCC 4.0を著作権契約、複製、元のソースのリンクと、この文を添付してください。
状況:
その他の地域のカスタム例外傍受、ここでしか問題を見つけるために、数日間見て、不自由されているが、フィルターが動作しません。
私がやりたい理由は、本当に怖いです
インターネットはその切片の多くではない、と私は長い時間が傍受することができたが、スロー傍受した後、いないカスタム例外500であります
しかし、このプロセスはまた、次のように傍受方式があり、変装するカスタム例外を書くことに再びスローさを投げパックされたことができます
package com.read.data.bi_statistics.cms.exception;
import org.springframework.boot.autoconfigure.web.ErrorProperties;
import org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController;
import org.springframework.boot.autoconfigure.web.servlet.error.ErrorViewResolver;
import org.springframework.boot.web.servlet.error.DefaultErrorAttributes;
import org.springframework.boot.web.servlet.error.ErrorAttributes;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @author: tianyong
* @time: 2019/8/21 10:46
* @description:
*/
@RestControllerAdvice
public class ChainExceptionHandler extends BasicErrorController {
public ChainExceptionHandler() {
super(new DefaultErrorAttributes(), new ErrorProperties());
}
public ChainExceptionHandler(ErrorAttributes errorAttributes, ErrorProperties errorProperties) {
super(errorAttributes, errorProperties);
}
public ChainExceptionHandler(ErrorAttributes errorAttributes, ErrorProperties errorProperties, List<ErrorViewResolver> errorViewResolvers) {
super(errorAttributes, errorProperties, errorViewResolvers);
}
/**
* @author: tianyong
* @time: 2019/8/21 10:46
* @description:处理传过来的异常(解决自定义全局异常捕获不到的问题)
*/
@Override
@RequestMapping(produces = {MediaType.APPLICATION_JSON_VALUE})
public ResponseEntity<Map<String, Object>> error(HttpServletRequest request) {
Map<String, Object> body = getErrorAttributes(request, isIncludeStackTrace(request, MediaType.ALL));
HttpStatus status = getStatus(request);
String code=body.get("status").toString();
String message = body.get("message").toString();
Map<String,Object> map = new HashMap<String,Object>();
map.put("code",code);
map.put("msg",message);
return new ResponseEntity<Map<String, Object>>(map, status);
}
}
私は、エラーステータスコードのための内部コードを持っているので、スローされた例外のエラー・コードは、最初ので、ここでの傍受後に来る、来ます
私たちは、上記のインターセプトプログラムを使用していませんでした
package com.read.data.bi_statistics.cms.exception;
import com.read.data.bi_statistics.cms.data.enums.CodeInfo;
import com.read.data.bi_statistics.cms.entity.ReturnBone;
import org.springframework.boot.web.servlet.error.ErrorController;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;
import static com.read.data.bi_statistics.authority.utils.JwtUtil.checkChar;
import static com.read.data.bi_statistics.cms.common.Utils.error;
/**
* @author: tianyong
* @Time: 2019/7/15 15:54
* @description:状态码配置类
*/
@Controller
public class StatusCodeExceptionHandler implements ErrorController {
/**
* @author: tianyong
* @time: 2019/7/16 11:36
* @description:指定跳转路径/error
*/
@Override
public String getErrorPath() {
return "/error";
}
/**
* @author: tianyong
* @time: 2019/7/16 11:37
* @description:处理错误状态码
*/
@RequestMapping("/error")
public String handleError(HttpServletRequest request) {
Integer statusCode = (Integer) request.getAttribute("javax.servlet.error.status_code");
Exception attribute = (Exception)request.getAttribute("javax.servlet.error.exception");
if(statusCode == 400) {
return "/400";
}else if(statusCode == 401) {
return "/401";
}else if(statusCode == 403) {
return "/403";
}else if(statusCode == 404) {
return "/404";
}else if(statusCode == 405) {
return "/405";
}else if(statusCode == 500) {
//处理全局异常无法捕获过滤器中自定义异常问题
String message = attribute.getCause().getMessage();
if(checkChar(message)){
return "/401";
}else{
return "/500";
}
}else{
return "/500";
}
}
/**
* @author: tianyong
* @time: 2019/7/15 16:14
* @description:400 请求无效
*/
@ResponseBody
@RequestMapping("/400")
public ReturnBone error400 (){
return error(CodeInfo.STATUS_CODE_400);
}
/**
* @author: tianyong
* @time: 2019/8/15 14:16
* @description:401 用户认证失败
*/
@ResponseBody
@RequestMapping("/401")
public ReturnBone error401 (){
return error(CodeInfo.STATUS_CODE_401);
}
/**
* @author: tianyong
* @time: 2019/7/15 16:14
* @description:403 禁止访问
*/
@ResponseBody
@RequestMapping("/403")
public ReturnBone error403 (){
return error(CodeInfo.STATUS_CODE_403);
}
/**
* @author: tianyong
* @time: 2019/7/15 16:14
* @description:404 请求的网页不存在
*/
@ResponseBody
@RequestMapping("/404")
public ReturnBone error404 (){
return error(CodeInfo.STATUS_CODE_404);
}
/**
* @author: tianyong
* @time: 2019/7/15 16:14
* @description:405 资源被禁止
*/
@ResponseBody
@RequestMapping("/405")
public ReturnBone error405 (){
return error(CodeInfo.STATUS_CODE_405);
}
/**
* @author: tianyong
* @time: 2019/7/15 16:14
* @description:500 内部服务器错误,请联系管理员
*/
@ResponseBody
@RequestMapping("/500")
public ReturnBone error500 (){
return error(CodeInfo.STATUS_CODE_500);
}
}
私が最後のプログラムが使用される:最初のコードのブロック、500エラー、及びI及びテキストでスローカスタム例外によれば、通常と異常500との間の区別
そして、限りの文字は401をジャンプがあるので、特定の区別をしませんでしたが、あなたはまた、特定のテキストメッセージ、その後、カプセル化スローカスタム例外に基づくことができます
if(authorization == null){
loggers.error("获取token失败!");
throw new AuthenticationException("获取token失败!");
}
または方法があり、私はいくつかの他の記事は、フィルタに対応していない参照が、処理要求の内部迎撃 - >フィルタ - >インターセプタは、アクセス順序であります