net.dreamlu.iot.mqtt.spring.server.config.MqttServerConfiguration Maven / Gradle / Ivy
The newest version!
/*
* Copyright (c) 2019-2029, Dreamlu 卢春梦 ([email protected] & dreamlu.net).
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.dreamlu.iot.mqtt.spring.server.config;
import net.dreamlu.iot.mqtt.core.server.MqttServer;
import net.dreamlu.iot.mqtt.core.server.MqttServerCreator;
import net.dreamlu.iot.mqtt.core.server.auth.IMqttServerAuthHandler;
import net.dreamlu.iot.mqtt.core.server.auth.IMqttServerPublishPermission;
import net.dreamlu.iot.mqtt.core.server.auth.IMqttServerSubscribeValidator;
import net.dreamlu.iot.mqtt.core.server.auth.IMqttServerUniqueIdService;
import net.dreamlu.iot.mqtt.core.server.dispatcher.IMqttMessageDispatcher;
import net.dreamlu.iot.mqtt.core.server.event.IMqttConnectStatusListener;
import net.dreamlu.iot.mqtt.core.server.event.IMqttMessageListener;
import net.dreamlu.iot.mqtt.core.server.event.IMqttSessionListener;
import net.dreamlu.iot.mqtt.core.server.func.MqttFunctionManager;
import net.dreamlu.iot.mqtt.core.server.func.MqttFunctionMessageListener;
import net.dreamlu.iot.mqtt.core.server.interceptor.IMqttMessageInterceptor;
import net.dreamlu.iot.mqtt.core.server.session.IMqttSessionManager;
import net.dreamlu.iot.mqtt.core.server.store.IMqttMessageStore;
import net.dreamlu.iot.mqtt.core.server.support.DefaultMqttServerAuthHandler;
import net.dreamlu.iot.mqtt.spring.server.MqttServerCustomizer;
import net.dreamlu.iot.mqtt.spring.server.MqttServerFunctionDetector;
import net.dreamlu.iot.mqtt.spring.server.MqttServerTemplate;
import net.dreamlu.iot.mqtt.spring.server.event.SpringEventMqttConnectStatusListener;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.tio.core.ssl.SSLEngineCustomizer;
import org.tio.core.ssl.SslConfig;
/**
* mqtt server 配置
*
* @author L.cm
*/
@Configuration(proxyBeanMethods = false)
@ConditionalOnProperty(
prefix = MqttServerProperties.PREFIX,
name = "enabled",
havingValue = "true",
matchIfMissing = true
)
@EnableConfigurationProperties(MqttServerProperties.class)
public class MqttServerConfiguration {
@Bean
@ConditionalOnMissingBean(MqttFunctionManager.class)
public MqttFunctionManager mqttFunctionManager() {
return new MqttFunctionManager();
}
@Bean
@ConditionalOnMissingBean(IMqttMessageListener.class)
public IMqttMessageListener mqttFunctionMessageListener(MqttFunctionManager mqttFunctionManager) {
return new MqttFunctionMessageListener(mqttFunctionManager);
}
@Bean
@ConditionalOnBean(MqttFunctionManager.class)
public MqttServerFunctionDetector mqttServerFunctionDetector(ApplicationContext applicationContext,
MqttFunctionManager mqttFunctionManager) {
return new MqttServerFunctionDetector(applicationContext, mqttFunctionManager);
}
@Bean
public MqttServerCreator mqttServerCreator(MqttServerProperties properties,
ApplicationEventPublisher eventPublisher,
ObjectProvider authHandlerObjectProvider,
ObjectProvider uniqueIdServiceObjectProvider,
ObjectProvider subscribeValidatorObjectProvider,
ObjectProvider publishPermissionObjectProvider,
ObjectProvider messageDispatcherObjectProvider,
ObjectProvider messageStoreObjectProvider,
ObjectProvider sessionManagerObjectProvider,
ObjectProvider sessionListenerObjectProvider,
IMqttMessageListener messageListener,
ObjectProvider connectStatusListenerObjectProvider,
ObjectProvider messageInterceptorObjectProvider,
ObjectProvider sslCustomizers,
ObjectProvider customizers) {
MqttServerCreator serverCreator = MqttServer.create()
.name(properties.getName())
.ip(properties.getIp())
.port(properties.getPort())
.heartbeatTimeout(properties.getHeartbeatTimeout())
.keepaliveBackoff(properties.getKeepaliveBackoff())
.readBufferSize((int) properties.getReadBufferSize().toBytes())
.maxBytesInMessage((int) properties.getMaxBytesInMessage().toBytes())
.bufferAllocator(properties.getBufferAllocator())
.maxClientIdLength(properties.getMaxClientIdLength())
.maxDecodeFailCount(properties.getMaxDecodeFailCount())
.webPort(properties.getWebPort())
.websocketEnable(properties.isWebsocketEnable())
.httpEnable(properties.isHttpEnable())
.nodeName(properties.getNodeName())
.statEnable(properties.isStatEnable())
.proxyProtocolEnable(properties.isProxyProtocolEnable());
if (properties.isDebug()) {
serverCreator.debug();
}
MqttServerProperties.HttpBasicAuth httpBasicAuth = properties.getHttpBasicAuth();
if (serverCreator.isHttpEnable() && httpBasicAuth.isEnable()) {
serverCreator.httpBasicAuth(httpBasicAuth.getUsername(), httpBasicAuth.getPassword());
}
MqttServerProperties.Ssl ssl = properties.getSsl();
// ssl 配置
if (ssl.isEnabled()) {
SslConfig sslConfig = SslConfig.forServer(ssl.getKeystorePath(), ssl.getKeystorePass(), ssl.getTruststorePath(), ssl.getTruststorePass(), ssl.getClientAuth());
serverCreator.sslConfig(sslConfig);
sslCustomizers.ifAvailable(sslConfig::setSslEngineCustomizer);
}
// web 端口 ssl 配置
MqttServerProperties.Ssl webSsl = properties.getWebSsl();
// web ssl 配置
if (webSsl.isEnabled()) {
serverCreator.useSsl(webSsl.getKeystorePath(), webSsl.getKeystorePass(), webSsl.getTruststorePath(), webSsl.getTruststorePass(), webSsl.getClientAuth());
}
// 认证处理器
IMqttServerAuthHandler authHandler = authHandlerObjectProvider.getIfAvailable(() -> {
MqttServerProperties.MqttAuth mqttAuth = properties.getAuth();
return mqttAuth.isEnable() ? new DefaultMqttServerAuthHandler(mqttAuth.getUsername(), mqttAuth.getPassword()) : null;
});
serverCreator.authHandler(authHandler);
// mqtt 内唯一id
uniqueIdServiceObjectProvider.ifAvailable(serverCreator::uniqueIdService);
// 订阅校验
subscribeValidatorObjectProvider.ifAvailable(serverCreator::subscribeValidator);
// 订阅权限校验
publishPermissionObjectProvider.ifAvailable(serverCreator::publishPermission);
// 消息转发
messageDispatcherObjectProvider.ifAvailable(serverCreator::messageDispatcher);
// 消息存储
messageStoreObjectProvider.ifAvailable(serverCreator::messageStore);
// session 管理
sessionManagerObjectProvider.ifAvailable(serverCreator::sessionManager);
// session 监听
sessionListenerObjectProvider.ifAvailable(serverCreator::sessionListener);
// 状态监听
IMqttConnectStatusListener connectStatusListener = connectStatusListenerObjectProvider.getIfAvailable(() ->
new SpringEventMqttConnectStatusListener(eventPublisher, serverCreator)
);
serverCreator.connectStatusListener(connectStatusListener);
// 自定义消息监听
serverCreator.messageListener(messageListener);
// 消息拦截器
messageInterceptorObjectProvider.orderedStream().forEach(serverCreator::addInterceptor);
// 自定义处理
customizers.ifAvailable((customizer) -> customizer.customize(serverCreator));
return serverCreator;
}
@Bean
public MqttServer mqttServer(MqttServerCreator mqttServerCreator) {
return mqttServerCreator.build();
}
@Bean
public MqttServerLauncher mqttServerLauncher(MqttServer mqttServer) {
return new MqttServerLauncher(mqttServer);
}
@Bean
public MqttServerTemplate mqttServerTemplate(MqttServer mqttServer) {
return new MqttServerTemplate(mqttServer);
}
}