Browse Source

fix:增加各个模块的拦截器

dist.1.3.0
pref_mail@163.com 11 months ago
parent
commit
505da3a19c
  1. 4
      blade-biz-common/src/main/java/org/springblade/common/constant/LauncherConstant.java
  2. 2
      blade-biz-common/src/main/java/org/springblade/common/constant/ModuleNameConstant.java
  3. 31
      blade-service/logpm-aftersales/src/main/java/com/logpm/aftersales/aspect/AsyncAnnotationAspect.java
  4. 31
      blade-service/logpm-basic/src/main/java/com/logpm/basic/aspect/AsyncAnnotationAspect.java
  5. 31
      blade-service/logpm-basicdata/src/main/java/com/logpm/basicdata/aspect/AsyncAnnotationAspect.java
  6. 31
      blade-service/logpm-distribution/src/main/java/com/logpm/distribution/aspect/AsyncAnnotationAspect.java
  7. 67
      blade-service/logpm-distribution/src/main/java/com/logpm/distribution/config/CustomMessageConverter.java
  8. 1
      blade-service/logpm-distribution/src/main/java/com/logpm/distribution/config/RabbitMqConfiguration.java
  9. 50
      blade-service/logpm-factory/src/main/java/com/logpm/factory/aspect/AsyncAnnotationAspect.java
  10. 6
      blade-service/logpm-factory/src/main/java/com/logpm/factory/comfac/controller/FactoryCommonController.java
  11. 67
      blade-service/logpm-factory/src/main/java/com/logpm/factory/config/CustomMessageConverter.java
  12. 3
      blade-service/logpm-factory/src/main/java/com/logpm/factory/config/RabbitMqConfiguration.java
  13. 2
      blade-service/logpm-factory/src/main/java/com/logpm/factory/interceptor/LocalServerLoginAccountsInterceptor.java
  14. 31
      blade-service/logpm-supervise/src/main/java/com/logpm/supervise/aspect/AsyncAnnotationAspect.java
  15. 31
      blade-service/logpm-trunkline/src/main/java/com/logpm/trunkline/aspect/AsyncAnnotationAspect.java
  16. 67
      blade-service/logpm-trunkline/src/main/java/com/logpm/trunkline/config/CustomMessageConverter.java
  17. 1
      blade-service/logpm-trunkline/src/main/java/com/logpm/trunkline/config/RabbitMqConfiguration.java
  18. 31
      blade-service/logpm-warehouse/src/main/java/com/logpm/warehouse/aspect/AsyncAnnotationAspect.java
  19. 67
      blade-service/logpm-warehouse/src/main/java/com/logpm/warehouse/config/CustomMessageConverter.java
  20. 1
      blade-service/logpm-warehouse/src/main/java/com/logpm/warehouse/config/RabbitMqConfiguration.java

4
blade-biz-common/src/main/java/org/springblade/common/constant/LauncherConstant.java

@ -93,11 +93,11 @@ public interface LauncherConstant {
/** /**
* 本地登录 dev地址 * 本地登录 dev地址
*/ */
String APPLICATION_DEV_LOGIN_ADDR = "192.168.5.14:13000"; String APPLICATION_DEV_LOGIN_ADDR = "192.168.3.2:13000";
/** /**
* 本地登录 test地址 * 本地登录 test地址
*/ */
String APPLICATION_TEST_LOGIN_ADDR = "192.168.5.14:13000"; String APPLICATION_TEST_LOGIN_ADDR = "192.168.2.110:9999";
/** /**
* 本地登录 pord地址 * 本地登录 pord地址

2
blade-biz-common/src/main/java/org/springblade/common/constant/ModuleNameConstant.java

@ -9,7 +9,7 @@ public interface ModuleNameConstant {
/** /**
* 如果不需要 "" * 如果不需要 ""
*/ */
public static final String DEVAUTH ="-pref"; public static final String DEVAUTH ="";
/** /**
* 工厂对接服务名称 * 工厂对接服务名称

31
blade-service/logpm-aftersales/src/main/java/com/logpm/aftersales/aspect/AsyncAnnotationAspect.java

@ -47,4 +47,35 @@ public class AsyncAnnotationAspect {
} }
/**
* 该方法为一个切面用于对标记了@RabbitListener注解的方法进行增强主要增加了日志记录和数据源上下文处理的功能
*
* @param joinPoint 切面的连接点表示当前被拦截的方法
* @return 返回被拦截方法的执行结果
* @throws Throwable 如果执行过程中发生异常则抛出
*/
@Around("@annotation(org.springframework.amqp.rabbit.annotation.RabbitListener)")
public Object rabbitMethods(ProceedingJoinPoint joinPoint) throws Throwable {
// 在方法执行前,获取当前租户ID并设置到数据源上下文中
String tenantId = AuthUtil.getTenantId();
DynamicDataSourceContextHolder.push(tenantId);
// 执行原方法
Object result = joinPoint.proceed();
// 在方法执行后,从数据源上下文中移除租户ID
DynamicDataSourceContextHolder.poll();
return result;
}
@Around("@annotation(org.springframework.amqp.rabbit.annotation.RabbitHandler)")
public Object rabbitHandlerMethods(ProceedingJoinPoint joinPoint) throws Throwable {
// 在方法执行前,获取当前租户ID并设置到数据源上下文中
String tenantId = AuthUtil.getTenantId();
DynamicDataSourceContextHolder.push(tenantId);
// 执行原方法
Object result = joinPoint.proceed();
// 在方法执行后,从数据源上下文中移除租户ID
DynamicDataSourceContextHolder.poll();
return result;
}
} }

31
blade-service/logpm-basic/src/main/java/com/logpm/basic/aspect/AsyncAnnotationAspect.java

@ -47,4 +47,35 @@ public class AsyncAnnotationAspect {
} }
/**
* 该方法为一个切面用于对标记了@RabbitListener注解的方法进行增强主要增加了日志记录和数据源上下文处理的功能
*
* @param joinPoint 切面的连接点表示当前被拦截的方法
* @return 返回被拦截方法的执行结果
* @throws Throwable 如果执行过程中发生异常则抛出
*/
// @Around("@annotation(org.springframework.amqp.rabbit.annotation.RabbitListener)")
// public Object rabbitMethods(ProceedingJoinPoint joinPoint) throws Throwable {
// // 在方法执行前,获取当前租户ID并设置到数据源上下文中
// String tenantId = AuthUtil.getTenantId();
// DynamicDataSourceContextHolder.push(tenantId);
// // 执行原方法
// Object result = joinPoint.proceed();
// // 在方法执行后,从数据源上下文中移除租户ID
// DynamicDataSourceContextHolder.poll();
// return result;
// }
// @Around("@annotation(org.springframework.amqp.rabbit.annotation.RabbitHandler)")
// public Object rabbitHandlerMethods(ProceedingJoinPoint joinPoint) throws Throwable {
// // 在方法执行前,获取当前租户ID并设置到数据源上下文中
// String tenantId = AuthUtil.getTenantId();
// DynamicDataSourceContextHolder.push(tenantId);
// // 执行原方法
// Object result = joinPoint.proceed();
// // 在方法执行后,从数据源上下文中移除租户ID
// DynamicDataSourceContextHolder.poll();
// return result;
// }
} }

31
blade-service/logpm-basicdata/src/main/java/com/logpm/basicdata/aspect/AsyncAnnotationAspect.java

@ -47,4 +47,35 @@ public class AsyncAnnotationAspect {
} }
/**
* 该方法为一个切面用于对标记了@RabbitListener注解的方法进行增强主要增加了日志记录和数据源上下文处理的功能
*
* @param joinPoint 切面的连接点表示当前被拦截的方法
* @return 返回被拦截方法的执行结果
* @throws Throwable 如果执行过程中发生异常则抛出
*/
// @Around("@annotation(org.springframework.amqp.rabbit.annotation.RabbitListener)")
// public Object rabbitMethods(ProceedingJoinPoint joinPoint) throws Throwable {
// // 在方法执行前,获取当前租户ID并设置到数据源上下文中
// String tenantId = AuthUtil.getTenantId();
// DynamicDataSourceContextHolder.push(tenantId);
// // 执行原方法
// Object result = joinPoint.proceed();
// // 在方法执行后,从数据源上下文中移除租户ID
// DynamicDataSourceContextHolder.poll();
// return result;
// }
// @Around("@annotation(org.springframework.amqp.rabbit.annotation.RabbitHandler)")
// public Object rabbitHandlerMethods(ProceedingJoinPoint joinPoint) throws Throwable {
// // 在方法执行前,获取当前租户ID并设置到数据源上下文中
// String tenantId = AuthUtil.getTenantId();
// DynamicDataSourceContextHolder.push(tenantId);
// // 执行原方法
// Object result = joinPoint.proceed();
// // 在方法执行后,从数据源上下文中移除租户ID
// DynamicDataSourceContextHolder.poll();
// return result;
// }
} }

31
blade-service/logpm-distribution/src/main/java/com/logpm/distribution/aspect/AsyncAnnotationAspect.java

@ -47,4 +47,35 @@ public class AsyncAnnotationAspect {
} }
/**
* 该方法为一个切面用于对标记了@RabbitListener注解的方法进行增强主要增加了日志记录和数据源上下文处理的功能
*
* @param joinPoint 切面的连接点表示当前被拦截的方法
* @return 返回被拦截方法的执行结果
* @throws Throwable 如果执行过程中发生异常则抛出
*/
@Around("@annotation(org.springframework.amqp.rabbit.annotation.RabbitListener)")
public Object rabbitMethods(ProceedingJoinPoint joinPoint) throws Throwable {
// 在方法执行前,获取当前租户ID并设置到数据源上下文中
String tenantId = AuthUtil.getTenantId();
DynamicDataSourceContextHolder.push(tenantId);
// 执行原方法
Object result = joinPoint.proceed();
// 在方法执行后,从数据源上下文中移除租户ID
DynamicDataSourceContextHolder.poll();
return result;
}
@Around("@annotation(org.springframework.amqp.rabbit.annotation.RabbitHandler)")
public Object rabbitHandlerMethods(ProceedingJoinPoint joinPoint) throws Throwable {
// 在方法执行前,获取当前租户ID并设置到数据源上下文中
String tenantId = AuthUtil.getTenantId();
DynamicDataSourceContextHolder.push(tenantId);
// 执行原方法
Object result = joinPoint.proceed();
// 在方法执行后,从数据源上下文中移除租户ID
DynamicDataSourceContextHolder.poll();
return result;
}
} }

67
blade-service/logpm-distribution/src/main/java/com/logpm/distribution/config/CustomMessageConverter.java

@ -0,0 +1,67 @@
package com.logpm.distribution.config;
import cn.hutool.json.JSONUtil;
import org.springblade.core.secure.BladeUser;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tool.utils.ThreadLocalUtil;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.support.converter.MessageConversionException;
import org.springframework.amqp.support.converter.MessageConverter;
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.util.Map;
/**
* @author zhaoqiaobo
* @create 2024-05-08
*/
@Component
public class CustomMessageConverter implements MessageConverter {
private String name;
@Override
public Message toMessage(Object o, MessageProperties messageProperties) throws MessageConversionException {
HttpHeaders headers = (HttpHeaders) ThreadLocalUtil.get("bladeContext");
if (headers != null && !headers.isEmpty()) {
headers.forEach((key, values) -> {
values.forEach((value) -> {
messageProperties.setHeader(key, new String[]{value});
});
});
}
BladeUser user = AuthUtil.getUser();
BladeUser bladeUser = new BladeUser();
bladeUser.setTenantId(user.getTenantId());
bladeUser.setUserId(user.getUserId());
bladeUser.setAccount(user.getAccount());
bladeUser.setRoleId(user.getRoleId());
messageProperties.setHeader("bladeUser", JSONUtil.toJsonStr(bladeUser));
return new Message(o.toString().getBytes(), messageProperties);
}
@Override
public Object fromMessage(Message message) throws MessageConversionException {
MessageProperties messageProperties = message.getMessageProperties();
Map<String, Object> headers = messageProperties.getHeaders();
ThreadLocalUtil.put("bladeContext", headers);
Object bladeUser = headers.get("bladeUser");
MockHttpServletRequest request = new MockHttpServletRequest();
BladeUser bladeUser1 = JSONUtil.toBean(bladeUser.toString(), BladeUser.class);
request.setAttribute("_BLADE_USER_REQUEST_ATTR_", bladeUser1);
RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request));
return new String(message.getBody());
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}

1
blade-service/logpm-distribution/src/main/java/com/logpm/distribution/config/RabbitMqConfiguration.java

@ -28,6 +28,7 @@ public class RabbitMqConfiguration {
RabbitTemplate template = new RabbitTemplate(); RabbitTemplate template = new RabbitTemplate();
template.setConnectionFactory(connectionFactory); template.setConnectionFactory(connectionFactory);
template.setMandatory(true); template.setMandatory(true);
template.setMessageConverter(new CustomMessageConverter());
template.setConfirmCallback(new RabbitTemplate.ConfirmCallback() { template.setConfirmCallback(new RabbitTemplate.ConfirmCallback() {
@Override @Override
public void confirm(CorrelationData correlationData, boolean b, String s) { public void confirm(CorrelationData correlationData, boolean b, String s) {

50
blade-service/logpm-factory/src/main/java/com/logpm/factory/aspect/AsyncAnnotationAspect.java

@ -0,0 +1,50 @@
package com.logpm.factory.aspect;
import com.baomidou.dynamic.datasource.toolkit.DynamicDataSourceContextHolder;
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.core.secure.utils.AuthUtil;
import org.springblade.core.tool.utils.StringUtil;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import java.lang.reflect.Method;
@Aspect
@Component
public class AsyncAnnotationAspect {
/**
* 该方法为一个切面用于对标记了@RabbitListener注解的方法进行增强主要增加了日志记录和数据源上下文处理的功能
*
* @param joinPoint 切面的连接点表示当前被拦截的方法
* @return 返回被拦截方法的执行结果
* @throws Throwable 如果执行过程中发生异常则抛出
*/
@Around("@annotation(org.springframework.amqp.rabbit.annotation.RabbitListener)")
public Object rabbitMethods(ProceedingJoinPoint joinPoint) throws Throwable {
// 在方法执行前,获取当前租户ID并设置到数据源上下文中
String tenantId = AuthUtil.getTenantId();
DynamicDataSourceContextHolder.push(tenantId);
// 执行原方法
Object result = joinPoint.proceed();
// 在方法执行后,从数据源上下文中移除租户ID
DynamicDataSourceContextHolder.poll();
return result;
}
@Around("@annotation(org.springframework.amqp.rabbit.annotation.RabbitHandler)")
public Object rabbitHandlerMethods(ProceedingJoinPoint joinPoint) throws Throwable {
// 在方法执行前,获取当前租户ID并设置到数据源上下文中
String tenantId = AuthUtil.getTenantId();
DynamicDataSourceContextHolder.push(tenantId);
// 执行原方法
Object result = joinPoint.proceed();
// 在方法执行后,从数据源上下文中移除租户ID
DynamicDataSourceContextHolder.poll();
return result;
}
}

6
blade-service/logpm-factory/src/main/java/com/logpm/factory/comfac/controller/FactoryCommonController.java

@ -185,11 +185,7 @@ public class FactoryCommonController {
map.put("flagType", "OrderStatusLog"); map.put("flagType", "OrderStatusLog");
//将消息携带绑定键值 //将消息携带绑定键值
rabbitTemplate.convertAndSend(RabbitConstant.UNLOAD_CAR_COM_INFO_EXCHANGE, RabbitConstant.UNLOAD_CAR_COM_INFO_ROUTING, map,message -> { rabbitTemplate.convertAndSend(RabbitConstant.UNLOAD_CAR_COM_INFO_EXCHANGE, RabbitConstant.UNLOAD_CAR_COM_INFO_ROUTING, map);
HttpServletRequest request = WebUtil.getRequest();
message.getMessageProperties().setHeader("Blade-Auth",request.getHeader("Blade-Auth"));
return message;
});
return R.success("调用成功"); return R.success("调用成功");
} catch (CustomerException e) { } catch (CustomerException e) {

67
blade-service/logpm-factory/src/main/java/com/logpm/factory/config/CustomMessageConverter.java

@ -0,0 +1,67 @@
package com.logpm.factory.config;
import cn.hutool.json.JSONUtil;
import org.springblade.core.secure.BladeUser;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tool.utils.ThreadLocalUtil;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.support.converter.MessageConversionException;
import org.springframework.amqp.support.converter.MessageConverter;
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.util.Map;
/**
* @author zhaoqiaobo
* @create 2024-05-08
*/
@Component
public class CustomMessageConverter implements MessageConverter {
private String name;
@Override
public Message toMessage(Object o, MessageProperties messageProperties) throws MessageConversionException {
HttpHeaders headers = (HttpHeaders) ThreadLocalUtil.get("bladeContext");
if (headers != null && !headers.isEmpty()) {
headers.forEach((key, values) -> {
values.forEach((value) -> {
messageProperties.setHeader(key, new String[]{value});
});
});
}
BladeUser user = AuthUtil.getUser();
BladeUser bladeUser = new BladeUser();
bladeUser.setTenantId(user.getTenantId());
bladeUser.setUserId(user.getUserId());
bladeUser.setAccount(user.getAccount());
bladeUser.setRoleId(user.getRoleId());
messageProperties.setHeader("bladeUser", JSONUtil.toJsonStr(bladeUser));
return new Message(o.toString().getBytes(), messageProperties);
}
@Override
public Object fromMessage(Message message) throws MessageConversionException {
MessageProperties messageProperties = message.getMessageProperties();
Map<String, Object> headers = messageProperties.getHeaders();
ThreadLocalUtil.put("bladeContext", headers);
Object bladeUser = headers.get("bladeUser");
MockHttpServletRequest request = new MockHttpServletRequest();
BladeUser bladeUser1 = JSONUtil.toBean(bladeUser.toString(), BladeUser.class);
request.setAttribute("_BLADE_USER_REQUEST_ATTR_", bladeUser1);
RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request));
return new String(message.getBody());
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}

3
blade-service/logpm-factory/src/main/java/com/logpm/factory/config/RabbitMqConfiguration.java

@ -27,10 +27,11 @@ import static io.protostuff.MapSchema.MessageFactories.HashMap;
public class RabbitMqConfiguration { public class RabbitMqConfiguration {
@Bean @Bean
public RabbitTemplate createRabbitTemplate(ConnectionFactory connectionFactory) { public RabbitTemplate createRabbitTemplate(ConnectionFactory connectionFactory,CustomMessageConverter converter) {
RabbitTemplate template = new RabbitTemplate(); RabbitTemplate template = new RabbitTemplate();
template.setConnectionFactory(connectionFactory); template.setConnectionFactory(connectionFactory);
template.setMandatory(true); template.setMandatory(true);
template.setMessageConverter(converter);
template.setConfirmCallback(new RabbitTemplate.ConfirmCallback() { template.setConfirmCallback(new RabbitTemplate.ConfirmCallback() {
@Override @Override
public void confirm(CorrelationData correlationData, boolean b, String s) { public void confirm(CorrelationData correlationData, boolean b, String s) {

2
blade-service/logpm-factory/src/main/java/com/logpm/factory/interceptor/LocalServerLoginAccountsInterceptor.java

@ -54,7 +54,7 @@ public class LocalServerLoginAccountsInterceptor extends HandlerInterceptorAdapt
Map<String, Object> params = new HashMap<>(); Map<String, Object> params = new HashMap<>();
params.put("grant_type", "local_server"); params.put("grant_type", "local_server");
params.put("scope", "all"); params.put("scope", "all");
params.put("username", "admin"); params.put("username", "shujutongbu");
params.put("tenantId", "627683"); params.put("tenantId", "627683");
HttpResponse execute = urlRequest.form(params).execute(); HttpResponse execute = urlRequest.form(params).execute();
String body = execute.body(); String body = execute.body();

31
blade-service/logpm-supervise/src/main/java/com/logpm/supervise/aspect/AsyncAnnotationAspect.java

@ -47,4 +47,35 @@ public class AsyncAnnotationAspect {
} }
/**
* 该方法为一个切面用于对标记了@RabbitListener注解的方法进行增强主要增加了日志记录和数据源上下文处理的功能
*
* @param joinPoint 切面的连接点表示当前被拦截的方法
* @return 返回被拦截方法的执行结果
* @throws Throwable 如果执行过程中发生异常则抛出
*/
@Around("@annotation(org.springframework.amqp.rabbit.annotation.RabbitListener)")
public Object rabbitMethods(ProceedingJoinPoint joinPoint) throws Throwable {
// 在方法执行前,获取当前租户ID并设置到数据源上下文中
String tenantId = AuthUtil.getTenantId();
DynamicDataSourceContextHolder.push(tenantId);
// 执行原方法
Object result = joinPoint.proceed();
// 在方法执行后,从数据源上下文中移除租户ID
DynamicDataSourceContextHolder.poll();
return result;
}
@Around("@annotation(org.springframework.amqp.rabbit.annotation.RabbitHandler)")
public Object rabbitHandlerMethods(ProceedingJoinPoint joinPoint) throws Throwable {
// 在方法执行前,获取当前租户ID并设置到数据源上下文中
String tenantId = AuthUtil.getTenantId();
DynamicDataSourceContextHolder.push(tenantId);
// 执行原方法
Object result = joinPoint.proceed();
// 在方法执行后,从数据源上下文中移除租户ID
DynamicDataSourceContextHolder.poll();
return result;
}
} }

31
blade-service/logpm-trunkline/src/main/java/com/logpm/trunkline/aspect/AsyncAnnotationAspect.java

@ -47,4 +47,35 @@ public class AsyncAnnotationAspect {
} }
/**
* 该方法为一个切面用于对标记了@RabbitListener注解的方法进行增强主要增加了日志记录和数据源上下文处理的功能
*
* @param joinPoint 切面的连接点表示当前被拦截的方法
* @return 返回被拦截方法的执行结果
* @throws Throwable 如果执行过程中发生异常则抛出
*/
@Around("@annotation(org.springframework.amqp.rabbit.annotation.RabbitListener)")
public Object rabbitMethods(ProceedingJoinPoint joinPoint) throws Throwable {
// 在方法执行前,获取当前租户ID并设置到数据源上下文中
String tenantId = AuthUtil.getTenantId();
DynamicDataSourceContextHolder.push(tenantId);
// 执行原方法
Object result = joinPoint.proceed();
// 在方法执行后,从数据源上下文中移除租户ID
DynamicDataSourceContextHolder.poll();
return result;
}
@Around("@annotation(org.springframework.amqp.rabbit.annotation.RabbitHandler)")
public Object rabbitHandlerMethods(ProceedingJoinPoint joinPoint) throws Throwable {
// 在方法执行前,获取当前租户ID并设置到数据源上下文中
String tenantId = AuthUtil.getTenantId();
DynamicDataSourceContextHolder.push(tenantId);
// 执行原方法
Object result = joinPoint.proceed();
// 在方法执行后,从数据源上下文中移除租户ID
DynamicDataSourceContextHolder.poll();
return result;
}
} }

67
blade-service/logpm-trunkline/src/main/java/com/logpm/trunkline/config/CustomMessageConverter.java

@ -0,0 +1,67 @@
package com.logpm.trunkline.config;
import cn.hutool.json.JSONUtil;
import org.springblade.core.secure.BladeUser;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tool.utils.ThreadLocalUtil;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.support.converter.MessageConversionException;
import org.springframework.amqp.support.converter.MessageConverter;
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.util.Map;
/**
* @author zhaoqiaobo
* @create 2024-05-08
*/
@Component
public class CustomMessageConverter implements MessageConverter {
private String name;
@Override
public Message toMessage(Object o, MessageProperties messageProperties) throws MessageConversionException {
HttpHeaders headers = (HttpHeaders) ThreadLocalUtil.get("bladeContext");
if (headers != null && !headers.isEmpty()) {
headers.forEach((key, values) -> {
values.forEach((value) -> {
messageProperties.setHeader(key, new String[]{value});
});
});
}
BladeUser user = AuthUtil.getUser();
BladeUser bladeUser = new BladeUser();
bladeUser.setTenantId(user.getTenantId());
bladeUser.setUserId(user.getUserId());
bladeUser.setAccount(user.getAccount());
bladeUser.setRoleId(user.getRoleId());
messageProperties.setHeader("bladeUser", JSONUtil.toJsonStr(bladeUser));
return new Message(o.toString().getBytes(), messageProperties);
}
@Override
public Object fromMessage(Message message) throws MessageConversionException {
MessageProperties messageProperties = message.getMessageProperties();
Map<String, Object> headers = messageProperties.getHeaders();
ThreadLocalUtil.put("bladeContext", headers);
Object bladeUser = headers.get("bladeUser");
MockHttpServletRequest request = new MockHttpServletRequest();
BladeUser bladeUser1 = JSONUtil.toBean(bladeUser.toString(), BladeUser.class);
request.setAttribute("_BLADE_USER_REQUEST_ATTR_", bladeUser1);
RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request));
return new String(message.getBody());
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}

1
blade-service/logpm-trunkline/src/main/java/com/logpm/trunkline/config/RabbitMqConfiguration.java

@ -21,6 +21,7 @@ public class RabbitMqConfiguration {
RabbitTemplate template = new RabbitTemplate(); RabbitTemplate template = new RabbitTemplate();
template.setConnectionFactory(connectionFactory); template.setConnectionFactory(connectionFactory);
template.setMandatory(true); template.setMandatory(true);
template.setMessageConverter(new CustomMessageConverter());
template.setConfirmCallback(new RabbitTemplate.ConfirmCallback() { template.setConfirmCallback(new RabbitTemplate.ConfirmCallback() {
@Override @Override
public void confirm(CorrelationData correlationData, boolean b, String s) { public void confirm(CorrelationData correlationData, boolean b, String s) {

31
blade-service/logpm-warehouse/src/main/java/com/logpm/warehouse/aspect/AsyncAnnotationAspect.java

@ -47,4 +47,35 @@ public class AsyncAnnotationAspect {
} }
/**
* 该方法为一个切面用于对标记了@RabbitListener注解的方法进行增强主要增加了日志记录和数据源上下文处理的功能
*
* @param joinPoint 切面的连接点表示当前被拦截的方法
* @return 返回被拦截方法的执行结果
* @throws Throwable 如果执行过程中发生异常则抛出
*/
@Around("@annotation(org.springframework.amqp.rabbit.annotation.RabbitListener)")
public Object rabbitMethods(ProceedingJoinPoint joinPoint) throws Throwable {
// 在方法执行前,获取当前租户ID并设置到数据源上下文中
String tenantId = AuthUtil.getTenantId();
DynamicDataSourceContextHolder.push(tenantId);
// 执行原方法
Object result = joinPoint.proceed();
// 在方法执行后,从数据源上下文中移除租户ID
DynamicDataSourceContextHolder.poll();
return result;
}
@Around("@annotation(org.springframework.amqp.rabbit.annotation.RabbitHandler)")
public Object rabbitHandlerMethods(ProceedingJoinPoint joinPoint) throws Throwable {
// 在方法执行前,获取当前租户ID并设置到数据源上下文中
String tenantId = AuthUtil.getTenantId();
DynamicDataSourceContextHolder.push(tenantId);
// 执行原方法
Object result = joinPoint.proceed();
// 在方法执行后,从数据源上下文中移除租户ID
DynamicDataSourceContextHolder.poll();
return result;
}
} }

67
blade-service/logpm-warehouse/src/main/java/com/logpm/warehouse/config/CustomMessageConverter.java

@ -0,0 +1,67 @@
package com.logpm.warehouse.config;
import cn.hutool.json.JSONUtil;
import org.springblade.core.secure.BladeUser;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tool.utils.ThreadLocalUtil;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.support.converter.MessageConversionException;
import org.springframework.amqp.support.converter.MessageConverter;
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.util.Map;
/**
* @author zhaoqiaobo
* @create 2024-05-08
*/
@Component
public class CustomMessageConverter implements MessageConverter {
private String name;
@Override
public Message toMessage(Object o, MessageProperties messageProperties) throws MessageConversionException {
HttpHeaders headers = (HttpHeaders) ThreadLocalUtil.get("bladeContext");
if (headers != null && !headers.isEmpty()) {
headers.forEach((key, values) -> {
values.forEach((value) -> {
messageProperties.setHeader(key, new String[]{value});
});
});
}
BladeUser user = AuthUtil.getUser();
BladeUser bladeUser = new BladeUser();
bladeUser.setTenantId(user.getTenantId());
bladeUser.setUserId(user.getUserId());
bladeUser.setAccount(user.getAccount());
bladeUser.setRoleId(user.getRoleId());
messageProperties.setHeader("bladeUser", JSONUtil.toJsonStr(bladeUser));
return new Message(o.toString().getBytes(), messageProperties);
}
@Override
public Object fromMessage(Message message) throws MessageConversionException {
MessageProperties messageProperties = message.getMessageProperties();
Map<String, Object> headers = messageProperties.getHeaders();
ThreadLocalUtil.put("bladeContext", headers);
Object bladeUser = headers.get("bladeUser");
MockHttpServletRequest request = new MockHttpServletRequest();
BladeUser bladeUser1 = JSONUtil.toBean(bladeUser.toString(), BladeUser.class);
request.setAttribute("_BLADE_USER_REQUEST_ATTR_", bladeUser1);
RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request));
return new String(message.getBody());
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}

1
blade-service/logpm-warehouse/src/main/java/com/logpm/warehouse/config/RabbitMqConfiguration.java

@ -27,6 +27,7 @@ public class RabbitMqConfiguration {
public RabbitTemplate createRabbitTemplate(ConnectionFactory connectionFactory){ public RabbitTemplate createRabbitTemplate(ConnectionFactory connectionFactory){
RabbitTemplate template = new RabbitTemplate(); RabbitTemplate template = new RabbitTemplate();
template.setConnectionFactory(connectionFactory); template.setConnectionFactory(connectionFactory);
template.setMessageConverter(new CustomMessageConverter());
template.setMandatory(true); template.setMandatory(true);
template.setConfirmCallback(new RabbitTemplate.ConfirmCallback() { template.setConfirmCallback(new RabbitTemplate.ConfirmCallback() {
@Override @Override

Loading…
Cancel
Save