10 changed files with 1334 additions and 14 deletions
@ -0,0 +1,70 @@
|
||||
//
|
||||
// Source code recreated from a .class file by IntelliJ IDEA
|
||||
// (powered by FernFlower decompiler)
|
||||
//
|
||||
|
||||
package org.springblade.common.model; |
||||
|
||||
import org.springframework.lang.Nullable; |
||||
import org.springframework.util.CollectionUtils; |
||||
|
||||
import java.util.ArrayList; |
||||
import java.util.Collection; |
||||
import java.util.Collections; |
||||
import java.util.Iterator; |
||||
import java.util.LinkedList; |
||||
import java.util.List; |
||||
|
||||
class HeaderValueHolder { |
||||
private final List<Object> values = new LinkedList(); |
||||
|
||||
HeaderValueHolder() { |
||||
} |
||||
|
||||
void setValue(@Nullable Object value) { |
||||
this.values.clear(); |
||||
if (value != null) { |
||||
this.values.add(value); |
||||
} |
||||
|
||||
} |
||||
|
||||
void addValue(Object value) { |
||||
this.values.add(value); |
||||
} |
||||
|
||||
void addValues(Collection<?> values) { |
||||
this.values.addAll(values); |
||||
} |
||||
|
||||
void addValueArray(Object values) { |
||||
CollectionUtils.mergeArrayIntoCollection(values, this.values); |
||||
} |
||||
|
||||
List<Object> getValues() { |
||||
return Collections.unmodifiableList(this.values); |
||||
} |
||||
|
||||
List<String> getStringValues() { |
||||
List<String> stringList = new ArrayList(this.values.size()); |
||||
Iterator var2 = this.values.iterator(); |
||||
|
||||
while(var2.hasNext()) { |
||||
Object value = var2.next(); |
||||
stringList.add(value.toString()); |
||||
} |
||||
|
||||
return Collections.unmodifiableList(stringList); |
||||
} |
||||
|
||||
@Nullable |
||||
Object getValue() { |
||||
return !this.values.isEmpty() ? this.values.get(0) : null; |
||||
} |
||||
|
||||
@Nullable |
||||
String getStringValue() { |
||||
return !this.values.isEmpty() ? String.valueOf(this.values.get(0)) : null; |
||||
} |
||||
|
||||
} |
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,68 @@
|
||||
package com.logpm.factorydata.base.config; |
||||
|
||||
import cn.hutool.json.JSONObject; |
||||
import cn.hutool.json.JSONUtil; |
||||
import org.springblade.common.model.MockHttpServletRequest; |
||||
import org.springblade.core.secure.BladeUser; |
||||
import org.springblade.core.secure.utils.AuthUtil; |
||||
import org.springblade.core.tool.utils.ThreadLocalUtil; |
||||
import org.springblade.core.tool.utils.WebUtil; |
||||
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.web.context.request.RequestContextHolder; |
||||
import org.springframework.web.context.request.ServletRequestAttributes; |
||||
|
||||
import javax.servlet.http.HttpServletRequest; |
||||
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.common.model.MockHttpServletRequest; |
||||
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.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