Spring下Filter過濾器配置全局異常處理的詳細步驟
Spring下Filter過濾器配置全局異常處理
Filter中出現(xiàn)的異常,spring的全局異常處理器是無法捕獲的,所以filter攔截器中出現(xiàn)的異常會直接的拋向瀏覽器,在瀏覽器中顯示500錯誤。 而我當前的項目中,是在Filter中判斷用戶是否有攜帶Token訪問,如果沒有,則拋出異常,讓其做登錄操作。而且異常信息要處理成json格式返回給前端。這就很尷尬了。好了廢話說多了,上解決方案:
結(jié)局方案:
Filter攔截器中直接拋出異常信息
@Componentpublic class AdminAuthentiationFilter extends OncePerRequestFilter { private final String DEFAULTE_URI = '/api/admin/login'; @Override protected void doFilterInternal(HttpServletRequest req, HttpServletResponse resp, FilterChain filterChain) throws ServletException, IOException { String admin_token = req.getHeader('admin_token'); if(StrUtil.isBlank(admin_token) && !req.getRequestURI().equals(DEFAULTE_URI)){ //在攔截器中直接拋出一個異常 throw new LoginException('用戶未登錄,請先登錄!'); } filterChain.doFilter(req,resp); }}
第一步:在web.xml中配置錯誤頁,用于捕獲500狀態(tài)
<!-- 注冊過濾器--><filter> <filter-name>myFilter</filter-name> <filter-class>com.fenkuan.support.filters.AdminAuthentiationFilter</filter-class></filter><filter-mapping> <filter-name>myFilter</filter-name> <url-pattern>/*</url-pattern></filter-mapping><!--捕獲500錯誤狀態(tài)--><error-page> <error-code>500</error-code> <location>/500</location></error-page>
第二步:編寫一個FilterException自定義異常類
public class FilterException extends RuntimeException{private String data; public FilterException(String message) { super(message); } public FilterException(String message, String data) { super(message, data); this.data = data; } public String getData() { return data; }}
第三步:編寫一個用于處理500錯誤的controller
@RestControllerpublic class FilterErrorController { @RequestMapping('/500') public void filterError(HttpServletRequest req){ //獲取servlet請求中的異常屬性。該屬性下存儲了確切的錯誤信息。 Throwable t = (Throwable) req.getAttribute('javax.servlet.error.exception');//創(chuàng)建一個filterException拋出,該異常會被全局異常處理類捕獲,并處理。 throw new FilterException(t.getMessage()); }}
第四步:編寫一個捕獲全局異常的異常處理類
//全局異常處理類@RestControllerAdvicepublic class ControllerExceptionHandler{ @ExceptionHandler(FilterException.class) @ResponseStatus(HttpStatus.BAD_REQUEST) public BadException<?> filterException(FilterException e){ BadException<Object> objectBadException = handleExceptionObject(e); objectBadException.setStatus(HttpStatus.BAD_REQUEST.value()); objectBadException.setMessage(e.getMessage()); return objectBadException; } @ExceptionHandler(Exception.class) @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) public BadException<?> responseException(Exception e){ //異常兜底處理 BadException<?> objectBadException = handleExceptionObject(e); objectBadException.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value()); objectBadException.setMessage(HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase()); return objectBadException; }// Throwable是Exception的父類,所以可以使用該類型來接受項目中拋出的所有異常,包括Exception即其子類。 private <T> BadException<T> handleExceptionObject(Throwable throwable){ BadException<T> bad = new BadException<>(); bad.setMessage(throwable.getMessage()); return bad; }}
BadException類,用于封裝要返會給前端的異常信息(這里使用了Lombok工具)
import lombok.Data;@Datapublic class BadException<T> { private Integer status; private String message; private Object data; private Object devData;}
結(jié)果:
到此這篇關(guān)于Spring下Filter過濾器配置全局異常處理的詳細步驟的文章就介紹到這了,更多相關(guān)Spring全局異常處理內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
