Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6558921
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T13:15:33+00:00 2026-05-25T13:15:33+00:00

We have an app running on JBoss. In many installations the server is running

  • 0

We have an app running on JBoss. In many installations the server is running behind a firewall that denies it access to the internet except through a proxy.
Now my task is to find out how to use this proxy when authentication is necessary.

Configuring JBoss to use a proxy is no problem with -Dhttp.proxyHost=proxy_host -Dhttp.proxyPort=proxy_port, but I see no way to indicate the username and password.

On a non-EJB-app I have had success using Authenticator.setDefault(new ProxyAuthenticator("test", "test")) where ProxyAuthenticator is extending Authenticator. This, however, does not work on JBoss.

A sub-problem I have to this case is that the server and the non-EJB-app needs to have access to local resources without using the proxy.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T13:15:34+00:00Added an answer on May 25, 2026 at 1:15 pm

    Finally I got this this to work. With the two links in Richs post and some trial and error it now works as requiered.
    At the moment I have only implemented basic authentication and I will have to add other authentication types in the future.

    A big obstruction was that I started configuring the JVM with -Dhttp.proxyHost and -Dhttp.proxyPort. That somehow confused the JVM more than it helped. With that configuration the ProxyAuthenticator.getPasswordAuthentication() was never called. So it is necessary also to set a default ProxySelector.

    The code leads everything through the proxy – also calls to local addresses. Soon I’ll need to work on a solution to this 🙂 (Any ideas?)

    This is what I do to set it up:

    ProxySelector proxySelector;
    if (proxySelector == null) {
        proxySelector = new MyProxySelector(ProxySelector.getDefault(), address, port);
    }
    
    ProxySelector.setDefault(proxySelector);
    Authenticator.setDefault(ProxyAuthenticator.getInstance());
    

    MyProxySelector:

    import java.io.IOException;
    import java.net.*;
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    
    public class MyProxySelector extends ProxySelector {
        /**
         * Keep a reference on the default ProxySelector
         */
        private ProxySelector defaultProxySelector = null;
        private static ProxySelector proxySelector;
    
        /*
         * Inner class representing a Proxy and a few extra data
         */
        private class InnerProxy {
            Proxy proxy;
            SocketAddress addr;
             // How many times did we fail to reach this proxy?
            int failedCount = 0;
    
            InnerProxy(InetSocketAddress a) {
                addr = a;
                proxy = new Proxy(Proxy.Type.HTTP, a);
            }
    
            SocketAddress address() {
                return addr;
            }
    
            Proxy toProxy() {
                return proxy;
            }
    
            int failed() {
                return ++failedCount;
            }
        }
    
        /* A list of proxies, indexed by their address. */
        private HashMap<SocketAddress, InnerProxy> proxies = new HashMap<SocketAddress, InnerProxy>();
    
        public MyProxySelector(ProxySelector def, String address, Integer port) {
            // Save the previous default
            defaultProxySelector = def;
    
            // Populate the HashMap (List of proxies)
            InnerProxy i;
            if (address != null && port != null) {
                i = new InnerProxy(new InetSocketAddress(address, port));
                proxies.put(i.address(), i);
            }
        }
    
        /**
         * This is the method that the handlers will call.
         *
         * @param uri
         * @return a List of proxies.
         */
        public List<Proxy> select(URI uri) {
            if (uri == null) {
                throw new IllegalArgumentException("URI can't be null.");
            }
    
            // If it's a http (or https) URL, then we use our own
            // list.
            String protocol = uri.getScheme();
            if ("http".equalsIgnoreCase(protocol) || "https".equalsIgnoreCase(protocol)) {
                List<Proxy> proxyList = new ArrayList<Proxy>();
                for (InnerProxy p : proxies.values()) {
                    proxyList.add(p.toProxy());
                }
    
                if (proxyList.size() == 0) {
                    proxyList.add(Proxy.NO_PROXY);
                }
                return proxyList;
            }
    
             // Not HTTP or HTTPS (could be SOCKS or FTP)
             // defer to the default selector.
            if (defaultProxySelector != null) {
                return defaultProxySelector.select(uri);
            } else {
                List<Proxy> proxyList = new ArrayList<Proxy>();
                proxyList.add(Proxy.NO_PROXY);
                return proxyList;
            }
        }
    
        /**
         * Method called by the handlers when it failed to connect
         * to one of the proxies returned by select().
         *
         * @param uri
         * @param sa
         * @param ioe
         */
        public void connectFailed(URI uri, SocketAddress sa, IOException ioe) {
            // Let's stick to the specs again.
            if (uri == null || sa == null || ioe == null) {
                throw new IllegalArgumentException("Arguments can't be null.");
            }
    
            // Let's lookup for the proxy
            InnerProxy p = proxies.get(sa);
            if (p != null) {
                // It's one of ours, if it failed more than 3 times
                // let's remove it from the list.
                if (p.failed() >= 3)
                    proxies.remove(sa);
            } else {
                // Not one of ours, let's delegate to the default.
                if (defaultProxySelector != null)
                    defaultProxySelector.connectFailed(uri, sa, ioe);
            }
        }
    }
    

    ProxyAuthenticator:

    import org.bouncycastle.crypto.RuntimeCryptoException;
    
    import java.net.Authenticator;
    import java.net.PasswordAuthentication;
    
    public class ProxyAuthenticator extends Authenticator {
    
        private String user;
        private String password;
        private static ProxyAuthenticator authenticator;
    
        public ProxyAuthenticator(String user, String password) {
            this.user = user;
            this.password = password;
        }
    
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(user, password.toCharArray());
        }
    
        public static Authenticator getInstance(String user, String password) {
            if (authenticator == null) {
                authenticator = new ProxyAuthenticator(user, password);
            }
            return authenticator;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an app running on my production server that uses the pg gem
I'm building a Spring 3.0 app that is running on JBoss 6.x. I have
I have an app running Rails 2.3.5 that has a JSON API for much
I have an iPhone app running in the simulator that won't quit. I also
I have a BlackBerry app running in the background that needs to know when
I have a BlackBerry app running in the background that needs to know when
I have my app running and tested on 10.7 but later realized that I
We have an app running on JBoss. This app has one or two bugs
I have an app running on iOS 5.1 built with Xcode 4.31 that plays
I have an app running. I want to create a UserNotifications migration that belongs_to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.