feat: Add Druid monitoring controller for credential retrieval

This commit is contained in:
2026-07-16 00:29:05 +08:00
parent 708d779533
commit 95f8e0af7e

View File

@@ -0,0 +1,54 @@
package com.ruoyi.web.controller.monitor;
import java.util.HashMap;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.CacheControl;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.alibaba.druid.spring.boot.autoconfigure.properties.DruidStatProperties;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.StringUtils;
/**
* Druid 数据监控接口。
*
* @author Codex
*/
@RestController
@RequestMapping("/monitor/druid")
public class DruidController
{
@Autowired
private DruidStatProperties druidStatProperties;
/**
* 获取当前环境的 Druid 登录信息,供已授权的管理前端自动登录使用。
*
* 注意:该接口返回敏感信息,只允许拥有数据监控权限的用户访问,并禁止缓存。
*/
@PreAuthorize("@ss.hasPermi('monitor:druid:list')")
@GetMapping("/credentials")
public ResponseEntity<AjaxResult> getCredentials()
{
DruidStatProperties.StatViewServlet statViewServlet = druidStatProperties.getStatViewServlet();
if (statViewServlet == null || !statViewServlet.isEnabled()
|| StringUtils.isEmpty(statViewServlet.getLoginUsername())
|| StringUtils.isEmpty(statViewServlet.getLoginPassword()))
{
return ResponseEntity.ok()
.cacheControl(CacheControl.noStore())
.body(AjaxResult.error("Druid 监控登录配置未启用"));
}
Map<String, String> credentials = new HashMap<>(2);
credentials.put("username", statViewServlet.getLoginUsername());
credentials.put("password", statViewServlet.getLoginPassword());
return ResponseEntity.ok()
.cacheControl(CacheControl.noStore())
.body(AjaxResult.success(credentials));
}
}