Sunday Sep 01

Key Concepts

How to configure Maven behind an auto configured proxy

PDFPrintE-mail
Tuesday, 28 June 2011 12:28
AddThis Social Bookmark Button

Sometimes you are behind a proxy but you don't know the configuration details. You customer is using a Web Proxy Autodiscovery Protocol (aka WPAP). 

In internet Explorer or Firefox you probably have the Automatic detects settings checked in your configuration. In the other hand you have Maven who wants to access the internet to download the plugins and libraries. In the following tutorial, i will give you a step by step tutorial to configure your Maven settings.xml file to use the proxy settings

Retrieve the actual settings behind the Proxy Auto-Config (PAC)

Before fetching its first page, your favorite web browser implementing the auto configuration sends the local DHCP server a DHCPINFORM query, and uses the URL from the WPAD option in the server's reply. If the DHCP server does not provide the desired information, DNS is used.

If, for example, the network name of the user's computer is station.department.branch.ubiteck.com, the browser will try the following URLs in turn until it finds a proxy configuration file within the domain of the client:

  1. http://wpad.department.branch.ubiteck.com/wpad.dat 
  2. http://wpad.branch.ubiteck.com/wpad.dat
  3. http://wpad.ubiteck.com/wpad.dat 
  4. http://wpad.com/wpad.dat 

After having downloaded this file you should see something like that inside :

wdap.dat sample proxy configuration

function FindProxyForURL(url, host) {
// If URL has no dots in host name, send traffic direct.
    if (isPlainHostName(host))
        return "DIRECT";
// If specific URL needs to bypass proxy, send traffic direct.
    if (shExpMatch(url,"*domain.com*") ||
        shExpMatch(url,"*vpn.domain.com*"))                  
        return "DIRECT";
// If IP address is internal or hostname resolves to internal IP, send direct.
    var resolved_ip = dnsResolve(host);
    if (isInNet(resolved_ip, "10.0.0.0", "255.0.0.0") ||
        isInNet(resolved_ip, "172.16.0.0",  "255.240.0.0") ||
        isInNet(resolved_ip, "192.168.0.0", "255.255.0.0") ||
        isInNet(resolved_ip, "127.0.0.0", "255.255.255.0"))
        return "DIRECT";
                    
// If not on a internal/LAN IP address, send traffic direct.
    if (!isInNet(myIpAddress(), "10.10.1.0", "255.255.255.0"))
        return "DIRECT";
// All other traffic uses below proxies, in fail-over order.
    return "PROXY 1.2.3.4:8080; PROXY 4.5.6.7:8080; DIRECT";
    
}

The most important information is the proxy information returned at line 21 :

return "PROXY 1.2.3.4:8080; PROXY 4.5.6.7:8080; DIRECT";

You have two proxy addresses with there corresponding port. In our configuration example we will take those addresses to illustrate the maven configuration

Configure Maven according to the Automatic detects settings of your browser

You have know the primary and secondary proxy ip addresses and ports. We are know able to configure the proxy in our Maven settings.xml file.

Include the following snippet in the <proxies> section of your settings.xml-file:

proxy maven settings example

<settings>
  ...
  <proxies>
   <proxy>
      <active>true</active>
      <protocol>http</protocol>
      <host>1.2.3.4</host>
      <port>8080</port>
      <username>proxyuser</username>
      <password>somepassword</password>
      <nonProxyHosts>www.google.com|*.somewhere.com</nonProxyHosts>
    </proxy>
  </proxies>
  ....
</settings>

The <proxy> element contains informations required to a proxy settings.

ElementTypeDescription
active boolean Whether this proxy configuration is the active one.
Default value is: true.
protocol String The proxy protocol.
Default value is: http.
username String The proxy user.
password String The proxy password.
port int The proxy port.
Default value is: 8080.
host String The proxy host.
nonProxyHosts String The list of non-proxied hosts (delimited by |).
id String No description.
Default value is: default.

If your proxy requires a user name and password you can add it in the proxy section. The nonProxyHosts setting accepts wild cards, and each host not to proxy is separated by the | character. This matches the JDK configuration equivalent.

Source : Configuring a proxy

You are now able to download plugins and artifacts from your favorite repository.

If you have any remark or questions feel free to put a comment.If you enjoyed this tutorial and want to promote it don't hesitate to click on </p>

Tags: maven, http, configuration, password, configure, proxy, settings, string, nonproxyhosts

Add comment


Security code
Refresh

Java Tutorial on Facebook