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

  • Home
  • SEARCH
  • 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 9163211
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T14:23:30+00:00 2026-06-17T14:23:30+00:00

I’m trying to automate some tasks on a website using Java. I have a

  • 0

I’m trying to automate some tasks on a website using Java. I have a valid client for that website (works when I login using firefox), but I keep getting a 403 error when I try to login using http client. Note that I want my trust store to trust anything (I know it’s not safe, but at this point I am not worried about that).

Here’s my code:

    KeyStore keystore = getKeyStore();//Implemented somewhere else and working ok
    String password = "changeme";

    SSLContext context = SSLContext.getInstance("SSL");
    KeyManagerFactory kmfactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
    kmfactory.init(keystore, password.toCharArray());
    context.init(kmfactory.getKeyManagers(), new TrustManager[] { new X509TrustManager() {
        @Override
        public void checkClientTrusted(java.security.cert.X509Certificate[] arg0, String arg1)
                throws CertificateException {
        }
        @Override
        public void checkServerTrusted(java.security.cert.X509Certificate[] arg0, String arg1)
                throws CertificateException {
        }

        @Override
        public java.security.cert.X509Certificate[] getAcceptedIssuers() {
            return null;
        }
    } }, new SecureRandom());
    SSLSocketFactory sf = new SSLSocketFactory(context);

    Scheme httpsScheme = new Scheme("https", 443, sf);
    SchemeRegistry schemeRegistry = new SchemeRegistry();
    schemeRegistry.register(httpsScheme);
    ClientConnectionManager cm = new SingleClientConnManager(schemeRegistry);
    HttpClient client = new DefaultHttpClient(cm);

    HttpGet get = new HttpGet("https://theurl.com");
    HttpResponse response = client.execute(get);
    System.out.println(EntityUtils.toString(response.getEntity(), "UTF-8"));

The last statement prints out a 403 error. What am I missing here?

  • 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-06-17T14:23:32+00:00Added an answer on June 17, 2026 at 2:23 pm

    Figured it out own my own. The 403 error was getting was because Java SSL was not selecting my client certificate.

    I debugged the SSL handshake and found that the server asked for a client certificate issued by a list of authorities and the issuer of my client certificate wasn’t on that list. So Java SSL simply couldn’t find an appropriate certificate on my keystore. It looks like web browsers and Java implement SSL a little differently since my browser actually asks me which certificate to use, no matter what the server certificate asks for in terms of issuers of client certificates.

    In this case, the server certificate is to blame. It is self-signed and the list of issuers it informs as acceptable is incomplete. And that doesn’t mix well with the Java SSL implementation. But the server isn’t mine and there is nothing I can do about it, except for complaining about the brazilian government (their server). Without further due, here’s my work around:

    First, I used a TrustManager that trusts anything (like I did in my question):

    public class MyTrustManager implements X509TrustManager {
        public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
        }
        public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
        }
        public X509Certificate[] getAcceptedIssuers() {
            return null;
        }
    }
    

    Then I implemented a key manager that always uses the key I want from a PKCS12 (.pfx) certificate:

    public class MyKeyManager extends X509ExtendedKeyManager {
    
    KeyStore keystore = null;
    String password = null;
    public MyKeyManager(KeyStore keystore, String password) {
            this.keystore = keystore;
            this.password = password;
    }
    
    @Override
    public String chooseClientAlias(String[] arg0, Principal[] arg1, Socket arg2) {
        return "";//can't be null
    }
    
    @Override
    public String chooseServerAlias(String arg0, Principal[] arg1, Socket arg2) {
        return null;
    }
    
    @Override
    public X509Certificate[] getCertificateChain(String arg0) {
        try {
            X509Certificate[] result = new X509Certificate[keystore.getCertificateChain(keystore.aliases().nextElement()).length];
            for (int i=0; i < result.length; i++){
                result[i] = (X509Certificate) keystore.getCertificateChain(keystore.aliases().nextElement())[i];
            }
            return result ;
        } catch (Exception e) {
        }
        return null;
    }
    
    @Override
    public String[] getClientAliases(String arg0, Principal[] arg1) {
        try {
            return new String[] { keystore.aliases().nextElement() };
        } catch (Exception e) {
            return null;
        }
    }
    
    @Override
    public PrivateKey getPrivateKey(String arg0) {
        try {
            return ((KeyStore.PrivateKeyEntry) keystore.getEntry(keystore.aliases().nextElement(),
                    new KeyStore.PasswordProtection(password.toCharArray()))).getPrivateKey();
        } catch (Exception e) {
        }
        return null;
    }
    
    @Override
    public String[] getServerAliases(String arg0, Principal[] arg1) {
        return null;
    }
    
    }
    

    This would work if my pfx also contained its issuer certificate. But it doesn’t (yay!). So when I used the key manager as above, I got a SSL handshake error (peer not authenticated). The server only authenticates the client if the client sends a certificate chain that the server trusts. Since my certificate (issued by a Brazilian agency) doesn’t contain its issuer, its certificate chain contains only itself. The server doesn’t like that and denies to authenticate the client. The work around is to create manually the certificate chain:

    ...
    @Override
        //The order matters, your certificate should be the first one in the chain, its issuer the second, its issuer's issuer the third and so on.
    public X509Certificate[] getCertificateChain(String arg0) {
                X509Certificate[] result = new X509Certificate[2];
                //The certificate chain contains only one entry in my case
                result[0] = (X509Certificate) keystore.getCertificateChain(keystore.aliases().nextElement())[0];
                //Implement getMyCertificateIssuer() according to your needs. In my case, I read it from a JKS keystore from my database
                result[1] = getMyCertificateIssuer();
                return result;
    }
    ...
    

    After that, it was just a matter of putting my custom key and trust managers to good use:

                InputStream keystoreContents = null;//Read it from a file, a byte array or whatever floats your boat
                KeyStore keystore = KeyStore.getInstance("PKCS12");
                keystore.load(keystoreContetns, "changeme".toCharArray());
                SSLContext context = SSLContext.getInstance("TLSv1");
                context.init(new KeyManager[] { new MyKeyManager(keystore, "changeme") },
                                new TrustManager[] { new MyTrustManager() }, new SecureRandom());
                SSLSocketFactory sf = new SSLSocketFactory(context);
                Scheme httpsScheme = new Scheme("https", 443, sf);
                SchemeRegistry schemeRegistry = new SchemeRegistry();
                schemeRegistry.register(httpsScheme);
                ClientConnectionManager cm = new SingleClientConnManager(schemeRegistry);
                HttpClient client = new DefaultHttpClient(cm);
                HttpPost post = new HttpPost("https://www.someserver.com");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to find ID3V2 tags from MP3 file using jid3lib in Java.
I have thousands of HTML files to process using Groovy/Java and I need to
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I have a small JavaScript validation script that validates inputs based on Regex. I
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I have been unable to fix a problem with Java Unicode and encoding. The
I'm trying to create an if statement in PHP that prevents a single post

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.