Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feature]Support config email server on web-ui #953

Merged
merged 1 commit into from
May 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,9 @@
import org.dromara.hertzbeat.manager.component.alerter.AlertNotifyHandler;
import org.dromara.hertzbeat.manager.config.MailConfigProperties;
import org.dromara.hertzbeat.manager.dao.GeneralConfigDao;
import org.dromara.hertzbeat.manager.pojo.dto.MailConfig;
import org.dromara.hertzbeat.manager.pojo.dto.NoticeSender;
import org.dromara.hertzbeat.manager.service.MailService;
import org.dromara.hertzbeat.manager.support.exception.AlertNoticeException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl;
Expand Down Expand Up @@ -80,12 +79,12 @@ public void send(NoticeReceiver receiver, Alert alert) throws AlertNoticeExcepti
//若启用数据库配置
if (enabled == true) {
String content = generalConfigFoundByType.getContent();
MailConfig mailConfigByfind = objectMapper.readValue(content, MailConfig.class);
sender.setHost(mailConfigByfind.getHost());
sender.setPort(mailConfigByfind.getPort());
sender.setUsername(mailConfigByfind.getUsername());
sender.setPassword(mailConfigByfind.getPassword());
emailFromUser = mailConfigByfind.getUsername();
NoticeSender noticeSenderByfind = objectMapper.readValue(content, NoticeSender.class);
sender.setHost(noticeSenderByfind.getEmailHost());
sender.setPort(noticeSenderByfind.getEmailPort());
sender.setUsername(noticeSenderByfind.getEmailUsername());
sender.setPassword(noticeSenderByfind.getEmailPassword());
emailFromUser = noticeSenderByfind.getEmailUsername();
}
//若启用yml配置
else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ public class MailConfigProperties {
private String username;
private String password;
private Integer port;
private String defaultEncoding;

@Bean
public JavaMailSender javaMailSender() {
Expand All @@ -42,7 +41,7 @@ public JavaMailSender javaMailSender() {
props.put("spring.mail.smtp.socketFactory.port", port);
props.put("spring.mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.put("spring.mail.debug", "false");
props.put("spring.mail.default-encoding", defaultEncoding);
props.put("spring.mail.default-encoding", "UTF-8");

return mailSender;
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package org.dromara.hertzbeat.manager.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.dromara.hertzbeat.common.entity.dto.Message;
import org.dromara.hertzbeat.manager.pojo.dto.NoticeSender;
import org.dromara.hertzbeat.manager.service.impl.MailGeneralConfigServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;


import javax.validation.Valid;

import java.util.List;

import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE;

/**
* Alert sender Configuration API
* 告警发送端配置API
*
* @author zqr10159
*
*/
@RestController
@RequestMapping(value = "/api/config", produces = {APPLICATION_JSON_VALUE})
@Tag(name = "Alert sender Configuration API | 告警发送端配置API")
@Slf4j
public class NoticeSenderConfigController {
@Autowired
private MailGeneralConfigServiceImpl mailConfigService;

@PostMapping(path = "/sender")
@Operation(summary = "Save the sender config", description = "保存发送端配置")
public ResponseEntity<Message<String>> saveOrUpdateConfig(
@RequestBody @Valid NoticeSender noticeSender) {
mailConfigService.saveConfig(noticeSender, noticeSender.isEmailEnable());
return ResponseEntity.ok(new Message<>("发送端配置保存成功|The sender configuration is saved successfully"));
}

@GetMapping(path = "/senders")
@Operation(summary = "Get the sender config", description = "获取发送端配置")
public ResponseEntity<Message<List<NoticeSender>>> getConfig(){
List<NoticeSender> senders = mailConfigService.getConfigs();
return ResponseEntity.ok(new Message<>(senders));
}

@DeleteMapping(path = "/sender/{id}")
@Operation(summary = "Delete the sender config", description = "删除发送端配置")
public ResponseEntity<Message<String>> deleteConfig(){
mailConfigService.deleteConfig();
return ResponseEntity.ok(new Message<>("发送端配置删除成功|The sender configuration is deleted"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,29 @@

/**
* @description: 邮件账号配置dto
*
* @author zqr
* @date 2023/5/7 19:56
* @version 1.0
*/
@Data
public class MailConfig {
public class NoticeSender {
@NotNull(message = "类型不能为空|Type cannot be empty")
private Integer type;
@NotBlank(message = "邮件主机不能为空|Mail host cannot be empty")
private String host;
private String emailHost;

@NotBlank(message = "用户名不能为空|Username cannot be empty")
@Email
private String username;
private String emailUsername;

@NotBlank(message = "密码不能为空|Password cannot be empty")
private String password;
private String emailPassword;

@NotNull(message = "邮件端口不能为空|Mail port cannot be null")
@Max(message = "邮件端口不得大于65535|Mail port must be less than or equal to 65535", value = 65535)
@Min(message = "邮件端口不得小于1|Mail port must be greater than or equal to 1", value = 1)
private Integer port;
private Integer emailPort;

private boolean enabled;
private boolean emailEnable;
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.dromara.hertzbeat.manager.service;

import java.util.List;

/**
* ConfigService接口,提供配置的增删查改操作
* ConfigService interface provides CRUD operations for configurations.
Expand All @@ -24,4 +26,9 @@ public interface GeneralConfigService<T> {
* Get a configuration.
*/
T getConfig();
/*
* 获取所有配置
* Get all configurations.
*/
List<T> getConfigs();
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
import org.dromara.hertzbeat.manager.service.GeneralConfigService;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Slf4j
abstract class AbstractGeneralConfigServiceImpl<T> implements GeneralConfigService<T> {
protected final GeneralConfigDao generalConfigDao;
Expand Down Expand Up @@ -62,9 +65,25 @@ public T getConfig() {
try {
return objectMapper.readValue(generalConfig.getContent(), getTypeReference());
} catch (JsonProcessingException e) {
throw new RuntimeException("获取设备失败|Get configuration failed");
throw new RuntimeException("获取配置失败|Get configuration failed");
}
}

@Override
public List<T> getConfigs() {
List<GeneralConfig> configs = generalConfigDao.findAll();
List<T> result = new ArrayList<>();
for (GeneralConfig config : configs) {
try {
T t = objectMapper.readValue(config.getContent(), getTypeReference());
result.add(t);
} catch (JsonProcessingException e) {
throw new RuntimeException("获取配置失败|Get configuration failed");
}
}
return result;
}

protected abstract TypeReference<T> getTypeReference();

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,29 @@
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.dromara.hertzbeat.manager.dao.GeneralConfigDao;
import org.dromara.hertzbeat.manager.pojo.dto.MailConfig;
import org.dromara.hertzbeat.manager.pojo.dto.NoticeSender;
import org.springframework.stereotype.Service;

import java.lang.reflect.Type;


@Service
public class MailGeneralConfigServiceImpl extends AbstractGeneralConfigServiceImpl<MailConfig> {
public class MailGeneralConfigServiceImpl extends AbstractGeneralConfigServiceImpl<NoticeSender> {
public MailGeneralConfigServiceImpl(GeneralConfigDao generalConfigDao, ObjectMapper objectMapper) {
super(generalConfigDao, objectMapper, (byte) 2);
}

@Override
public void saveConfig(MailConfig config, boolean enabled) {
super.saveConfig(config, config.isEnabled());
public void saveConfig(NoticeSender config, boolean enabled) {
super.saveConfig(config, config.isEmailEnable());
}

@Override
protected TypeReference<MailConfig> getTypeReference() {
protected TypeReference<NoticeSender> getTypeReference() {
return new TypeReference<>() {
@Override
public Type getType() {
return MailConfig.class;
return NoticeSender.class;
}
};
}
Expand Down
17 changes: 6 additions & 11 deletions manager/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -87,19 +87,14 @@ spring:
# Attention: this is mail server address.
# 请注意此为邮件服务器地址:qq邮箱为 smtp.qq.com qq企业邮箱为 smtp.exmail.qq.com
host: smtp.qq.com
username: example@tancloud.cn
username: tancloud@qq.com
# Attention: this is not email account password, this requires an email authorization code
# 请注意此非邮箱账户密码 此需填写邮箱授权码
password: example
port: 465
default-encoding: UTF-8
properties:
mail:
smtp:
socketFactoryClass: javax.net.ssl.SSLSocketFactory
ssl:
enable: true
debug: false
password: yourpassword
#Attention: Tencent mail smtps 465,smtp 587
#请注意腾讯邮箱465为smtps,587为smtp
port: 587


warehouse:
store:
Expand Down
15 changes: 15 additions & 0 deletions web-app/src/app/pojo/NoticeSender.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
export class NoticeSender {
id!: number;
name!: string;
// 通知信息方式: 1-短信 2-邮箱
type: number = 2;
emailHost!: string;
emailPort!: number;
emailUsername!: string;
emailPassword!: string;
emailEnable!: boolean;
creator!: string;
modifier!: string;
gmtCreate!: number;
gmtUpdate!: number;
}
Loading