mirror of
http://192.168.1.205:9980/cf_devdept2/cf_imes_server.git
synced 2026-08-13 21:32:07 +08:00
rabbitmq配置补充解密开关
This commit is contained in:
-42
@@ -1,28 +1,18 @@
|
|||||||
package com.cf.imes.framework.mq.rabbitmq.config;
|
package com.cf.imes.framework.mq.rabbitmq.config;
|
||||||
|
|
||||||
import com.cf.imes.framework.common.util.encrypt.AesUtils;
|
|
||||||
import com.cf.imes.framework.mq.rabbitmq.constant.RabbitMqConstants;
|
import com.cf.imes.framework.mq.rabbitmq.constant.RabbitMqConstants;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.apache.commons.lang3.StringUtils;
|
|
||||||
import org.springframework.amqp.core.Binding;
|
import org.springframework.amqp.core.Binding;
|
||||||
import org.springframework.amqp.core.BindingBuilder;
|
import org.springframework.amqp.core.BindingBuilder;
|
||||||
import org.springframework.amqp.core.DirectExchange;
|
import org.springframework.amqp.core.DirectExchange;
|
||||||
import org.springframework.amqp.core.FanoutExchange;
|
import org.springframework.amqp.core.FanoutExchange;
|
||||||
import org.springframework.amqp.core.Queue;
|
import org.springframework.amqp.core.Queue;
|
||||||
import org.springframework.amqp.core.QueueBuilder;
|
import org.springframework.amqp.core.QueueBuilder;
|
||||||
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
|
|
||||||
import org.springframework.amqp.rabbit.connection.RabbitConnectionFactoryBean;
|
|
||||||
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
|
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
|
||||||
import org.springframework.amqp.support.converter.MessageConverter;
|
import org.springframework.amqp.support.converter.MessageConverter;
|
||||||
import org.springframework.beans.factory.ObjectProvider;
|
|
||||||
import org.springframework.beans.factory.annotation.Value;
|
|
||||||
import org.springframework.boot.autoconfigure.AutoConfiguration;
|
import org.springframework.boot.autoconfigure.AutoConfiguration;
|
||||||
import org.springframework.boot.autoconfigure.amqp.CachingConnectionFactoryConfigurer;
|
|
||||||
import org.springframework.boot.autoconfigure.amqp.ConnectionFactoryCustomizer;
|
|
||||||
import org.springframework.boot.autoconfigure.amqp.RabbitConnectionFactoryBeanConfigurer;
|
|
||||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.Primary;
|
|
||||||
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
@@ -40,38 +30,6 @@ import static com.cf.imes.framework.mq.rabbitmq.constant.RabbitMqConstants.ORDER
|
|||||||
@Slf4j
|
@Slf4j
|
||||||
@ConditionalOnClass(name = "org.springframework.amqp.rabbit.core.RabbitTemplate")
|
@ConditionalOnClass(name = "org.springframework.amqp.rabbit.core.RabbitTemplate")
|
||||||
public class ChenfengRabbitMQAutoConfiguration {
|
public class ChenfengRabbitMQAutoConfiguration {
|
||||||
|
|
||||||
@Value("${chenfeng.encrypt.publicKey:}")
|
|
||||||
private String publicKey;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 参考 org.springframework.boot.autoconfigure.amqp.RabbitAutoConfiguration 的 rabbitConnectionFactory 方法
|
|
||||||
*/
|
|
||||||
@Bean
|
|
||||||
@Primary
|
|
||||||
CachingConnectionFactory rabbitConnectionFactory(
|
|
||||||
RabbitConnectionFactoryBeanConfigurer rabbitConnectionFactoryBeanConfigurer,
|
|
||||||
CachingConnectionFactoryConfigurer rabbitCachingConnectionFactoryConfigurer,
|
|
||||||
ObjectProvider<ConnectionFactoryCustomizer> connectionFactoryCustomizers) throws Exception {
|
|
||||||
|
|
||||||
RabbitConnectionFactoryBean connectionFactoryBean = new RabbitConnectionFactoryBean();
|
|
||||||
rabbitConnectionFactoryBeanConfigurer.configure(connectionFactoryBean);
|
|
||||||
connectionFactoryBean.afterPropertiesSet();
|
|
||||||
com.rabbitmq.client.ConnectionFactory connectionFactory = connectionFactoryBean.getObject();
|
|
||||||
|
|
||||||
if (StringUtils.isNotEmpty(publicKey)) {
|
|
||||||
connectionFactory.setUsername(AesUtils.decrypt(connectionFactory.getUsername(), publicKey));
|
|
||||||
connectionFactory.setPassword(AesUtils.decrypt(connectionFactory.getPassword(), publicKey));
|
|
||||||
}
|
|
||||||
connectionFactoryCustomizers.orderedStream()
|
|
||||||
.forEach((customizer) -> customizer.customize(connectionFactory));
|
|
||||||
|
|
||||||
CachingConnectionFactory factory = new CachingConnectionFactory(connectionFactory);
|
|
||||||
rabbitCachingConnectionFactoryConfigurer.configure(factory);
|
|
||||||
|
|
||||||
return factory;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Jackson2JsonMessageConverter Bean:使用 jackson 序列化消息
|
* Jackson2JsonMessageConverter Bean:使用 jackson 序列化消息
|
||||||
*/
|
*/
|
||||||
|
|||||||
+54
@@ -0,0 +1,54 @@
|
|||||||
|
package com.cf.imes.framework.mq.rabbitmq.config;
|
||||||
|
|
||||||
|
import com.cf.imes.framework.common.util.encrypt.AesUtils;
|
||||||
|
import org.apache.commons.lang3.StringUtils;
|
||||||
|
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
|
||||||
|
import org.springframework.amqp.rabbit.connection.RabbitConnectionFactoryBean;
|
||||||
|
import org.springframework.beans.factory.ObjectProvider;
|
||||||
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
|
import org.springframework.boot.autoconfigure.AutoConfiguration;
|
||||||
|
import org.springframework.boot.autoconfigure.amqp.CachingConnectionFactoryConfigurer;
|
||||||
|
import org.springframework.boot.autoconfigure.amqp.ConnectionFactoryCustomizer;
|
||||||
|
import org.springframework.boot.autoconfigure.amqp.RabbitConnectionFactoryBeanConfigurer;
|
||||||
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Primary;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Gqr
|
||||||
|
* @since 2025/3/13 13:53
|
||||||
|
*/
|
||||||
|
@AutoConfiguration
|
||||||
|
@ConditionalOnProperty(name = "chenfeng.encrypt.enable", havingValue = "true")
|
||||||
|
public class ChenfengRabbitMQEncryptAutoConfiguration {
|
||||||
|
@Value("${chenfeng.encrypt.publicKey:}")
|
||||||
|
private String publicKey;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 参考 org.springframework.boot.autoconfigure.amqp.RabbitAutoConfiguration 的 rabbitConnectionFactory 方法
|
||||||
|
*/
|
||||||
|
@Bean
|
||||||
|
@Primary
|
||||||
|
CachingConnectionFactory rabbitConnectionFactory(
|
||||||
|
RabbitConnectionFactoryBeanConfigurer rabbitConnectionFactoryBeanConfigurer,
|
||||||
|
CachingConnectionFactoryConfigurer rabbitCachingConnectionFactoryConfigurer,
|
||||||
|
ObjectProvider<ConnectionFactoryCustomizer> connectionFactoryCustomizers) throws Exception {
|
||||||
|
|
||||||
|
RabbitConnectionFactoryBean connectionFactoryBean = new RabbitConnectionFactoryBean();
|
||||||
|
rabbitConnectionFactoryBeanConfigurer.configure(connectionFactoryBean);
|
||||||
|
connectionFactoryBean.afterPropertiesSet();
|
||||||
|
com.rabbitmq.client.ConnectionFactory connectionFactory = connectionFactoryBean.getObject();
|
||||||
|
|
||||||
|
if (StringUtils.isNotEmpty(publicKey)) {
|
||||||
|
connectionFactory.setUsername(AesUtils.decrypt(connectionFactory.getUsername(), publicKey));
|
||||||
|
connectionFactory.setPassword(AesUtils.decrypt(connectionFactory.getPassword(), publicKey));
|
||||||
|
}
|
||||||
|
connectionFactoryCustomizers.orderedStream()
|
||||||
|
.forEach((customizer) -> customizer.customize(connectionFactory));
|
||||||
|
|
||||||
|
CachingConnectionFactory factory = new CachingConnectionFactory(connectionFactory);
|
||||||
|
rabbitCachingConnectionFactoryConfigurer.configure(factory);
|
||||||
|
|
||||||
|
return factory;
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user