SpringBoot - AOP 實現登入狀態檢查
阿新 • • 發佈:2021-10-14
目錄
- End -
﹀
﹀
﹀
白嫖有風險
點贊加收藏
以上為本篇文章的主要內容,希望大家多提意見,如果喜歡記得點個推薦哦
作者:Maggieq8324
出處:https://www.cnblogs.com/maggieq8324/
本文版權歸作者和部落格園共有,歡迎轉載,轉載時保留原作者和文章地址即可。
前言
記錄下AOP
實現登入狀態的檢查,文章使用的JWT
校驗參考:SpringBoot - 整合Auth0 JWT
實現登入狀態檢查的方式
Servlet
過濾器- 攔截器
Spring AOP
AOP 定義
AOP(Aspect Oriented Programming)
,面向切面程式設計,通過預編譯方式和執行期間動態代理實現程式功能的統一維護的一種技術,在程式開發中主要用來解決一些系統層面上的問題,在不改變原有的邏輯的基礎上,增加一些額外的功能,如日誌,事務,許可權等
AOP 相關概念
術語 | 概念 | 描述 |
---|---|---|
Aspect | 切面 | 通常是一個類,可以定義切入點和通知 |
Joint point | 連線點 | 程式執行過程中明確的點,在Spring中指的是被攔截到的方法(Spring只支援方法型別的連線點) |
Advice | 通知 | AOP在特定的切入點上執行的增強處理,有before(前置),after(後置),afterReturning(最終),afterThrowing(異常),around(環繞) |
Pointcut | 切入點 | 帶有通知的連線點,在程式中主要體現為書寫切入點表示式 |
Target | 目標物件 | 織入 Advice 的目標物件 |
Weaving | 織入 | 將 Aspect 和其他物件連線起來, 並建立 Adviced object 的過程 |
Advice 通知型別
型別 | 描述 |
---|---|
Before | 在目標方法被呼叫之前做增強處理 |
After | 在目標方法完成之後做增強 |
AfterReturning | 在目標方法正常完成後做增強 |
AfterThrowing | 處理程式中未處理的異常 |
Around | 環繞通知,在目標方法完成前後做增強處理,環繞通知是最重要的通知型別 |
具體實現
實現程式碼
- 自定義註解
CheckLogin
/** * @Description 登入校驗註解 * @author coisini * @date Oct 14, 2021 * @Version 1.0 */ public @interface CheckLogin { }
- 切面
CheckLoginAspect
import com.coisini.aop.util.JwtUtil;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
/**
* @Description 登入校驗切面
* @author coisini
* @date Oct 14, 2021
* @Version 1.0
*/
@Aspect
@Component
public class CheckLoginAspect {
/**
* 只要加了@CheckLogin的方法都會走到這裡
* @param point
* @return
*/
@Around("@annotation(com.coisini.aop.auth.annotation.CheckLogin)")
public Object checkLogin(ProceedingJoinPoint point) {
try {
// 從header中獲取token
RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
ServletRequestAttributes attributes = (ServletRequestAttributes) requestAttributes;
HttpServletRequest request = attributes.getRequest();
String token = request.getHeader("token");
// 校驗token是否合法
Boolean valid = JwtUtil.verifyToken(token);
if (!valid) {
throw new ServerErrorException(HttpStatus.UNAUTHORIZED.value(), "Token 不合法");
}
// 執行後續的方法
return point.proceed();
} catch (Throwable throwable) {
throw new ServerErrorException(HttpStatus.UNAUTHORIZED.value(), "Token 不合法");
}
}
}
- 自定義異常
ServerErrorException
import lombok.AllArgsConstructor;
import lombok.Data;
/**
* @Description 自定義異常
* @author coisini
* @date Oct 14, 2021
* @Version 1.0
*/
@Data
@AllArgsConstructor
public class ServerErrorException extends RuntimeException{
public Integer code;
public String message;
}
- 統一異常處理
GlobalExceptionAdvice
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
/**
* @Description 統一異常處理
* @author coisini
* @date Oct 14, 2021
* @Version 1.0
*/
@RestControllerAdvice
@Slf4j
public class GlobalExceptionAdvice {
@ExceptionHandler(ServerErrorException.class)
public ResponseEntity<UnifyMessage> handleServerErrorException(ServerErrorException e) {
log.warn("ServerErrorException 異常", e);
return new ResponseEntity<>(
UnifyMessage.builder()
.code(e.getCode())
.message(e.getMessage())
.build(),
HttpStatus.UNAUTHORIZED
);
}
}
- 統一訊息返回
UnifyMessage
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
* @Description 統一訊息返回
* @author coisini
* @date Oct 14, 2021
* @Version 1.0
*/
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class UnifyMessage {
private int code;
private String message;
}
測試
- 測試方法
TestController
/**
* AOP校驗請求頭中的Token
* @return
*/
@CheckLogin
@GetMapping(value = "/test")
public String testCheckLogin() {
// TODO 業務
return "Token驗證通過";
}
- 獲取
Token
- 無
Token
測試
- 傳遞
Token
原始碼
GitHub
:https://github.com/maggieq8324/java-learn-demo/tree/master/springboot-aop