10 changed files with 409 additions and 134 deletions
@ -0,0 +1,122 @@ |
|||||||
|
package com.logpm.basicdata.aspect; |
||||||
|
|
||||||
|
import com.alibaba.fastjson.JSONObject; |
||||||
|
import com.baomidou.dynamic.datasource.toolkit.DynamicDataSourceContextHolder; |
||||||
|
import lombok.AllArgsConstructor; |
||||||
|
import lombok.extern.slf4j.Slf4j; |
||||||
|
import org.aspectj.lang.ProceedingJoinPoint; |
||||||
|
import org.aspectj.lang.annotation.Around; |
||||||
|
import org.aspectj.lang.annotation.Aspect; |
||||||
|
import org.aspectj.lang.reflect.MethodSignature; |
||||||
|
import org.springblade.common.annotations.ChangeAsync; |
||||||
|
import org.springblade.common.cache.CacheNames; |
||||||
|
import org.springblade.common.component.MockLoginService; |
||||||
|
import org.springblade.core.redis.cache.BladeRedis; |
||||||
|
import org.springblade.core.redis.lock.LockType; |
||||||
|
import org.springblade.core.redis.lock.RedisLockClient; |
||||||
|
import org.springblade.core.tool.utils.StringUtil; |
||||||
|
import org.springblade.core.tool.utils.ThreadLocalUtil; |
||||||
|
import org.springframework.core.annotation.Order; |
||||||
|
import org.springframework.core.env.Environment; |
||||||
|
import org.springframework.http.HttpHeaders; |
||||||
|
import org.springframework.mock.web.MockHttpServletRequest; |
||||||
|
import org.springframework.stereotype.Component; |
||||||
|
import org.springframework.web.context.request.RequestContextHolder; |
||||||
|
import org.springframework.web.context.request.ServletRequestAttributes; |
||||||
|
|
||||||
|
import java.lang.reflect.Method; |
||||||
|
import java.util.Objects; |
||||||
|
import java.util.concurrent.TimeUnit; |
||||||
|
|
||||||
|
@Aspect |
||||||
|
@Component |
||||||
|
@Slf4j |
||||||
|
@Order(-1) |
||||||
|
@AllArgsConstructor |
||||||
|
public class ChangeAsyncAnnotationAspect { |
||||||
|
|
||||||
|
private final MockLoginService mockLoginService; |
||||||
|
|
||||||
|
private final BladeRedis bladeRedis; |
||||||
|
private final Environment environment; |
||||||
|
private final RedisLockClient redisLockClient; |
||||||
|
private final String account = "shujutongbu"; |
||||||
|
|
||||||
|
/** |
||||||
|
* 定义一个切点,匹配所有带有@ChangeAsync注解的方法。 |
||||||
|
* 注意:实际上Spring Framework自带对@ChangeAsync的处理,直接这样配置可能会导致预期之外的行为。 |
||||||
|
*/ |
||||||
|
@Around("@annotation(org.springblade.common.annotations.ChangeAsync)") |
||||||
|
public Object logAroundAsyncMethods(ProceedingJoinPoint joinPoint) throws Throwable { |
||||||
|
|
||||||
|
MethodSignature signature = (MethodSignature) joinPoint.getSignature(); |
||||||
|
Method method = signature.getMethod(); |
||||||
|
|
||||||
|
ChangeAsync myAsync = method.getAnnotation(ChangeAsync.class); |
||||||
|
String annotationValue = myAsync.value(); |
||||||
|
log.info(">>>>>>>>>>>>>>>>>> ChangeAsync={}", annotationValue); |
||||||
|
|
||||||
|
// 获取当前拦截方法的入参参数
|
||||||
|
Object[] args = joinPoint.getArgs(); |
||||||
|
// 获取入参名称
|
||||||
|
String[] parameterNames = signature.getParameterNames(); |
||||||
|
String tenantId = null; |
||||||
|
// 获取参数名称 为tenantId 的值
|
||||||
|
for (int i = 0; i < parameterNames.length; i++) { |
||||||
|
if ("tenantId".equals(parameterNames[i])) { |
||||||
|
tenantId = (String) args[i]; |
||||||
|
log.info(">> tenandId {} ", tenantId); |
||||||
|
break; |
||||||
|
} |
||||||
|
} |
||||||
|
// 执行模拟登录
|
||||||
|
if (StringUtil.isNotBlank(tenantId)) { |
||||||
|
String key =CacheNames.LOCAL_SERVER_USER+tenantId+":"+account; |
||||||
|
String lockKey =key+":lock"; |
||||||
|
JSONObject data =bladeRedis.get(key); |
||||||
|
if(Objects.isNull(data)){ |
||||||
|
boolean flag = redisLockClient.tryLock(lockKey, LockType.FAIR, 5000, 10000, TimeUnit.MILLISECONDS); |
||||||
|
if(flag){ |
||||||
|
data =bladeRedis.get(key); |
||||||
|
if(Objects.isNull(data)){ |
||||||
|
data = mockLoginService.mockToken(tenantId,account); |
||||||
|
bladeRedis.setEx(key,data,2591990L); |
||||||
|
redisLockClient.unLock(lockKey, LockType.FAIR); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
MockHttpServletRequest mockRequest = new MockHttpServletRequest(); |
||||||
|
mockRequest.addHeader("Blade-Auth", "bearer "+data.get("access_token")); |
||||||
|
RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(mockRequest)); |
||||||
|
|
||||||
|
HttpHeaders httpHeaders = new HttpHeaders(); |
||||||
|
httpHeaders.add("Blade-Auth","bearer "+data.get("access_token") ); |
||||||
|
httpHeaders.add( "Authorization", "Basic bG9jYWw6bG9jYWxfc2VjcmV0"); |
||||||
|
ThreadLocalUtil.put("bladeContext", httpHeaders); |
||||||
|
|
||||||
|
DynamicDataSourceContextHolder.push(data.getString("tenant_id")); |
||||||
|
// 执行原方法
|
||||||
|
Object result = joinPoint.proceed(); |
||||||
|
// 在方法执行后,从数据源上下文中移除租户ID
|
||||||
|
DynamicDataSourceContextHolder.poll(); |
||||||
|
return result; |
||||||
|
}else{ |
||||||
|
return joinPoint.proceed(); |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
// // 在方法执行前的操作
|
||||||
|
// String tenantId = AuthUtil.getTenantId();
|
||||||
|
// log.info(">> tenandId {} ",tenantId);
|
||||||
|
// DynamicDataSourceContextHolder.push("627683");
|
||||||
|
//
|
||||||
|
// // 执行原方法
|
||||||
|
// Object result = joinPoint.proceed();
|
||||||
|
//
|
||||||
|
// // 在方法执行后的操作
|
||||||
|
// DynamicDataSourceContextHolder.poll();
|
||||||
|
// return result;
|
||||||
|
} |
||||||
|
|
||||||
|
} |
Loading…
Reference in new issue