Skip to main content

๐ŸŒ How Java Handles Proxy in Private Networks: Calling Azure Token URL Explained

๐ŸŒ Understanding Proxy, Bypass & Calling Azure Token URL from Private Network

1️⃣ What is a Proxy? ๐Ÿค”

A proxy server acts as an intermediary between your computer (or app) and the internet. It forwards your requests and responses to control or monitor network traffic, improve security, or cache content.

  • In private networks (like company intranets), direct internet access might be blocked.
  • All outgoing calls must go through a proxy to access external services (e.g., Azure token endpoint).

2️⃣ Why do we need to configure or bypass Proxy? ๐Ÿ›‘

When your app tries to call https://login.microsoftonline.com/{tenant}/oauth2/token, but you are inside a private network, the call may fail if proxy is not configured properly.

Depending on your company's network setup, you might:

  • Configure proxy in your Java app so the call goes through proxy.
  • Bypass proxy for certain URLs if proxy blocks or slows down calls.
  • Set proxy globally in the container or JVM environment (like in your build.gradle or Jib configuration).

3️⃣ How do we configure Proxy in Java apps? ๐Ÿ”ง

๐Ÿ”น JVM System Properties

Java uses system properties to configure proxies for HTTP/HTTPS:

-Dhttp.proxyHost=proxy.company.com
-Dhttp.proxyPort=8080
-Dhttps.proxyHost=proxy.company.com
-Dhttps.proxyPort=8080
-Dhttp.nonProxyHosts="localhost|127.0.0.1|*.company.local"

These are usually set via:

  • Command line when starting the JVM
  • In Docker container environment variables
  • In build.gradle (for Jib builds, setting environment variables)

๐Ÿ”น Programmatically Setting Proxy

 System.setProperty("https.proxyHost", "proxy.company.com"); 
 System.setProperty("https.proxyPort", "8080");
 System.setProperty("http.nonProxyHosts", "localhost|127.0.0.1|*.company.local");

4️⃣ How does Java internally handle the proxy call? ๐Ÿง 

When your Java code calls HttpURLConnection or uses libraries (RestTemplate, WebClient, etc),

  • The JVM checks these proxy system properties.
  • JDK classes involved:
    • java.net.Proxy: represents proxy settings.
    • java.net.InetSocketAddress: proxy host and port.
    • sun.net.www.protocol.http.HttpURLConnection: opens connection via proxy if set.
  • The networking stack routes traffic through the proxy server.

5️⃣ What happens when you call the Azure Token Endpoint? ๐Ÿ”„

Sequence flow:

  1. Your Java method calls HttpURLConnection.openConnection() or uses RestTemplate.
  2. The JVM checks if proxy is set (system properties or programmatically).
  3. If proxy is configured, the call is routed via proxy server (proxy.company.com:8080).
  4. Proxy forwards the request to https://login.microsoftonline.com/.
  5. Response from Azure comes back via proxy to your app.

6️⃣ Diagram: Proxy Call Flow Architecture ๐Ÿ“Š

Your Java Application
(calls Azure Token URL)
JVM Networking Stack
(HttpURLConnection, java.net.Proxy)
Proxy Server
(proxy.company.com:8080)
Azure OAuth Token Endpoint
login.microsoftonline.com
1️⃣ Call made 2️⃣ Check proxy settings 3️⃣ Forward request 4️⃣ Response sent back

7️⃣ How we configure Proxy in Gradle/Jib build? ๐Ÿ› ️

In your build.gradle or Docker build using Jib, you set environment variables so the JVM inside the container uses the proxy:

groovy Copy Edit jib { container { environment = [ "HTTP_PROXY": "http://proxy.company.com:8080", "HTTPS_PROXY": "http://proxy.company.com:8080", "NO_PROXY": "localhost,127.0.0.1,.company.local" ] } }

These environment variables are picked by the JVM or HTTP client libraries at runtime to route traffic accordingly.

8️⃣ Summary ๐Ÿ’ก

  • Proxy is a gateway between your app and the internet, often mandatory in private networks.
  • Java uses java.net.Proxy and system properties to configure proxy settings.
  • You can configure proxy globally via JVM args, environment variables, or programmatically.
  • When calling Azure Token URL, your request flows through proxy if configured.
  • Gradle Jib plugin helps you set environment variables inside container builds.
https://docs.oracle.com/javase/8/docs/technotes/guides/net/proxies.html

๐Ÿ˜‚ Developer Humor

Proxy servers are like your office receptionist — making sure every call goes through the right gate, sometimes making you wait but keeping things safe! ๐Ÿ˜„

Comments

Popular posts from this blog

๐Ÿ” Is final Really Final in Java? The Truth May Surprise You ๐Ÿ˜ฒ

๐Ÿ’ฌ “When I was exploring what to do and what not to do in Java, one small keyword caught my eye — final . I thought it meant: locked, sealed, frozen — like my fridge when I forget to defrost it.”   But guess what? Java has its own meaning of final… and it’s not always what you expect! ๐Ÿ˜… Let’s break it down together — with code, questions, confusion, jokes, and everything in between. ๐ŸŽฏ The Confusing Case: You Said It's Final... Then It Changed?! ๐Ÿซ  final List<String> names = new ArrayList <>(); names.add( "Anand" ); names.add( "Rahul" ); System.out.println(names); // [Anand, Rahul] ๐Ÿคฏ Hold on... that’s final , right?! So how on earth is it still changing ? Time to dive deeper... ๐Ÿง  Why Is It Designed Like This? Here’s the key secret: In Java, final applies to the reference , not the object it points to . Let’s decode this like a spy mission ๐Ÿ•ต️‍♂️: Imagine This: final List<String> names = new ArrayList <>(); Be...

๐ŸŽข Java Loops: Fun, Fear, and ForEach() Fails

๐ŸŒ€ Oops, I Looped It Again! — The Ultimate Java Loop Guide You Won't Forget “I remember this question from one of my early interviews — I was just 2 years into Java and the interviewer asked, ‘Which loop do you prefer and why?’” At first, I thought, “Duh! for-each is cleaner.” But then he grilled me with cases where it fails. ๐Ÿ˜ต That led me to explore all loop types, their powers, and their pitfalls. Let’s deep-dive into every major Java loop with examples &  real-world guidance so you'll never forget again. ๐Ÿ” Loop Type #1: Classic For Loop — “The Old Reliable” ✅ When to Use: You need an index You want to iterate in reverse You want full control over loop mechanics ✅ Good Example: List<String> names = List.of("A", "B", "C"); for (int i = 0; i < names.size(); i++) { System.out.println(i + ": " + names.get(i)); } ๐Ÿ”ฅ Reverse + Removal Example: List<String> item...

๐Ÿงต Virtual Threads in Java — The Ultimate Guide with Diagrams, Code & Interview Qs!

๐Ÿš€ “How are Virtual Threads different from Thread Pools?” ๐Ÿ˜ต “Are they OS threads or JVM threads?” ๐Ÿ™ƒ “Should I still use CompletableFuture?” ๐Ÿคฏ “How do I even use them in real-time microservices?” ๐Ÿง  What are Virtual Threads? Virtual Threads (introduced in Java 21 as stable ๐ŸŽ‰) are lightweight threads managed by the JVM instead of the OS kernel. ๐Ÿ‘‰ They look like normal threads, but don’t hog OS resources like traditional threads. ๐Ÿง  What is the OS Kernel? ๐Ÿ›️ OS Kernel = The Brain of the Operating System It’s the core part of your OS (Windows, Linux, Mac) that: Manages memory ๐Ÿง  Schedules threads ๐Ÿ•’ Talks to hardware ๐Ÿ’ป Handles I/O operations ๐Ÿ“จ When you create a traditional thread in Java, the JVM asks the OS Kernel to create a real OS-level thread. ๐Ÿ–ผ️ Imagine This... ┌───────────────────────────┐ │ Your Java Application │ └────────────┬──────────────┘ │ ...