詳解Spring?Security?捕獲?filter?層面異常返回我們自定義的內(nèi)容
通常,我們通過 @ControllerAdvice 和 @ExceptionHandler 來捕獲并處理 Controller 層面的異常。但是,filter 是在 controller 層之前的,需要先通過 filter 才能到達 controller 層,此文就介紹一下如何捕獲filter層面的異常。
Spring 的異常會轉(zhuǎn)發(fā)到 BasicErrorController
中進行異常寫入,然后才會返回客戶端。所以,我們可以在 BasicErrorController
對 filter
異常進行捕獲并處理。
所以,我們需要重寫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à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
是我自定義的一個通用返回器,如果用 ResponseEntity
直接返回也是可以的。自定義通用返回器可以配合 SpringMVC 的配置來更正確地實現(xiàn)。
補充:下面介紹下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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot讀取Resource目錄下文件的四種方式總結(jié)
在Spring?Boot項目中,經(jīng)常需要獲取resources目錄下的文件,這些文件可以包括配置文件、模板文件、靜態(tài)資源等,本文將介紹四種常用的方法來獲取resources目錄下的文件,需要的朋友可以參考下2023-08-08java導(dǎo)出excel 瀏覽器直接下載或者或以文件形式導(dǎo)出
這篇文章主要介紹了java導(dǎo)出excel 瀏覽器直接下載或者或以文件形式導(dǎo)出方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-06-06SpringBoot2底層注解@Configuration配置類詳解
這篇文章主要為大家介紹了SpringBoot2底層注解@Configuration配置類詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-05-05java框架基礎(chǔ)之SPI機制實現(xiàn)及源碼解析
這篇文章主要為大家介紹了java框架基礎(chǔ)之SPI機制實現(xiàn)及源碼解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-09-09