EventGet 50% off your ticket to MongoDB.local London on October 2. Use code WEB50Learn more >>
MongoDB Developer
Java
plus
Sign in to follow topics
MongoDB Developer Centerchevron-right
Developer Topicschevron-right
Languageschevron-right
Javachevron-right

How to Connect to MongoDB With a SOCKS5 Proxy With Java

Maxime Beugnet2 min read • Published Aug 29, 2024 • Updated Aug 29, 2024
SpringMongoDBJava
Facebook Icontwitter iconlinkedin icon
Rate this tutorial
star-empty
star-empty
star-empty
star-empty
star-empty

Introduction

SOCKS5 is a standardized protocol for communicating with network services through a proxy server. It offers several advantages like allowing the users to change their virtual location or hide their IP address from the online services.
SOCKS5 also offers an authentication layer that can be used to enhance security.
In our case, the network service is MongoDB. Let's see how we can connect to MongoDB through a SOCKS5 proxy with Java.

SOCKS5 with vanilla Java

Authentication is optional for SOCKS5 proxies. So to be able to connect to a SOCKS5 proxy, you need:
  • proxyHost: IPv4, IPv6, or hostname of the proxy
  • proxyPort: TCP port number (default 1080)
If authentication is activated, then you'll also need a username and password. Both need to be provided, or it won't work.
  • proxyUsername: the proxy username (not null or empty)
  • proxyPassword: the proxy password (not null or empty)

Using connection string parameters

The first method to connect to MongoDB through a SOCKS5 proxy is to simply provide the above parameters directly in the MongoDB connection string.

Using MongoClientSettings

The second method involves passing these parameters into a MongoClientSettings class, which is then used to create the connection to the MongoDB cluster.

Connection with Spring Boot

Using connection string parameters

If you are using Spring Boot or Spring Data MongoDB, you can connect like so if you are passing the SOCKS5 parameters in the connection string.
Most of the time, if you are using Spring Boot or Spring Data, you'll need the codec registry to support the POJO mappings. So I included this as well.
In this case, all the SOCKS5 action is actually happening in the application.properties file of your Spring Boot project.

Using MongoClientSettings

If you prefer to use the MongoClientSettings, then you can just pass a classic MongoDB URI and handle the different SOCKS5 parameters directly in the SocketSettings.Builder.

Conclusion

Leveraging a SOCKS5 proxy for connecting to MongoDB in Java offers enhanced security and flexibility. Whether through connection string parameters or MongoClientSettings, integrating SOCKS5 functionality is straightforward.
If you want to read more details, you can check out the SOCKS5 documentation online.
If you have questions, please head to our Developer Community website where the MongoDB engineers and the MongoDB community will help you build your next big idea with MongoDB.
Top Comments in Forums
There are no comments on this article yet.
Start the Conversation

Facebook Icontwitter iconlinkedin icon
Rate this tutorial
star-empty
star-empty
star-empty
star-empty
star-empty
Related
Quickstart

How to Build a CRUD Application With MongoDB, Quarkus, and GraalVM


Aug 29, 2024 | 7 min read
Tutorial

How to Migrate PostgreSQL to MongoDB With Confluent Kafka


Aug 30, 2024 | 10 min read
Tutorial

Single-Collection Designs in MongoDB with Spring Data (Part 1)


Sep 09, 2024 | 10 min read
Quickstart

Java - MongoDB Multi-Document ACID Transactions


Mar 01, 2024 | 10 min read
Table of Contents
  • Introduction