spring Aop實現身份驗證和springboot異常統一處理
阿新 • • 發佈:2019-02-04
文章有不當之處,歡迎指正,如果喜歡微信閱讀,你也可以關注我的微信公眾號:
好好學java
,獲取優質學習資源。
一、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>