Master Druid Monitoring in Spring Boot: Step‑by‑Step Setup and Ad Removal
This guide walks you through creating a Spring Boot project, integrating the Druid connection‑pool, configuring its WebStatFilter and StatViewServlet for full SQL monitoring, testing the setup, and removing the built‑in advertisement via a custom filter.
Preparation
Start by creating a Spring Boot project and adding MyBatis and MySQL driver dependencies.
<code>spring.datasource.username=root
spring.datasource.password=123
spring.datasource.url=jdbc:mysql:///test05?serverTimezone=Asia/Shanghai</code>Create a simple
Userentity, mapper, service and controller to query users by username.
<code>public class User {
private Integer id;
private String username;
private String address;
private String password;
private String email;
// getters/setters omitted
}
@Mapper
public interface UserMapper {
List<User> getUserByUsername(String username);
}
@Service
public class UserService {
@Autowired
UserMapper userMapper;
public List<User> getUserByUsername(String username) {
return userMapper.getUserByUsername(username);
}
}
@RestController
public class UserController {
@Autowired
UserService userService;
@GetMapping("/user")
public List<User> getUser(String username) {
return userService.getUserByUsername(username);
}
}</code>Introduce Druid
Add the Druid starter dependency:
<code><dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.2.8</version>
</dependency></code>Configure
WebStatFilterin
application.propertiesto collect web‑JDBC statistics:
<code># Enable WebStatFilter
spring.datasource.druid.web-stat-filter.enabled=true
# URL pattern to filter
spring.datasource.druid.web-stat-filter.url-pattern=/*
# Exclusions (static resources, Druid UI, etc.)
spring.datasource.druid.web-stat-filter.exclusions=*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*
# Enable session statistics
spring.datasource.druid.web-stat-filter.session-stat-enable=true
spring.datasource.druid.web-stat-filter.session-stat-max-count=1000</code>Enable the built‑in monitoring servlet:
<code># Enable StatViewServlet
spring.datasource.druid.stat-view-servlet.enabled=true
# URL for the monitoring UI
spring.datasource.druid.stat-view-servlet.url-pattern=/druid/*
# Allow reset of statistics
spring.datasource.druid.stat-view-servlet.reset-enable=true
# Login credentials
spring.datasource.druid.stat-view-servlet.login-username=javaboy
spring.datasource.druid.stat-view-servlet.login-password=123
# Whitelist (allow all if empty)
spring.datasource.druid.stat-view-servlet.allow=127.0.0.1
# Blacklist (deny takes precedence)
spring.datasource.druid.stat-view-servlet.deny=</code>Note: even if
reset-enableis set to
false, the reset button still appears but does nothing.
Testing
Run the Spring Boot application and open
http://localhost:8080/druid/login.html. Log in with the credentials configured above. After a successful login you will see the full monitoring dashboard showing data source, SQL monitoring, firewall, etc.
Invoke a test endpoint, e.g.
http://localhost:8080/user?username=aaa, then return to the Druid UI to see the recorded SQL execution.
Remove Advertisement
The default Druid UI includes an advertisement injected by
common.js. To suppress it, create a servlet filter that intercepts requests to
/druid/js/common.js, reads the original file, removes the call to
this.buildFooter();, and writes the modified content back.
<code>@WebFilter(urlPatterns = "/druid/js/common.js")
public class RemoveAdFilter implements Filter {
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
String text = Utils.readFromResource("support/http/resources/js/common.js");
text = text.replace("this.buildFooter();", "");
response.getWriter().write(text);
}
}
</code>Make sure the filter package is scanned by adding
@ServletComponentScan("org.javaboy.druid_monitor.filter")to the main application class:
<code>@SpringBootApplication
@ServletComponentScan("org.javaboy.druid_monitor.filter")
public class DruidMonitorApplication {
public static void main(String[] args) {
SpringApplication.run(DruidMonitorApplication.class, args);
}
}
</code>After redeploying, the advertisement footer disappears from the Druid UI.
Reference
Official Druid documentation: https://github.com/alibaba/druid/wiki/
macrozheng
Dedicated to Java tech sharing and dissecting top open-source projects. Topics include Spring Boot, Spring Cloud, Docker, Kubernetes and more. Author’s GitHub project “mall” has 50K+ stars.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.