33 lines
944 B
Java
33 lines
944 B
Java
package com.kama.notes.aspect;
|
|
|
|
import org.aspectj.lang.ProceedingJoinPoint;
|
|
import org.aspectj.lang.annotation.Around;
|
|
import org.aspectj.lang.annotation.Aspect;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.stereotype.Component;
|
|
|
|
import com.kama.notes.annotation.NeedLogin;
|
|
import com.kama.notes.scope.RequestScopeData;
|
|
import com.kama.notes.utils.ApiResponseUtil;
|
|
|
|
@Aspect
|
|
@Component
|
|
public class NeedLoginAspect {
|
|
|
|
@Autowired
|
|
private RequestScopeData requestScopeData;
|
|
|
|
@Around("@annotation(needLogin)")
|
|
public Object around(ProceedingJoinPoint joinPoint, NeedLogin needLogin) throws Throwable {
|
|
|
|
if (!requestScopeData.isLogin()) {
|
|
return ApiResponseUtil.error("用户未登录");
|
|
}
|
|
|
|
if (requestScopeData.getUserId() == null) {
|
|
return ApiResponseUtil.error("用户 ID 异常");
|
|
}
|
|
return joinPoint.proceed();
|
|
}
|
|
}
|