This guide shows how to secure ClickHouse connections in Spring Boot applications with authentication, SSL, and restricted access.
1. Enable User Authentication in ClickHouse
Create a dedicated user with limited permissions.
<!-- users.xml -->
<users>
<app_user>
<password>strongpassword</password>
<networks>
<ip>::/0</ip>
</networks>
<profile>default</profile>
<quota>default</quota>
</app_user>
</users>
2. Configure Spring Boot with Credentials
spring.datasource.url=jdbc:clickhouse://localhost:8443/default?ssl=true
spring.datasource.username=app_user
spring.datasource.password=strongpassword
spring.datasource.driver-class-name=com.clickhouse.jdbc.ClickHouseDriver
3. Use SSL for Secure Connections
Enable TLS in ClickHouse server and use SSL options in JDBC.
spring.datasource.url=jdbc:clickhouse://localhost:8443/default?ssl=true&sslmode=STRICT
4. Restrict Network Access
Allow only specific IPs in
users.xml.
<networks>
<ip>192.168.1.100</ip>
</networks>
5. Parameterized Queries
Always use prepared statements to avoid SQL injection.
@Query(value = "SELECT * FROM events WHERE type = :type", nativeQuery = true)
List<Event> findByType(@Param("type") String type);
6. Encrypt Sensitive Data
Use Java libraries like JCE or Spring Security Crypto for encrypting fields before inserting into ClickHouse.
7. Monitor Security
- Check
system.query_log for suspicious queries.
- Use quotas and roles to limit query abuse.
image quote pre code