服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Java教程 - 详解Spring Security 捕获 filter 层面异常返回我们自定义的内容

详解Spring Security 捕获 filter 层面异常返回我们自定义的内容

2022-12-08 14:52扛麻袋的少年 Java教程

Spring 的异常会转发到 BasicErrorController 中进行异常写入,然后才会返回客户端。所以,我们可以在 BasicErrorController 对 filter异常进行捕获并处理,下面通过本文给大家介绍Spring Security 捕获 filter 层面异常,返回我们自定义的内容,感

通常,我们通过 @ControllerAdvice 和 @ExceptionHandler 来捕获并处理 Controller 层面的异常。但是,filter 是在 controller 层之前的,需要先通过 filter 才能到达 controller 层,此文就介绍一下如何捕获filter层面的异常。

Spring 的异常会转发到 BasicErrorController 中进行异常写入,然后才会返回客户端。所以,我们可以在 BasicErrorControllerfilter异常进行捕获并处理。

所以,我们需要重写BasicErrorController中的error方法。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import com.ddky.mobile.vo.basicVO.ResponseVO;
import org.springframework.boot.autoconfigure.web.ServerProperties;
import org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController;
import org.springframework.boot.web.error.ErrorAttributeOptions;
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;
/**
 * 解决Filter中异常返回值问题
 * @author : lzb
 * @date: 2022-05-10 09:16
 */
@RestController
public class FilterErrorController extends BasicErrorController {
    public FilterErrorController(ServerProperties serverProperties) {
        super(new DefaultErrorAttributes(), serverProperties.getError());
    }
    @Override
    @RequestMapping(produces = {MediaType.APPLICATION_JSON_VALUE})
    public ResponseEntity error(HttpServletRequest request) {
        Map<String, Object> body = getErrorAttributes(request, ErrorAttributeOptions.defaults());
        //可以通过断点调试来查看body中的信息
        HttpStatus status = getStatus(request);
        ResponseVO response = new ResponseVO();
        response.setCode(status.value());
        response.setMessage(String.valueOf(body.get("error")));
        return new ResponseEntity<>(response,status);
    }
}

此处,ResponseVO 是我自定义的一个通用返回器,如果用 ResponseEntity 直接返回也是可以的。自定义通用返回器可以配合 SpringMVC 的配置来更正确地实现。

补充:下面介绍下spring boot 如何捕获filter抛出的异常,自定义返回结构

主要是继承 BasicErrorController

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
@RestController
public class ErrorController extends BasicErrorController {
  public ErrorController() {
    super(new DefaultErrorAttributes(), new ErrorProperties());
  }
  @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);
    Map<String, Object> map = new HashMap<>();
    map.put("message", "error");
    return new ResponseEntity<Map<String, Object>>(map, status);
  }
}

到此这篇关于Spring Security 捕获 filter 层面异常,返回我们自定义的内容的文章就介绍到这了,更多相关Spring Security 捕获 filter 层面异常内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://blog.csdn.net/lzb348110175/article/details/124681349

延伸 · 阅读

精彩推荐