8 changed files with 199 additions and 5 deletions
@ -0,0 +1,65 @@ |
|||||||
|
package com.logpm.factorydata.base.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.web.context.request.RequestContextHolder; |
||||||
|
import org.springframework.web.context.request.ServletRequestAttributes; |
||||||
|
|
||||||
|
import java.util.Map; |
||||||
|
|
||||||
|
/** |
||||||
|
* @author zhaoqiaobo |
||||||
|
* @create 2024-05-08 |
||||||
|
*/ |
||||||
|
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; |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,17 @@ |
|||||||
|
package com.logpm.factorydata.base.config; |
||||||
|
|
||||||
|
import org.springframework.amqp.rabbit.connection.ConnectionFactory; |
||||||
|
import org.springframework.amqp.rabbit.core.RabbitTemplate; |
||||||
|
import org.springframework.context.annotation.Bean; |
||||||
|
import org.springframework.context.annotation.Configuration; |
||||||
|
|
||||||
|
@Configuration |
||||||
|
public class RabbitConfig { |
||||||
|
|
||||||
|
@Bean |
||||||
|
public RabbitTemplate rabbitTemplate(ConnectionFactory factory) { |
||||||
|
RabbitTemplate template = new RabbitTemplate(factory); |
||||||
|
template.setMessageConverter(new CustomMessageConverter()); |
||||||
|
return template; |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,33 @@ |
|||||||
|
package com.logpm.factorydata.olo.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.springblade.core.secure.utils.AuthUtil; |
||||||
|
import org.springframework.stereotype.Component; |
||||||
|
|
||||||
|
@Aspect |
||||||
|
@Component |
||||||
|
public class AnnotationAspect { |
||||||
|
|
||||||
|
/** |
||||||
|
* 该方法为一个切面,用于对标记了@RabbitListener注解的方法进行增强,主要增加了日志记录和数据源上下文处理的功能。 |
||||||
|
* |
||||||
|
* @param joinPoint 切面的连接点,表示当前被拦截的方法。 |
||||||
|
* @return 返回被拦截方法的执行结果。 |
||||||
|
* @throws Throwable 如果执行过程中发生异常,则抛出。 |
||||||
|
*/ |
||||||
|
@Around("@annotation(org.springframework.amqp.rabbit.annotation.RabbitListener)") |
||||||
|
public Object logAroundAsyncMethods(ProceedingJoinPoint joinPoint) throws Throwable { |
||||||
|
// 在方法执行前,获取当前租户ID并设置到数据源上下文中
|
||||||
|
String tenantId = AuthUtil.getTenantId(); |
||||||
|
DynamicDataSourceContextHolder.push(tenantId); |
||||||
|
// 执行原方法
|
||||||
|
Object result = joinPoint.proceed(); |
||||||
|
// 在方法执行后,从数据源上下文中移除租户ID
|
||||||
|
DynamicDataSourceContextHolder.poll(); |
||||||
|
return result; |
||||||
|
} |
||||||
|
|
||||||
|
} |
@ -0,0 +1,57 @@ |
|||||||
|
package com.logpm.factorydata.olo.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 { |
||||||
|
@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()); |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,18 @@ |
|||||||
|
package com.logpm.factorydata.olo.config; |
||||||
|
|
||||||
|
import com.logpm.factorydata.olo.config.CustomMessageConverter; |
||||||
|
import org.springframework.amqp.rabbit.connection.ConnectionFactory; |
||||||
|
import org.springframework.amqp.rabbit.core.RabbitTemplate; |
||||||
|
import org.springframework.context.annotation.Bean; |
||||||
|
import org.springframework.context.annotation.Configuration; |
||||||
|
|
||||||
|
@Configuration |
||||||
|
public class RabbitConfig { |
||||||
|
|
||||||
|
@Bean |
||||||
|
public RabbitTemplate rabbitTemplate(ConnectionFactory factory, CustomMessageConverter customMessageConverter) { |
||||||
|
RabbitTemplate template = new RabbitTemplate(factory); |
||||||
|
template.setMessageConverter(customMessageConverter); |
||||||
|
return template; |
||||||
|
} |
||||||
|
} |
Loading…
Reference in new issue