首要咱們定義一個(gè)切入點(diǎn)(匹配com.ed.controller.Seller最初的controller的所有public辦法)
@Pointcut(“execution(public*com.ed.controller.Seller*.*(..))”)publicvoidcheckToken(){}
然后在進(jìn)入這些辦法之前進(jìn)行token校驗(yàn)
@Before(“checkToken()”)publicvoidcheck(){
ServletRequestAttributesattributes=(ServletRequestAttributes)RequestContextHolder.getRequestAttributes();
HttpServletRequestrequest=attributes.getRequest();//查詢cookieCookiecookie=CookieUtil.get(request,CookieConstant.TOKEN);if(cookie==null){
log.warn(“【token校驗(yàn)】Cookie中查不到token”);thrownewSellerAuthorizeException(ResultEnum.TOKEN_ERROR);
}//去redis里查詢StringtokenValue=redisTemplate.opsForValue().get(String.format(RedisConstant.TOKEN_PREFIX,cookie.getValue()));if(StringUtils.isEmpty(tokenValue)){
log.warn(“【token校驗(yàn)】Redis中查不到token”);thrownewSellerAuthorizeException(ResultEnum.TOKEN_ERROR);
}
}
拋出的異常可定義一個(gè)handler進(jìn)行攔截,并回來自定義的對(duì)象給前端
@ControllerAdvicepublicclassSellExceptionHandler{
@ExceptionHandler(value=SellerAuthorizeException.class)
@ResponseBodypublicResultVOhandlerSellerException(SellerAuthorizeExceptione){returnResultVOUtil.error(e.getCode(),e.getMessage());
}
}
CookieUtil辦法
/***獲取cookie
*@paramrequest
*@paramname
*@return*/publicstaticCookieget(HttpServletRequestrequest,
Stringname){
MapcookieMap=readCookieMap(request);if(cookieMap.containsKey(name)){returncookieMap.get(name);
}else{returnnull;
}
}/***將cookie封裝成Map
*@paramrequest
*@return*/privatestaticMapreadCookieMap(HttpServletRequestrequest){
MapcookieMap=newHashMap<>();
Cookie[]cookies=request.getCookies();if(cookies!=null){for(Cookiecookie:cookies){
cookieMap.put(cookie.getName(),cookie);
}
}returncookieMap;
}
廣州天河區(qū)珠江新城富力盈力大廈北塔2706
020-38013166(網(wǎng)站咨詢專線)
400-001-5281 (售后服務(wù)熱線)
深圳市坂田十二橡樹莊園F1-7棟
Site/ http://www.szciya.com
E-mail/ itciya@vip.163.com
品牌服務(wù)專線:400-001-5281
長(zhǎng)沙市天心區(qū)芙蓉中路三段398號(hào)新時(shí)空大廈5樓
聯(lián)系電話/ (+86 0731)88282200
品牌服務(wù)專線/ 400-966-8830
旗下運(yùn)營(yíng)網(wǎng)站:
Copyright ? 2016 廣州思洋文化傳播有限公司,保留所有權(quán)利。 粵ICP備09033321號(hào)