Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
mica
mica-mqtt
提交
e82145d6
mica-mqtt
项目概览
mica
/
mica-mqtt
通知
71
Star
1
Fork
1
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
mica-mqtt
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
e82145d6
编写于
1月 11, 2022
作者:
如梦技术
🐛
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
✨
mqtt 自定义接口的异常处理
上级
ee894a28
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
77 addition
and
6 deletion
+77
-6
mica-mqtt-core/src/main/java/net/dreamlu/iot/mqtt/core/server/auth/IMqttServerAuthHandler.java
...mlu/iot/mqtt/core/server/auth/IMqttServerAuthHandler.java
+22
-0
mica-mqtt-core/src/main/java/net/dreamlu/iot/mqtt/core/server/auth/IMqttServerPublishPermission.java
...t/mqtt/core/server/auth/IMqttServerPublishPermission.java
+22
-0
mica-mqtt-core/src/main/java/net/dreamlu/iot/mqtt/core/server/auth/IMqttServerSubscribeValidator.java
.../mqtt/core/server/auth/IMqttServerSubscribeValidator.java
+21
-0
mica-mqtt-core/src/main/java/net/dreamlu/iot/mqtt/core/server/support/DefaultMqttServerProcessor.java
.../mqtt/core/server/support/DefaultMqttServerProcessor.java
+12
-6
未找到文件。
mica-mqtt-core/src/main/java/net/dreamlu/iot/mqtt/core/server/auth/IMqttServerAuthHandler.java
浏览文件 @
e82145d6
...
...
@@ -16,6 +16,8 @@
package
net.dreamlu.iot.mqtt.core.server.auth
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
org.tio.core.ChannelContext
;
/**
...
...
@@ -25,6 +27,26 @@ import org.tio.core.ChannelContext;
*/
@FunctionalInterface
public
interface
IMqttServerAuthHandler
{
Logger
logger
=
LoggerFactory
.
getLogger
(
IMqttServerAuthHandler
.
class
);
/**
* 认证
*
* @param context ChannelContext
* @param uniqueId mqtt 内唯一id,默认和 clientId 相同
* @param clientId 客户端 ID
* @param userName 用户名
* @param password 密码
* @return 是否认证成功
*/
default
boolean
verifyAuthenticate
(
ChannelContext
context
,
String
uniqueId
,
String
clientId
,
String
userName
,
String
password
)
{
try
{
return
authenticate
(
context
,
uniqueId
,
clientId
,
userName
,
password
);
}
catch
(
Throwable
e
)
{
logger
.
error
(
"Mqtt authenticate validator error"
,
e
);
return
false
;
}
}
/**
* 认证
...
...
mica-mqtt-core/src/main/java/net/dreamlu/iot/mqtt/core/server/auth/IMqttServerPublishPermission.java
浏览文件 @
e82145d6
...
...
@@ -17,6 +17,8 @@
package
net.dreamlu.iot.mqtt.core.server.auth
;
import
net.dreamlu.iot.mqtt.codec.MqttQoS
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
org.tio.core.ChannelContext
;
/**
...
...
@@ -25,6 +27,26 @@ import org.tio.core.ChannelContext;
* @author L.cm
*/
public
interface
IMqttServerPublishPermission
{
Logger
logger
=
LoggerFactory
.
getLogger
(
IMqttServerPublishPermission
.
class
);
/**
* 否有发布权限
*
* @param context ChannelContext
* @param clientId 客户端 id
* @param topic topic
* @param qoS MqttQoS
* @param isRetain 是否保留消息
* @return 否有发布权限
*/
default
boolean
verifyPermission
(
ChannelContext
context
,
String
clientId
,
String
topic
,
MqttQoS
qoS
,
boolean
isRetain
)
{
try
{
return
hasPermission
(
context
,
clientId
,
topic
,
qoS
,
isRetain
);
}
catch
(
Throwable
e
)
{
logger
.
error
(
"Mqtt publish permission validator error"
,
e
);
return
false
;
}
}
/**
* 否有发布权限
...
...
mica-mqtt-core/src/main/java/net/dreamlu/iot/mqtt/core/server/auth/IMqttServerSubscribeValidator.java
浏览文件 @
e82145d6
...
...
@@ -17,6 +17,8 @@
package
net.dreamlu.iot.mqtt.core.server.auth
;
import
net.dreamlu.iot.mqtt.codec.MqttQoS
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
org.tio.core.ChannelContext
;
/**
...
...
@@ -25,6 +27,25 @@ import org.tio.core.ChannelContext;
* @author L.cm
*/
public
interface
IMqttServerSubscribeValidator
{
Logger
logger
=
LoggerFactory
.
getLogger
(
IMqttServerSubscribeValidator
.
class
);
/**
* 校验订阅的 topicFilter
*
* @param context ChannelContext
* @param clientId clientId
* @param topicFilter topicFilter
* @param qoS MqttQoS
* @return 是否有权限
*/
default
boolean
verifyTopicFilter
(
ChannelContext
context
,
String
clientId
,
String
topicFilter
,
MqttQoS
qoS
)
{
try
{
return
isValid
(
context
,
clientId
,
topicFilter
,
qoS
);
}
catch
(
Throwable
e
)
{
logger
.
error
(
"Mqtt subscribe validator error"
,
e
);
return
false
;
}
}
/**
* 是否可以订阅
...
...
mica-mqtt-core/src/main/java/net/dreamlu/iot/mqtt/core/server/support/DefaultMqttServerProcessor.java
浏览文件 @
e82145d6
...
...
@@ -103,7 +103,7 @@ public class DefaultMqttServerProcessor implements MqttServerProcessor {
return
;
}
// 3. 认证
if
(!
authHandler
.
a
uthenticate
(
context
,
uniqueId
,
clientId
,
userName
,
password
))
{
if
(!
authHandler
.
verifyA
uthenticate
(
context
,
uniqueId
,
clientId
,
userName
,
password
))
{
connAckByReturnCode
(
clientId
,
uniqueId
,
context
,
MqttConnectReasonCode
.
CONNECTION_REFUSED_BAD_USER_NAME_OR_PASSWORD
);
return
;
}
...
...
@@ -157,10 +157,16 @@ public class DefaultMqttServerProcessor implements MqttServerProcessor {
willMessage
.
setNode
(
nodeName
);
messageStore
.
addWillMessage
(
uniqueId
,
willMessage
);
}
// 9. 返回 ack
// 9. 在线状态
try
{
connectStatusListener
.
online
(
context
,
uniqueId
);
}
catch
(
Throwable
e
)
{
logger
.
error
(
"mqtt connectStatusListener"
,
e
);
connAckByReturnCode
(
clientId
,
uniqueId
,
context
,
MqttConnectReasonCode
.
CONNECTION_REFUSED_SERVER_UNAVAILABLE
);
return
;
}
// 10. 返回 ack
connAckByReturnCode
(
clientId
,
uniqueId
,
context
,
MqttConnectReasonCode
.
CONNECTION_ACCEPTED
);
// 10. 在线状态
connectStatusListener
.
online
(
context
,
uniqueId
);
}
private
static
void
connAckByReturnCode
(
String
clientId
,
String
uniqueId
,
ChannelContext
context
,
MqttConnectReasonCode
returnCode
)
{
...
...
@@ -199,7 +205,7 @@ public class DefaultMqttServerProcessor implements MqttServerProcessor {
MqttPublishVariableHeader
variableHeader
=
message
.
variableHeader
();
String
topicName
=
variableHeader
.
topicName
();
// 1. 判断是否有发布权限,没有权限则断开 mqtt 连接 mqtt 5.x qos1、qos2 可以响应 reasonCode
if
(
publishPermission
!=
null
&&
!
publishPermission
.
has
Permission
(
context
,
clientId
,
topicName
,
mqttQoS
,
fixedHeader
.
isRetain
()))
{
if
(
publishPermission
!=
null
&&
!
publishPermission
.
verify
Permission
(
context
,
clientId
,
topicName
,
mqttQoS
,
fixedHeader
.
isRetain
()))
{
Tio
.
remove
(
context
,
"Mqtt clientId:"
+
clientId
+
" publish topic: "
+
topicName
+
" no permission."
);
return
;
}
...
...
@@ -318,7 +324,7 @@ public class DefaultMqttServerProcessor implements MqttServerProcessor {
String
topicFilter
=
subscription
.
topicName
();
MqttQoS
mqttQoS
=
subscription
.
qualityOfService
();
// 校验是否可以订阅
if
(
enableSubscribeValidator
&&
!
subscribeValidator
.
isValid
(
context
,
clientId
,
topicFilter
,
mqttQoS
))
{
if
(
enableSubscribeValidator
&&
!
subscribeValidator
.
verifyTopicFilter
(
context
,
clientId
,
topicFilter
,
mqttQoS
))
{
grantedQosList
.
add
(
MqttQoS
.
FAILURE
);
logger
.
error
(
"Subscribe - clientId:{} topicFilter:{} mqttQoS:{} valid failed messageId:{}"
,
clientId
,
topicFilter
,
mqttQoS
,
messageId
);
}
else
{
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录