一、spring Aop身份驗證
一般,如果用戶沒有登錄的話,用戶只可以查看商品,但是其他的,比如支付等是不能夠進行操作的,這個時候,我們就需要用到用戶攔截, 或者說身份驗證了。
首先定義一個類AuthorizeAspect
,以@Aspect
註解。
然後把所有以Controller
聲明爲切點,但排除UserController
,因爲這個Controller就是驗證用戶登錄的Controller。
@Pointcut("execution(public * com.sihai.controller *.*(..))"+ "&& !execution(public * com.sihai.controller.UserController.*(..))") public void verify(){}
最後對這個切點做一些前置處理,因爲用戶登錄後,按照我們之前寫的邏輯,cookie和redis中應該含有用戶的信息,所以現在查詢這兩個地方,來驗證用戶有沒有登錄。
@Before("verify()") public void doVerify(){ ServletRequestAttributes attributes=(ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); HttpServletRequest request=attributes.getRequest(); //查詢cookie Cookie cookie= CookieUtil.get(request,CookieConstant.TOKEN); if (cookie==null){ log.warn("Cookie中查不到token"); throw new AuthorizeException(); } //去redis查詢,這個下面的redis用到的是springboot的redis工具類 String tokenValue=redisTemplate.opsForValue().get(String.format(RedisConstant.TOKEN_PREFIX,cookie.getValue())); if (StringUtils.isEmpty(tokenValue)){ log.warn("Redis中查不到token"); throw new AuthorizeException(); } }
完整代碼如下:
@Aspect @Component @Slf4j public class AuthorizeAspect { @Autowired private StringRedisTemplate redisTemplate; @Pointcut("execution(public * com.sihai.controller. *.*(..))"+ "&& !execution(public * com.sihai.controller. UserController.*(..))") public void verify(){} @Before("verify()") public void doVerify(){ ServletRequestAttributes attributes=(ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); HttpServletRequest request=attributes.getRequest(); //查詢cookie Cookie cookie= CookieUtil.get(request,CookieConstant.TOKEN); if (cookie==null){ log.warn(" Cookie中查不到token"); throw new AuthorizeException(); } //去redis查詢 String tokenValue=redisTemplate.opsForValue().get(String.format(RedisConstant.TOKEN_PREFIX,cookie.getValue())); if (StringUtils.isEmpty(tokenValue)){ log.warn(" Redis中查不到token"); throw new AuthorizeException(); } } }
二、springboot統一異常處理
- 自定義異常類
從以上代碼中可以看到,如果用戶沒有登陸,就會拋出一個 AuthorizeException
的異常,這是一個自定義的異常。這個異常很簡單,只有一個簡單的定義,爲運行時異常
public class AuthorizeException extends RuntimeException {}
之後我們需要定義一個對這個異常的處理器 ExceptionHandler
,當撲獲到這個異常,說明用戶沒有登陸,那就重新調到登陸界面(訪問處理登陸的Controller)。
- 創建全局異常處理類:通過使用
@ControllerAdvice
定義統一的異常處理類,而不是在每個Controller中逐個定義。@ExceptionHandler
用來定義函數針對的異常類型,最後將Exception對象和請求URL映射到error.html
中
@ControllerAdvice public class ExceptionHandler { @Autowired private ProjectUrlConfig projectUrlConfig; //攔截登錄異常 @ExceptionHandler(value = AuthorizeException.class) public ModelAndView handlerAuthorizeException(){ ModelAndView mav = new ModelAndView(); mav.addObject("exception", e); mav.addObject("url", req.getRequestURL()); mav.setViewName(DEFAULT_ERROR_VIEW); return mav; } }
- 實現error.html頁面展示:在templates目錄下創建error.html,將請求的URL和Exception對象的message輸出。
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8" /> <title>統一異常處理</title> </head> <body> <h1>Error Handler</h1> <div th:text="${url}"></div> <div th:text="${exception.message}"></div> </body> </html>
啓動該應用,訪問:http://localhost:8080/hello,可以看到如下錯誤提示頁面。