Browse Source

Merge remote-tracking branch 'origin/dev' into dev

dist.1.3.0
zhaoqiaobo 9 months ago
parent
commit
20a3c784ee
  1. 22
      blade-biz-common/src/main/java/org/springblade/common/annotations/LocalServerUser.java
  2. 5
      blade-biz-common/src/main/java/org/springblade/common/cache/CacheNames.java
  3. 25
      blade-biz-common/src/main/java/org/springblade/common/constant/LauncherConstant.java
  4. 8
      blade-biz-common/src/main/java/org/springblade/common/launch/LauncherServiceImpl.java
  5. 31
      blade-service/logpm-aftersales/src/main/java/com/logpm/aftersales/aspect/AsyncAnnotationAspect.java
  6. 31
      blade-service/logpm-basic/src/main/java/com/logpm/basic/aspect/AsyncAnnotationAspect.java
  7. 31
      blade-service/logpm-basicdata/src/main/java/com/logpm/basicdata/aspect/AsyncAnnotationAspect.java
  8. 31
      blade-service/logpm-distribution/src/main/java/com/logpm/distribution/aspect/AsyncAnnotationAspect.java
  9. 67
      blade-service/logpm-distribution/src/main/java/com/logpm/distribution/config/CustomMessageConverter.java
  10. 1
      blade-service/logpm-distribution/src/main/java/com/logpm/distribution/config/RabbitMqConfiguration.java
  11. 50
      blade-service/logpm-factory/src/main/java/com/logpm/factory/aspect/AsyncAnnotationAspect.java
  12. 59
      blade-service/logpm-factory/src/main/java/com/logpm/factory/aspect/LocalServerTenantIdAnnotationAspect.java
  13. 2
      blade-service/logpm-factory/src/main/java/com/logpm/factory/comfac/controller/FactoryAuthController.java
  14. 7
      blade-service/logpm-factory/src/main/java/com/logpm/factory/comfac/controller/FactoryCommonController.java
  15. 67
      blade-service/logpm-factory/src/main/java/com/logpm/factory/config/CustomMessageConverter.java
  16. 12
      blade-service/logpm-factory/src/main/java/com/logpm/factory/config/InterceptorAdapterConfig.java
  17. 3
      blade-service/logpm-factory/src/main/java/com/logpm/factory/config/RabbitMqConfiguration.java
  18. 6
      blade-service/logpm-factory/src/main/java/com/logpm/factory/filer/HttpServletFilter.java
  19. 100
      blade-service/logpm-factory/src/main/java/com/logpm/factory/interceptor/LocalServerLoginAccountsInterceptor.java
  20. 1
      blade-service/logpm-factory/src/main/java/com/logpm/factory/mt/mapper/MtFactoryOrderMainMapper.java
  21. 36
      blade-service/logpm-factory/src/main/java/com/logpm/factory/wrapper/CustomHttpServletRequestWrapper.java
  22. 8
      blade-service/logpm-old-project/src/main/resources/application-dev.yml
  23. 31
      blade-service/logpm-supervise/src/main/java/com/logpm/supervise/aspect/AsyncAnnotationAspect.java
  24. 31
      blade-service/logpm-trunkline/src/main/java/com/logpm/trunkline/aspect/AsyncAnnotationAspect.java
  25. 67
      blade-service/logpm-trunkline/src/main/java/com/logpm/trunkline/config/CustomMessageConverter.java
  26. 1
      blade-service/logpm-trunkline/src/main/java/com/logpm/trunkline/config/RabbitMqConfiguration.java
  27. 31
      blade-service/logpm-warehouse/src/main/java/com/logpm/warehouse/aspect/AsyncAnnotationAspect.java
  28. 67
      blade-service/logpm-warehouse/src/main/java/com/logpm/warehouse/config/CustomMessageConverter.java
  29. 1
      blade-service/logpm-warehouse/src/main/java/com/logpm/warehouse/config/RabbitMqConfiguration.java

22
blade-biz-common/src/main/java/org/springblade/common/annotations/LocalServerUser.java

@ -0,0 +1,22 @@
package org.springblade.common.annotations;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* 特俗标记
*/
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.METHOD})
public @interface LocalServerUser {
/**
* 匹配值
* factory:工厂系统
* 6278683: 租户
* factory-data: 租户
* @return
*/
String ds() default "";
}

5
blade-biz-common/src/main/java/org/springblade/common/cache/CacheNames.java vendored

@ -78,4 +78,9 @@ public interface CacheNames {
* 用户能操作的仓库
*/
String USER_WAREHOUSE_OWN = "logpm:user:warehouse:own:";
/**
* 模拟登录缓存对象
*/
String LOCAL_SERVER_USER = "logpm:user:local:user:";
}

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

@ -90,6 +90,20 @@ public interface LauncherConstant {
*/
String SEATA_TEST_ADDR = "192.168.2.110:8091";
/**
* 本地登录 dev地址
*/
String APPLICATION_DEV_LOGIN_ADDR = "192.168.3.2:13000";
/**
* 本地登录 test地址
*/
String APPLICATION_TEST_LOGIN_ADDR = "192.168.2.110:9999";
/**
* 本地登录 pord地址
*/
String APPLICATION_PROD_LOGIN_ADDR = "192.168.5.14:13000";
/**
* dbuuo提供者
@ -221,4 +235,15 @@ public interface LauncherConstant {
}
}
static String loginAddr(String profile) {
switch (profile) {
case (AppConstant.PROD_CODE):
return APPLICATION_DEV_LOGIN_ADDR;
case (AppConstant.TEST_CODE):
return APPLICATION_TEST_LOGIN_ADDR;
default:
return APPLICATION_PROD_LOGIN_ADDR;
}
}
}

8
blade-biz-common/src/main/java/org/springblade/common/launch/LauncherServiceImpl.java

@ -44,10 +44,10 @@ public class LauncherServiceImpl implements LauncherService {
PropsUtil.setProperty(props, "spring.cloud.sentinel.transport.dashboard", LauncherConstant.sentinelAddr(profile));
PropsUtil.setProperty(props, "spring.cloud.nacos.discovery.username","nacos");
PropsUtil.setProperty(props, "spring.cloud.nacos.discovery.password","Nacos123123");
PropsUtil.setProperty(props, "spring.cloud.nacos.config.username","nacos");
PropsUtil.setProperty(props, "spring.cloud.nacos.config.password","Nacos123123");
// PropsUtil.setProperty(props, "spring.cloud.nacos.discovery.username","nacos");
// PropsUtil.setProperty(props, "spring.cloud.nacos.discovery.password","Nacos123123");
// PropsUtil.setProperty(props, "spring.cloud.nacos.config.username","nacos");
// PropsUtil.setProperty(props, "spring.cloud.nacos.config.password","Nacos123123");
// dubbo注册
PropsUtil.setProperty(props, "dubbo.application.name", appName);
PropsUtil.setProperty(props, "dubbo.application.qos.enable", "false");

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();
template.setConnectionFactory(connectionFactory);
template.setMandatory(true);
template.setMessageConverter(new CustomMessageConverter());
template.setConfirmCallback(new RabbitTemplate.ConfirmCallback() {
@Override
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;
}
}

59
blade-service/logpm-factory/src/main/java/com/logpm/factory/aspect/LocalServerTenantIdAnnotationAspect.java

@ -0,0 +1,59 @@
//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.common.annotations.LocalServerUser;
//import org.springblade.core.secure.BladeUser;
//import org.springblade.core.secure.utils.AuthUtil;
//import org.springblade.core.tool.utils.StringUtil;
//import org.springblade.core.tool.utils.WebUtil;
//import org.springblade.system.cache.UserCache;
//import org.springblade.system.entity.User;
//import org.springframework.core.annotation.Order;
//import org.springframework.scheduling.annotation.Async;
//import org.springframework.stereotype.Component;
//
//import javax.servlet.http.HttpServletRequest;
//import java.lang.reflect.Method;
//
//@Aspect
//@Component
//public class LocalServerTenantIdAnnotationAspect {
//
//
// /**
// * 定义一个切点,匹配所有带有@Async("asyncExecutor")注解的方法。
// * 注意:实际上Spring Framework自带对@Async("asyncExecutor")的处理,直接这样配置可能会导致预期之外的行为。
// */
//
// // 定义一个
// @Around("@annotation(org.springblade.common.annotations.LocalServerUser)")
// public Object logAroundAsyncMethods(ProceedingJoinPoint joinPoint) throws Throwable {
//
// MethodSignature signature = (MethodSignature) joinPoint.getSignature();
// Method method = signature.getMethod();
//
// LocalServerUser myAsync = method.getAnnotation(LocalServerUser.class);
// String annotationValue = myAsync.ds();
//
// if(StringUtil.isNotBlank(annotationValue) ){
// // 在方法执行前的操作
// DynamicDataSourceContextHolder.push(annotationValue);
//
// // 执行原方法
// Object result = joinPoint.proceed();
//
// // 在方法执行后的操作
// DynamicDataSourceContextHolder.poll();
//
// return result;
// }else{
// return joinPoint.proceed();
// }
//
// }
//
//}

2
blade-service/logpm-factory/src/main/java/com/logpm/factory/comfac/controller/FactoryAuthController.java

@ -26,6 +26,7 @@ import lombok.AllArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.springblade.common.exception.CustomerException;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.tenant.annotation.NonDS;
import org.springblade.core.tool.api.R;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
@ -39,6 +40,7 @@ import javax.servlet.http.HttpServletResponse;
* @since 2023-06-06
*/
@Log4j2
@NonDS
@RestController
@AllArgsConstructor
@RequestMapping("/factory/auth")

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

@ -10,17 +10,23 @@ import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.springblade.common.annotations.LocalServerUser;
import org.springblade.common.constant.RabbitConstant;
import org.springblade.common.exception.CustomerException;
import org.springblade.common.utils.CommonUtil;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.DateUtil;
import org.springblade.core.tool.utils.StringUtil;
import org.springblade.core.tool.utils.WebUtil;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
@ -177,6 +183,7 @@ public class FactoryCommonController {
map.put("messageData", receiveInfoDTO);
map.put("createTime", new Date().getTime());
map.put("flagType", "OrderStatusLog");
//将消息携带绑定键值
rabbitTemplate.convertAndSend(RabbitConstant.UNLOAD_CAR_COM_INFO_EXCHANGE, RabbitConstant.UNLOAD_CAR_COM_INFO_ROUTING, map);

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;
}
}

12
blade-service/logpm-factory/src/main/java/com/logpm/factory/config/InterceptorAdapterConfig.java

@ -2,8 +2,10 @@ package com.logpm.factory.config;
import com.logpm.factory.comfac.service.IFactoryTokenService;
import com.logpm.factory.interceptor.FactoryAccountsInterceptor;
import com.logpm.factory.interceptor.LocalServerLoginAccountsInterceptor;
import com.logpm.factory.interceptor.ZbFactoryAccountsInterceptor;
import com.logpm.factory.props.ZbFactoryProperties;
import org.springblade.core.redis.cache.BladeRedis;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistration;
@ -15,8 +17,10 @@ public class InterceptorAdapterConfig implements WebMvcConfigurer {
@Autowired
IFactoryTokenService factoryTokenService;
@Autowired
ZbFactoryProperties zbFactoryProperties;
private BladeRedis redis;
@Override
public void addInterceptors(InterceptorRegistry interceptorRegistry) {
@ -26,6 +30,12 @@ public class InterceptorAdapterConfig implements WebMvcConfigurer {
excludePathPatterns("/factory/oupai/**").
excludePathPatterns("/factory/zb/**")
.excludePathPatterns("/factory/common/**");
registry=interceptorRegistry.addInterceptor(new LocalServerLoginAccountsInterceptor(redis));
// registry.addPathPatterns("/factory/**").
// excludePathPatterns("/factory/auth/token").
// excludePathPatterns("/factory/oupai/**").
// excludePathPatterns("/factory/zb/**")
// .excludePathPatterns("/factory/common/**");
// InterceptorRegistration registryZbFactory = interceptorRegistry.addInterceptor(new ZbFactoryAccountsInterceptor(zbFactoryProperties));
// registryZbFactory.addPathPatterns("/factory/zb/**");
}

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 {
@Bean
public RabbitTemplate createRabbitTemplate(ConnectionFactory connectionFactory) {
public RabbitTemplate createRabbitTemplate(ConnectionFactory connectionFactory,CustomMessageConverter converter) {
RabbitTemplate template = new RabbitTemplate();
template.setConnectionFactory(connectionFactory);
template.setMandatory(true);
template.setMessageConverter(converter);
template.setConfirmCallback(new RabbitTemplate.ConfirmCallback() {
@Override
public void confirm(CorrelationData correlationData, boolean b, String s) {

6
blade-service/logpm-factory/src/main/java/com/logpm/factory/filer/HttpServletFilter.java

@ -11,8 +11,14 @@ import java.io.IOException;
public class HttpServletFilter implements Filter {
public void init(FilterConfig filterConfig) throws ServletException {
System.out.println("****************************1");
}
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> 执行这里");
ServletRequest requestWrapper = null;
if(request instanceof HttpServletRequest) {
requestWrapper = new RequestWrapper((HttpServletRequest) request);

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

@ -0,0 +1,100 @@
package com.logpm.factory.interceptor;
import cn.hutool.core.map.MapUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONObject;
import com.logpm.factory.comfac.service.IFactoryTokenService;
import com.logpm.factory.config.RequestWrapper;
import com.logpm.factory.wrapper.CustomHttpServletRequestWrapper;
import lombok.AllArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.springblade.common.cache.CacheNames;
import org.springblade.common.constant.LauncherConstant;
import org.springblade.common.exception.CustomerException;
import org.springblade.core.launch.constant.AppConstant;
import org.springblade.core.redis.cache.BladeRedis;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.StringUtil;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
@Log4j2
@AllArgsConstructor
public class LocalServerLoginAccountsInterceptor extends HandlerInterceptorAdapter {
private final BladeRedis bladeRedis;
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws CustomerException {
try {
JSONObject data =bladeRedis.get(CacheNames.LOCAL_SERVER_USER+"admin");
CustomHttpServletRequestWrapper wrappedRequest = new CustomHttpServletRequestWrapper(request);
if(Objects.isNull(data)){
String url = "http://"+ LauncherConstant.loginAddr(AppConstant.DEV_CODE)+"/blade-auth/oauth/token";
cn.hutool.http.HttpRequest urlRequest = HttpRequest.post(url);
urlRequest.header("Authorization", "Basic c2FiZXI6c2FiZXJfc2VjcmV0");
urlRequest.header("Tenant-Id", "627683");
Map<String, Object> params = new HashMap<>();
params.put("grant_type", "local_server");
params.put("scope", "all");
params.put("username", "shujutongbu");
params.put("tenantId", "627683");
HttpResponse execute = urlRequest.form(params).execute();
String body = execute.body();
data = JSONObject.parseObject(body);
bladeRedis.setEx(CacheNames.LOCAL_SERVER_USER+"admin",data,3200L);
}
// 修改或添加header
// 发送登录请求
log.info("##########preHandle: request={}", request);
wrappedRequest.addHeader("Blade-Auth", "bearer "+data.getString("access_token"));
// 用包装后的request替换原始request
request = wrappedRequest;
RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request));
return true;
} catch (Exception e) {
returnJson(response, JSONObject.toJSONString(R.fail(500, "服务异常,请联系管理员")));
return false;
}
}
private void returnJson(HttpServletResponse response, String json) {
PrintWriter writer = null;
response.setCharacterEncoding("UTF-8");
response.setContentType("application/json");
try {
writer = response.getWriter();
writer.print(json);
} catch (IOException e) {
System.out.println(e.getMessage());
} finally {
if (writer != null) {
writer.close();
}
}
}
}

1
blade-service/logpm-factory/src/main/java/com/logpm/factory/mt/mapper/MtFactoryOrderMainMapper.java

@ -1,5 +1,6 @@
package com.logpm.factory.mt.mapper;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.logpm.factory.mt.entity.MtFactoryOrderMain;

36
blade-service/logpm-factory/src/main/java/com/logpm/factory/wrapper/CustomHttpServletRequestWrapper.java

@ -0,0 +1,36 @@
package com.logpm.factory.wrapper;
import org.springframework.http.HttpHeaders;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import java.util.*;
public class CustomHttpServletRequestWrapper extends HttpServletRequestWrapper {
private final HttpHeaders headers;
public CustomHttpServletRequestWrapper(HttpServletRequest request) {
super(request);
headers = new HttpHeaders();
}
@Override
public String getHeader(String name) {
String headerValue = headers.getFirst(name);
return headerValue != null ? headerValue : super.getHeader(name);
}
@Override
public Enumeration<String> getHeaderNames() {
List<String> names = Collections.list(super.getHeaderNames());
headers.forEach((key, value) -> names.add(key));
return Collections.enumeration(names);
}
// 其他需要覆盖的方法...
public void addHeader(String name, String value) {
headers.add(name, value);
}
}

8
blade-service/logpm-old-project/src/main/resources/application-dev.yml

@ -34,12 +34,12 @@ spring:
url: ${blade.datasource.oldproject.master.url}
username: ${blade.datasource.oldproject.master.username}
password: ${blade.datasource.oldproject.master.password}
slave:
627683:
druid:
#独立校验配置
validation-query: select 1
#oracle校验
#validation-query: select 1 from dual
url: ${blade.datasource.oldproject.slave.url}
username: ${blade.datasource.oldproject.slave.username}
password: ${blade.datasource.oldproject.slave.password}
url: ${blade.datasource.oldproject.627683.url}
username: ${blade.datasource.oldproject.627683.username}
password: ${blade.datasource.oldproject.627683.password}

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();
template.setConnectionFactory(connectionFactory);
template.setMandatory(true);
template.setMessageConverter(new CustomMessageConverter());
template.setConfirmCallback(new RabbitTemplate.ConfirmCallback() {
@Override
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){
RabbitTemplate template = new RabbitTemplate();
template.setConnectionFactory(connectionFactory);
template.setMessageConverter(new CustomMessageConverter());
template.setMandatory(true);
template.setConfirmCallback(new RabbitTemplate.ConfirmCallback() {
@Override

Loading…
Cancel
Save