|
|
@ -1,18 +1,20 @@ |
|
|
|
package com.ccsens.ht.aspect; |
|
|
|
package com.ccsens.cloudutil.aspect; |
|
|
|
|
|
|
|
import com.alibaba.fastjson.JSON; |
|
|
|
import com.alibaba.fastjson.JSONObject; |
|
|
|
import com.ccsens.cloudutil.annotation.MustLogin; |
|
|
|
import com.ccsens.cloudutil.feign.TallFeignClient; |
|
|
|
import com.ccsens.ht.bean.dto.QueryDto; |
|
|
|
import com.ccsens.util.CodeEnum; |
|
|
|
import com.ccsens.util.JsonResponse; |
|
|
|
import com.ccsens.util.WebConstant; |
|
|
|
import com.ccsens.util.exception.BaseException; |
|
|
|
import com.ccsens.util.bean.dto.QueryDto; |
|
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
|
import org.aspectj.lang.ProceedingJoinPoint; |
|
|
|
import org.aspectj.lang.Signature; |
|
|
|
import org.aspectj.lang.annotation.Around; |
|
|
|
import org.aspectj.lang.annotation.Aspect; |
|
|
|
import org.aspectj.lang.annotation.Pointcut; |
|
|
|
import org.aspectj.lang.reflect.MethodSignature; |
|
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
|
import org.springframework.core.annotation.Order; |
|
|
|
import org.springframework.stereotype.Component; |
|
|
@ -20,6 +22,7 @@ import org.springframework.web.context.request.RequestContextHolder; |
|
|
|
import org.springframework.web.context.request.ServletRequestAttributes; |
|
|
|
|
|
|
|
import javax.servlet.http.HttpServletRequest; |
|
|
|
import java.lang.reflect.Method; |
|
|
|
|
|
|
|
/** |
|
|
|
* @description: |
|
|
@ -34,7 +37,7 @@ public class MustLoginAspect { |
|
|
|
@Autowired |
|
|
|
private TallFeignClient tallFeignClient; |
|
|
|
|
|
|
|
@Pointcut("@annotation(com.ccsens.ht.annotation.MustLogin)") |
|
|
|
@Pointcut("@annotation(com.ccsens.cloudutil.annotation.MustLogin) || @annotation(com.ccsens.cloudutil.annotation.Login) ") |
|
|
|
public void loginAdvice(){} |
|
|
|
|
|
|
|
@Around("loginAdvice()") |
|
|
@ -47,8 +50,28 @@ public class MustLoginAspect { |
|
|
|
Object[] args = pjp.getArgs(); |
|
|
|
QueryDto dto = args == null || args.length < 1 ? null : (QueryDto) args[0]; |
|
|
|
|
|
|
|
//获取userId
|
|
|
|
JsonResponse response = tallFeignClient.getUserIdByToken(authHeader); |
|
|
|
log.info("{}获取userId:{}", authHeader, response); |
|
|
|
|
|
|
|
Signature signature = pjp.getSignature(); |
|
|
|
MethodSignature methodSignature = (MethodSignature) signature; |
|
|
|
Method targetMethod = methodSignature.getMethod(); |
|
|
|
MustLogin mustLoginAnnotation = targetMethod.getAnnotation(MustLogin.class); |
|
|
|
|
|
|
|
if (mustLoginAnnotation == null) { |
|
|
|
log.info("不是必须登录,有token,则添加userId,没有则不添加"); |
|
|
|
if (response.getCode().intValue() == CodeEnum.SUCCESS.getCode().intValue() && response.getData() != null) { |
|
|
|
JSONObject json = JSONObject.parseObject(JSON.toJSONString(response.getData())); |
|
|
|
Long userId = json.getLong("id"); |
|
|
|
if (dto != null) { |
|
|
|
dto.setUserId(userId); |
|
|
|
} |
|
|
|
} |
|
|
|
Object result = pjp.proceed(); |
|
|
|
return result; |
|
|
|
} |
|
|
|
//必须登录,未登录直接返回未登录相关信息
|
|
|
|
if (response.getCode().intValue() != CodeEnum.SUCCESS.getCode().intValue()) { |
|
|
|
return response; |
|
|
|
} |