diff --git a/alerter/src/main/java/org/dromara/hertzbeat/alert/util/AlertTemplateUtil.java b/alerter/src/main/java/org/dromara/hertzbeat/alert/util/AlertTemplateUtil.java index 13fa7d59b9d..a24fe5db91d 100644 --- a/alerter/src/main/java/org/dromara/hertzbeat/alert/util/AlertTemplateUtil.java +++ b/alerter/src/main/java/org/dromara/hertzbeat/alert/util/AlertTemplateUtil.java @@ -40,6 +40,9 @@ public class AlertTemplateUtil { private static final Pattern PATTERN = Pattern.compile("\\$\\{(\\w+)\\}"); public static String render(String template, Map replaceData) { + if (template == null) { + return null; + } try { Matcher matcher = PATTERN.matcher(template); StringBuffer buffer = new StringBuffer(); diff --git a/manager/src/main/java/org/dromara/hertzbeat/manager/controller/AccountController.java b/manager/src/main/java/org/dromara/hertzbeat/manager/controller/AccountController.java index 24c5f57fb97..f0562fc8cfa 100644 --- a/manager/src/main/java/org/dromara/hertzbeat/manager/controller/AccountController.java +++ b/manager/src/main/java/org/dromara/hertzbeat/manager/controller/AccountController.java @@ -30,6 +30,7 @@ import org.dromara.hertzbeat.common.constants.CommonConstants; import org.dromara.hertzbeat.common.entity.dto.Message; import org.dromara.hertzbeat.manager.pojo.dto.LoginDto; +import org.dromara.hertzbeat.manager.pojo.dto.RefreshTokenResponse; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; @@ -102,41 +103,33 @@ public ResponseEntity>> authGetToken(@Valid @Request @GetMapping("/refresh/{refreshToken}") @Operation(summary = "Use refresh TOKEN to re-acquire TOKEN", description = "使用刷新TOKEN重新获取TOKEN") - public ResponseEntity>> refreshToken( + public ResponseEntity> refreshToken( @Parameter(description = "Refresh TOKEN | 刷新TOKEN", example = "xxx") @PathVariable("refreshToken") @NotNull final String refreshToken) { - String userId; - boolean isRefresh; try { Claims claims = JsonWebTokenUtil.parseJwt(refreshToken); - userId = String.valueOf(claims.getSubject()); - isRefresh = claims.get("refresh", Boolean.class); + String userId = String.valueOf(claims.getSubject()); + boolean isRefresh = claims.get("refresh", Boolean.class); + if (userId == null || !isRefresh) { + return ResponseEntity.ok(new Message<>(CommonConstants.MONITOR_LOGIN_FAILED_CODE, "非法的刷新TOKEN")); + } + SurenessAccount account = accountProvider.loadAccount(userId); + if (account == null) { + return ResponseEntity.ok(new Message<>(CommonConstants.MONITOR_LOGIN_FAILED_CODE, "TOKEN对应的账户不存在")); + } + List roles = account.getOwnRoles(); + String issueToken = issueToken(userId, roles, PERIOD_TIME); + String issueRefresh = issueToken(userId, roles, PERIOD_TIME << 5); + RefreshTokenResponse response = new RefreshTokenResponse(issueToken, issueRefresh); + return ResponseEntity.ok(new Message<>(response)); } catch (Exception e) { - log.info(e.getMessage()); - Message> message = Message.>builder().msg("刷新TOKEN过期或错误") - .code(CommonConstants.MONITOR_LOGIN_FAILED_CODE).build(); - return ResponseEntity.ok(message); + log.error("Exception occurred during token refresh: {}", e.getClass().getName(), e); + return ResponseEntity.ok(new Message<>(CommonConstants.MONITOR_LOGIN_FAILED_CODE, "刷新TOKEN过期或错误")); } - if (userId == null || !isRefresh) { - Message> message = Message.>builder().msg("非法的刷新TOKEN") - .code(CommonConstants.MONITOR_LOGIN_FAILED_CODE).build(); - return ResponseEntity.ok(message); - } - SurenessAccount account = accountProvider.loadAccount(userId); - if (account == null) { - Message> message = Message.>builder().msg("TOKEN对应的账户不存在") - .code(CommonConstants.MONITOR_LOGIN_FAILED_CODE).build(); - return ResponseEntity.ok(message); - } - List roles = account.getOwnRoles(); - // Issue TOKEN 签发TOKEN - String issueToken = JsonWebTokenUtil.issueJwt(userId, PERIOD_TIME, roles); + } + private String issueToken(String userId, List roles, long expirationMillis) { Map customClaimMap = new HashMap<>(1); customClaimMap.put("refresh", true); - String issueRefresh = JsonWebTokenUtil.issueJwt(userId, PERIOD_TIME << 5, customClaimMap); - Map resp = new HashMap<>(2); - resp.put("token", issueToken); - resp.put("refreshToken", issueRefresh); - return ResponseEntity.ok(new Message<>(resp)); + return JsonWebTokenUtil.issueJwt(userId, expirationMillis, roles, customClaimMap); } } diff --git a/manager/src/main/java/org/dromara/hertzbeat/manager/pojo/dto/RefreshTokenResponse.java b/manager/src/main/java/org/dromara/hertzbeat/manager/pojo/dto/RefreshTokenResponse.java new file mode 100644 index 00000000000..fbf36448821 --- /dev/null +++ b/manager/src/main/java/org/dromara/hertzbeat/manager/pojo/dto/RefreshTokenResponse.java @@ -0,0 +1,25 @@ +package org.dromara.hertzbeat.manager.pojo.dto; + +import io.swagger.v3.oas.annotations.media.Schema; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + + +/** + * Refresh Token Response + * @author Carpe-Wang + */ +@Data +@Builder +@NoArgsConstructor +@AllArgsConstructor +@Schema(description = "刷新令牌响应") +public class RefreshTokenResponse { + @Schema(title = "Access Token") + private String token; + + @Schema(title = "Refresh Token") + private String refreshToken; +} diff --git a/manager/src/main/java/org/dromara/hertzbeat/manager/service/impl/MonitorServiceImpl.java b/manager/src/main/java/org/dromara/hertzbeat/manager/service/impl/MonitorServiceImpl.java index 9cb2d6caf4c..f09b13ee182 100644 --- a/manager/src/main/java/org/dromara/hertzbeat/manager/service/impl/MonitorServiceImpl.java +++ b/manager/src/main/java/org/dromara/hertzbeat/manager/service/impl/MonitorServiceImpl.java @@ -661,7 +661,6 @@ public void enableManageMonitors(HashSet ids) { .collect(Collectors.toList()); paramDefaultValue.forEach(defaultVar -> { if (configmaps.stream().noneMatch(item -> item.getKey().equals(defaultVar.getField()))) { - // todo type Configmap configmap = new Configmap(defaultVar.getField(), defaultVar.getDefaultValue(), (byte) 1); configmaps.add(configmap); } @@ -733,29 +732,37 @@ public void copyMonitors(List ids) { }, () -> log.warn("can not find the monitor for id :{}", id)); }); } - + @Override public void updateAppCollectJob(Job job) { List availableMonitors = monitorDao.findMonitorsByAppEquals(job.getApp()). - stream().filter(monitor -> monitor.getStatus() == CommonConstants.AVAILABLE_CODE) - .collect(Collectors.toList()); + stream().filter(monitor -> monitor.getStatus() == CommonConstants.AVAILABLE_CODE) + .collect(Collectors.toList()); if (!availableMonitors.isEmpty()) { for (Monitor monitor : availableMonitors) { - // 这里暂时是深拷贝处理 Job appDefine = JsonUtil.fromJson(JsonUtil.toJson(job), Job.class); - appDefine.setMonitorId(monitor.getId()); - appDefine.setInterval(monitor.getIntervals()); + if (monitor == null || appDefine == null) { + continue; + } + if (monitor.getId() != null) { + appDefine.setMonitorId(monitor.getId()); + } else { + continue; + } + if (monitor.getIntervals() !=null ) { + appDefine.setInterval(monitor.getIntervals()); + } else { + continue; + } appDefine.setCyclic(true); appDefine.setTimestamp(System.currentTimeMillis()); - List params = paramDao.findParamsByMonitorId(monitor.getId()); List configmaps = params.stream().map(param -> new Configmap(param.getField(), param.getValue(), param.getType())).collect(Collectors.toList()); List paramDefaultValue = appDefine.getParams().stream() - .filter(item -> StringUtils.hasText(item.getDefaultValue())) - .collect(Collectors.toList()); + .filter(item -> StringUtils.hasText(item.getDefaultValue())) + .collect(Collectors.toList()); paramDefaultValue.forEach(defaultVar -> { if (configmaps.stream().noneMatch(item -> item.getKey().equals(defaultVar.getField()))) { - // todo type Configmap configmap = new Configmap(defaultVar.getField(), defaultVar.getDefaultValue(), (byte) 1); configmaps.add(configmap); }