解决:通过添加用户名、密码登录才能访问ip:端口号/druid/index.html
步骤一: 配置application.yml【不启用druid监控页+访问登录的用户名密码等】
XML
spring:
datasource:
druid:
stat-view-servlet:
enabled: false #是否启用StatViewServlet(监控页面),默认true-启动false-不启动:http://ip:端口号/druid/api.html
druidLogin: #druid监控页登录账号
loginUsername: druidUser
loginPassword: druidPassword
resetEnable: false
步骤二: 新增配置类【注册一个关闭druid监控页面的Bean】
java
@Configuration
public class DruidConfig {
@Value("${druidLogin.loginUsername}")
private String loginUsername;
@Value("${druidLogin.loginPassword}")
private String loginPassword;
@Value("${druidLogin.resetEnable}")
private String resetEnable;
/**
* 注册一个关闭Druid监控页面的Bean---解决Druid未授权访问
* @return
*/
@Bean
public ServletRegistrationBean<StatViewServlet> druidServlet(){
StatViewServlet servlet=new StatViewServlet();
ServletRegistrationBean<StatViewServlet> registrationBean=new ServletRegistrationBean<>(servlet,"/druid/*");
//设置初始化参数
registrationBean.addInitParameter("loginUsername",loginUsername);
registrationBean.addInitParameter("loginPassword",loginPassword);
registrationBean.addInitParameter("resetEnable",resetEnable);
return registrationBean;
}
}
**部署后访问效果:**ip:端口号/druid/index.html
参考网址:解决spring boot关闭druid监控页面的具体操作步骤_mob649e8152a959的技术博客_51CTO博客