我有自己写的http认证以及http授权demo,但是不知道如何让发布代码走我的授权代码
目前测试,发布,认证,授权都是可以单独工作的,发布也会走认证,但是不走授权
以下是我的demo代码。
@Override
public String publishMessageToBroker(String topic, String msg, Integer qos) {
String broker = “tcp://127.0.0.1:1883”;
String clientId = MqttClient.generateClientId();
MemoryPersistence persistence = new MemoryPersistence();
try {
MqttClient client = new MqttClient(broker, clientId, persistence);
// 设置回调
client.setCallback(new SampleCallback());
// MQTT 连接选项
MqttConnectOptions connOpts = new MqttConnectOptions();
// 设置认证信息
connOpts.setUserName(“emqx_u”);
connOpts.setPassword(“public”.toCharArray());
client.connect(connOpts);
// 发布消息
MqttMessage mqttMessage = new MqttMessage(msg.getBytes());
mqttMessage.setQos(qos);
client.publish(topic, mqttMessage);
log.info("mqttMessage : " + mqttMessage);
return “Publish success!”;
} catch (MqttException e) {
log.error(“发布异常”, e);
return “Publish fail!”;
}
}
@Override
public ResponseEntity<LoginResponse> doLogin(String username, String password) {
LambdaQueryWrapper<MqttUser> eq = new LambdaQueryWrapper<MqttUser>()
.select(MqttUser::getPasswordHash, MqttUser::getSalt)
.eq(MqttUser::getUsername, username);
MqttUser mqttUser = mqttMapper.selectOne(eq);
if (mqttUser == null) {
return new ResponseEntity<>(HttpStatus.UNAUTHORIZED);
}
String currentPassword = DigestUtil.sha256Hex(password + mqttUser.getSalt());
if (currentPassword.equals(mqttUser.getPasswordHash())) {
// 设置响应头信息
HttpHeaders headers = new HttpHeaders();
headers.set("Content-Type", "application/json");
LoginResponse loginResponse = new LoginResponse("allow", true);
return new ResponseEntity<>(loginResponse, headers, HttpStatus.OK);
} else {
return new ResponseEntity<>(HttpStatus.UNAUTHORIZED);
}
}
@Override
public ResponseEntity<AuthResponse> checkAuth(AuthUser authUser) {
// 设置响应头信息
HttpHeaders headers = new HttpHeaders();
headers.set("Content-Type", "application/json");
AuthResponse authResponse = new AuthResponse("deny");
return new ResponseEntity<>(authResponse, headers, HttpStatus.OK);
}