BlogAnnounced at MongoDB.local NYC 2024: A recap of all announcements and updatesLearn more >>
MongoDB Developer
Java
plus
Sign in to follow topics
MongoDB Developer Centerchevron-right
Developer Topicschevron-right
Languageschevron-right
Javachevron-right

Connection to MongoDB With Java And SOCKS5 Proxy

Maxime Beugnet2 min read • Published Apr 17, 2024 • Updated Apr 17, 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.

Facebook Icontwitter iconlinkedin icon
Rate this tutorial
star-empty
star-empty
star-empty
star-empty
star-empty
Related
Code Example

Build a MongoDB Spring Boot Java Book Tracker for Beginners


Mar 22, 2023 | 0 min read
Tutorial

How to Update Realm SDK Database Schema for Android


Sep 02, 2022 | 2 min read
Tutorial

Microservices Architecture With Java, Spring, and MongoDB


Apr 17, 2024 | 4 min read
Article

Java 21: Unlocking the Power of the MongoDB Java Driver With Virtual Threads


Jan 31, 2024 | 2 min read
Table of Contents
  • Introduction