springboot中filter异常处理

 

package com.pocket.payment.controller.web;

import cn.hutool.json.JSONUtil;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.web.ErrorProperties;
import org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController;
import org.springframework.boot.web.servlet.error.DefaultErrorAttributes;
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.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @author: LL
 * @date: 2019/10/31 11:49
 * @description:
 */
@Slf4j
@Api(description = "filter错误处理")
@RestController
public class ErrorController extends BasicErrorController {

  public ErrorController() {
    super(new DefaultErrorAttributes(), new ErrorProperties());
  }

  @RequestMapping(produces = {MediaType.APPLICATION_JSON_VALUE})
  @Override
  public ResponseEntity error(HttpServletRequest request) {
    Map body =
        getErrorAttributes(request, isIncludeStackTrace(request, MediaType.ALL));
    log.info(JSONUtil.toJsonPrettyStr(body));
    // 错误信息
    String message = body.get("message").toString();
    if (message.contains("ShiroRealm")) {
      return new ResponseEntity<>("会话失效,请重新登录", HttpStatus.UNAUTHORIZED);
    }
    return ResponseEntity.ok(body);
  }
}

 

你可能感兴趣的:(springboot中filter异常处理)