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.
spring.datasource.username=root
spring.datasource.password=123
spring.datasource.url=jdbc:mysql:///test05?serverTimezone=Asia/ShanghaiCreate a simple User entity, mapper, service and controller to query users by username.
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);
}
}Introduce Druid
Add the Druid starter dependency:
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.2.8</version>
</dependency>Configure WebStatFilter in application.properties to collect web‑JDBC statistics:
# 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=1000Enable the built‑in monitoring servlet:
# 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=Note: even if reset-enable is 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.
@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);
}
}Make sure the filter package is scanned by adding @ServletComponentScan("org.javaboy.druid_monitor.filter") to the main application class:
@SpringBootApplication
@ServletComponentScan("org.javaboy.druid_monitor.filter")
public class DruidMonitorApplication {
public static void main(String[] args) {
SpringApplication.run(DruidMonitorApplication.class, args);
}
}After redeploying, the advertisement footer disappears from the Druid UI.
Reference
Official Druid documentation: https://github.com/alibaba/druid/wiki/
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
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.
