您好,登錄后才能下訂單哦!
這篇文章主要介紹Java攔截器和切面怎么用,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!
攔截器的使用:每次接收到某個(gè)請(qǐng)求之前,都會(huì)調(diào)用此攔截器中的方法,其中preHandle方法如果return true,表示繼續(xù)調(diào)用對(duì)應(yīng)的controller,如果return false,
public class CheckLoginInterceptor implements HandlerInterceptor { private Logger logger = Logger.getLogger(CheckLoginInterceptor.class); private static String TOKEN_VALID_MSG ; static { TOKEN_VALID_MSG=JsonUtil.writeObject2JSON(new AMSResultVO(CodeNum.TOKEN_VALID, CodeMessage.TOKEN_VALID)); } public Boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { //request.getMethod獲取請(qǐng)求是get,post等 if ("OPTIONS".equals(request.getMethod())) { // 指定允許其他域名訪問 response.setHeader("Access-Control-Allow-Origin", "*"); // 響應(yīng)類型 response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE"); // 響應(yīng)頭設(shè)置 response.setHeader("Access-Control-Allow-Headers", "Content-Type, x-requested-with, X-Custom-Header"); response.setStatus(204); return true; } // 獲取從header中得到的數(shù)據(jù) String userName = request.getHeader(CommonConsts.PARAM_USER_NAME); String userToken = request.getHeader(CommonConsts.PARAM_USER_TOKEN); Boolean result = true; String method = request.getRequestURI(); if(method.equals("/ams/fileUpload")) { return true; } if(StringUtil.isEmpty(userName) || StringUtil.isEmpty(userToken)) { result = false; } else { result = TokenUtil.validToken(userName, userToken); } // token校驗(yàn)失敗 if(!result) { response.setContentType("text/html;charset=UTF-8"); response.getWriter().print(TOKEN_VALID_MSG); response.getWriter().flush(); response.getWriter().close(); } return result; } public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { } public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { } }
切面的使用:
//壞繞通知:需要攜帶ProceedingJoinPoint類型的參數(shù) //環(huán)繞通知類似于動(dòng)態(tài)代理的全過程:ProceedingJoinPoint類型的參數(shù)可以決定是否執(zhí)行目標(biāo)方法 //且環(huán)繞通知必須有返回值,返回值即目標(biāo)方法的返回值。 @Around("execution(* com.sowell.controller.*Controller.*(..))") public Object aroundMethod(ProceedingJoinPoint pjd) { Object result = null; String methodName = pjd.getSignature().getName(); Object args = Arrays.asList(pjd.getArgs()); //執(zhí)行目標(biāo)方法 try { logger.info("request channels begin, param{pageNum:" + methodName + ", pageSize:" + args); //前置通知,表示在此之前的代碼會(huì)在調(diào)用controller之前調(diào)用 result = pjd.proceed(); recordOprationLog(result, methodName, result); //后置通知 logger.info("Arround:The method "+ methodName+" ends"); } catch (Throwable e) { e.printStackTrace(); //異常通知 logger.error("Arround:The method "+ methodName+"occurs exception:"+e); //throw new RuntimeException(e); //不拋出異常的話,異常就被上面抓住,執(zhí)行下去,返回result,result值為null,轉(zhuǎn)換為int } //返回通知 logger.info("Arround:The method "+ methodName+" ends with the Result "+ result); return result; }
以上是“Java攔截器和切面怎么用”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。