您好,登錄后才能下訂單哦!
這篇文章主要介紹Java攔截器和切面怎么用,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!
攔截器的使用:每次接收到某個請求之前,都會調用此攔截器中的方法,其中preHandle方法如果return true,表示繼續調用對應的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獲取請求是get,post等 if ("OPTIONS".equals(request.getMethod())) { // 指定允許其他域名訪問 response.setHeader("Access-Control-Allow-Origin", "*"); // 響應類型 response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE"); // 響應頭設置 response.setHeader("Access-Control-Allow-Headers", "Content-Type, x-requested-with, X-Custom-Header"); response.setStatus(204); return true; } // 獲取從header中得到的數據 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校驗失敗 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類型的參數 //環繞通知類似于動態代理的全過程:ProceedingJoinPoint類型的參數可以決定是否執行目標方法 //且環繞通知必須有返回值,返回值即目標方法的返回值。 @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()); //執行目標方法 try { logger.info("request channels begin, param{pageNum:" + methodName + ", pageSize:" + args); //前置通知,表示在此之前的代碼會在調用controller之前調用 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); //不拋出異常的話,異常就被上面抓住,執行下去,返回result,result值為null,轉換為int } //返回通知 logger.info("Arround:The method "+ methodName+" ends with the Result "+ result); return result; }
以上是“Java攔截器和切面怎么用”這篇文章的所有內容,感謝各位的閱讀!希望分享的內容對大家有幫助,更多相關知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。