Spring下Filter過(guò)濾器配置全局異常處理的詳細(xì)步驟
Spring下Filter過(guò)濾器配置全局異常處理
Filter中出現(xiàn)的異常,spring的全局異常處理器是無(wú)法捕獲的,所以filter攔截器中出現(xiàn)的異常會(huì)直接的拋向?yàn)g覽器,在瀏覽器中顯示500錯(cuò)誤。 而我當(dāng)前的項(xiàng)目中,是在Filter中判斷用戶(hù)是否有攜帶Token訪(fǎng)問(wèn),如果沒(méi)有,則拋出異常,讓其做登錄操作。而且異常信息要處理成json格式返回給前端。這就很尷尬了。好了廢話(huà)說(shuō)多了,上解決方案:
結(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)){ //在攔截器中直接拋出一個(gè)異常 throw new LoginException('用戶(hù)未登錄,請(qǐng)先登錄!'); } filterChain.doFilter(req,resp); }}
第一步:在web.xml中配置錯(cuò)誤頁(yè),用于捕獲500狀態(tài)
<!-- 注冊(cè)過(guò)濾器--><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錯(cuò)誤狀態(tài)--><error-page> <error-code>500</error-code> <location>/500</location></error-page>
第二步:編寫(xiě)一個(gè)FilterException自定義異常類(lèi)
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; }}
第三步:編寫(xiě)一個(gè)用于處理500錯(cuò)誤的controller
@RestControllerpublic class FilterErrorController { @RequestMapping('/500') public void filterError(HttpServletRequest req){ //獲取servlet請(qǐng)求中的異常屬性。該屬性下存儲(chǔ)了確切的錯(cuò)誤信息。 Throwable t = (Throwable) req.getAttribute('javax.servlet.error.exception');//創(chuàng)建一個(gè)filterException拋出,該異常會(huì)被全局異常處理類(lèi)捕獲,并處理。 throw new FilterException(t.getMessage()); }}
第四步:編寫(xiě)一個(gè)捕獲全局異常的異常處理類(lèi)
//全局異常處理類(lèi)@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的父類(lèi),所以可以使用該類(lèi)型來(lái)接受項(xiàng)目中拋出的所有異常,包括Exception即其子類(lèi)。 private <T> BadException<T> handleExceptionObject(Throwable throwable){ BadException<T> bad = new BadException<>(); bad.setMessage(throwable.getMessage()); return bad; }}
BadException類(lèi),用于封裝要返會(huì)給前端的異常信息(這里使用了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ò)濾器配置全局異常處理的詳細(xì)步驟的文章就介紹到這了,更多相關(guān)Spring全局異常處理內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. ASP.NET MVC遍歷驗(yàn)證ModelState的錯(cuò)誤信息2. 將properties文件的配置設(shè)置為整個(gè)Web應(yīng)用的全局變量實(shí)現(xiàn)方法3. asp(vbs)Rs.Open和Conn.Execute的詳解和區(qū)別及&H0001的說(shuō)明4. jsp網(wǎng)頁(yè)實(shí)現(xiàn)貪吃蛇小游戲5. 用css截取字符的幾種方法詳解(css排版隱藏溢出文本)6. ASP 信息提示函數(shù)并作返回或者轉(zhuǎn)向7. asp中response.write("中文")或者js中文亂碼問(wèn)題8. PHP設(shè)計(jì)模式中工廠(chǎng)模式深入詳解9. CSS hack用法案例詳解10. ThinkPHP5實(shí)現(xiàn)JWT Token認(rèn)證的過(guò)程(親測(cè)可用)
