最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

詳解Spring?Security?捕獲?filter?層面異常返回我們自定義的內(nèi)容

 更新時(shí)間:2022年05月10日 11:04:09   作者:扛麻袋的少年  
Spring?的異常會(huì)轉(zhuǎn)發(fā)到?BasicErrorController?中進(jìn)行異常寫入,然后才會(huì)返回客戶端。所以,我們可以在?BasicErrorController?對(duì)?filter異常進(jìn)行捕獲并處理,下面通過本文給大家介紹Spring?Security?捕獲?filter?層面異常,返回我們自定義的內(nèi)容,感興趣的朋友一起看看吧

通常,我們通過 @ControllerAdvice 和 @ExceptionHandler 來捕獲并處理 Controller 層面的異常。但是,filter 是在 controller 層之前的,需要先通過 filter 才能到達(dá) controller 層,此文就介紹一下如何捕獲filter層面的異常。

Spring 的異常會(huì)轉(zhuǎn)發(fā)到 BasicErrorController 中進(jìn)行異常寫入,然后才會(huì)返回客戶端。所以,我們可以在 BasicErrorController 對(duì) filter異常進(jìn)行捕獲并處理。

所以,我們需要重寫BasicErrorController中的error方法。

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());
        //可以通過斷點(diǎn)調(diào)試來查看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 是我自定義的一個(gè)通用返回器,如果用 ResponseEntity 直接返回也是可以的。自定義通用返回器可以配合 SpringMVC 的配置來更正確地實(shí)現(xiàn)。

補(bǔ)充:下面介紹下spring boot 如何捕獲filter拋出的異常,自定義返回結(jié)構(gòu)

主要是繼承 BasicErrorController

@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);
  }
}

到此這篇關(guān)于Spring Security 捕獲 filter 層面異常,返回我們自定義的內(nèi)容的文章就介紹到這了,更多相關(guān)Spring Security 捕獲 filter 層面異常內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論

罗源县| 辽宁省| 项城市| 西峡县| 芦溪县| 新津县| 龙陵县| 平泉县| 屯昌县| 兴隆县| 德令哈市| 射洪县| 汶上县| 大同市| 安康市| 盈江县| 南丹县| 游戏| 疏勒县| 桦川县| 五莲县| 萨嘎县| 兴宁市| 交城县| 东明县| 福安市| 遂宁市| 清远市| 永寿县| 什邡市| 阿拉尔市| 客服| 迁西县| 鸡东县| 泊头市| 麻栗坡县| 军事| 平定县| 西和县| 龙口市| 锦屏县|