


Exception when httpclient sends data to HTTPS to establish SSL connection
The exception information is as follows:
javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
Cause: The server's certificate is not trusted. This is generally caused.
Use the KEYTOOL tool to create a certificate, and then start it with TOMCAT. When you open the website in the browser, a prompt that the certificate is not trusted will appear. Of course, when HTTPCLIENT is used to send data to the server HTTPS, HTTPCLIENT will also detect whether the server's certificate is trusted. If it is not trusted, the above exception will be thrown.
There are two solutions. One is to make the certificate trusted by the client. The other is to use HTTPCLIENT to send data without checking whether the server certificate is trusted.
The first method is to make the certificate trusted.
Find a formal CA to issue a certificate, or issue the certificate yourself (it can only be trusted on that client). I won’t talk about finding a formal CA to issue a certificate. For how to issue a certificate yourself, see my other articles.
I found that after I completed the certificate signed by myself, when I opened the server address from the client, the above error was no longer prompted, but I still could not send data. what is the reason? Because the certificate is trusted on the client operating system, but not trusted in JAVA's KEYSTORE, you need to import the server's certificate into the KEYSTORE library
Import method:
Open the command line window and go to
keytool -import -noprompt -keystore cacerts -storepass changeit -alias yourEntry1 -file your.cer
The last one is the certificate exported by the server, and the others can be defaulted.
It should be noted that if there are many JAVA versions installed on the client computer, make sure that the JAVA version of the certificate you import is the one used by your TOMCAT. Generally, TOMCAT uses the JAVA version pointed to by the environment variable.
If it is a TOMCAT server created in ECLIPSE, you will be asked to choose the default JRE or the pointed JAVA when creating a new one. You must choose the path pointing to the JAVA you just imported. Otherwise, the certificate library you imported will have no effect.
The second method is not to check whether the server certificate is trustworthy when using HTTPCLIENT
Extend the HttpClient class to automatically accept the certificate
Because this method automatically receives all certificates, there are certain security issues. So please carefully consider the security requirements of your system before using this method. The specific steps are as follows:
• Provide a custom socket factory (test.MySecureProtocolSocketFactory). This custom class must implement the interface org.apache.commons.httpclient.protocol.SecureProtocolSocketFactory and call the custom X509TrustManager (test.MyX509TrustManager) in the class that implements the interface. These two classes can be obtained in the attachment attached to this article.
•Create an instance of org.apache.commons.httpclient.protocol.Protocol, specify the protocol name and default port number
Protocol myhttps = new Protocol("https", new MySecureProtocolSocketFactory (), 443);
•Register the https protocol object just created
Protocol.registerProtocol("https ", myhttps);
•Then open the https target address in the normal programming way, the code is as follows:
MySecureProtocolSocketFactory.java
import java.io.IOException; import java.net.InetAddress; import java.net.InetSocketAddress; import java.net.Socket; import java.net.SocketAddress; import java.net.UnknownHostException; import java.security.KeyManagementException; import java.security.NoSuchAlgorithmException; import java.security.cert.CertificateException; import java.security.cert.X509Certificate; import javax.net.SocketFactory; import javax.net.ssl.SSLContext; import javax.net.ssl.TrustManager; import javax.net.ssl.X509TrustManager; import org.apache.commons.httpclient.ConnectTimeoutException; import org.apache.commons.httpclient.params.HttpConnectionParams; import org.apache.commons.httpclient.protocol.SecureProtocolSocketFactory; public class MySecureProtocolSocketFactory implements SecureProtocolSocketFactory { private SSLContext sslcontext = null; private SSLContext createSSLContext() { SSLContext sslcontext=null; try { sslcontext = SSLContext.getInstance("SSL"); sslcontext.init(null, new TrustManager[]{new TrustAnyTrustManager()}, new java.security.SecureRandom()); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (KeyManagementException e) { e.printStackTrace(); } return sslcontext; } private SSLContext getSSLContext() { if (this.sslcontext == null) { this.sslcontext = createSSLContext(); } return this.sslcontext; } public Socket createSocket(Socket socket, String host, int port, boolean autoClose) throws IOException, UnknownHostException { return getSSLContext().getSocketFactory().createSocket( socket, host, port, autoClose ); } public Socket createSocket(String host, int port) throws IOException, UnknownHostException { return getSSLContext().getSocketFactory().createSocket( host, port ); } public Socket createSocket(String host, int port, InetAddress clientHost, int clientPort) throws IOException, UnknownHostException { return getSSLContext().getSocketFactory().createSocket(host, port, clientHost, clientPort); } public Socket createSocket(String host, int port, InetAddress localAddress, int localPort, HttpConnectionParams params) throws IOException, UnknownHostException, ConnectTimeoutException { if (params == null) { throw new IllegalArgumentException("Parameters may not be null"); } int timeout = params.getConnectionTimeout(); SocketFactory socketfactory = getSSLContext().getSocketFactory(); if (timeout == 0) { return socketfactory.createSocket(host, port, localAddress, localPort); } else { Socket socket = socketfactory.createSocket(); SocketAddress localaddr = new InetSocketAddress(localAddress, localPort); SocketAddress remoteaddr = new InetSocketAddress(host, port); socket.bind(localaddr); socket.connect(remoteaddr, timeout); return socket; } } //自定义私有类 private static class TrustAnyTrustManager implements X509TrustManager { public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException { } public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException { } public X509Certificate[] getAcceptedIssuers() { return new X509Certificate[]{}; } } }

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Kernelsecuritycheckfailure (kernel check failure) is a relatively common type of stop code. However, no matter what the reason is, the blue screen error causes many users to be very distressed. Let this site carefully introduce 17 types to users. Solution. 17 solutions to kernel_security_check_failure blue screen Method 1: Remove all external devices When any external device you are using is incompatible with your version of Windows, the Kernelsecuritycheckfailure blue screen error may occur. To do this, you need to unplug all external devices before trying to restart your computer.

NginxProxyManager Security Analysis and Protection Introduction: In Internet applications, security has always been a crucial issue. As a powerful reverse proxy and load balancing server software, Nginx plays an important role in ensuring the security of network applications. However, with the continuous development of Internet technology and the increasing number of network attacks, how to ensure the security of NginxProxyManager has become an urgent problem to be solved. This article will start from NginxProxyMana

Flask-Security: Adding user authentication and password encryption to Python web applications As the Internet continues to develop, more and more applications require user authentication and password encryption to protect the security of user data. In the Python language, there is a very popular web framework-Flask. Flask-Security is an extension library based on the Flask framework, which can help developers easily

In back-end management systems, access permission control is usually required to limit different users' ability to access interfaces. If a user lacks specific permissions, he or she cannot access certain interfaces. This article will use the waynboot-mall project as an example to introduce how common back-end management systems introduce the permission control framework SpringSecurity. The outline is as follows: waynboot-mall project address: https://github.com/wayn111/waynboot-mall 1. What is SpringSecurity? SpringSecurity is an open source project based on the Spring framework, aiming to provide powerful and flexible security for Java applications.

BubblePal, a newly launched AI-based interactive toy, appears to be something that could have inspired the writers of the 2022 sci-fi/horror flick M3GAN, if it hadn’t just been launched last week. Based on large language model (LLM) technology, the ‘

A large whale address, which previously offloaded significant amounts of SUN, has sold another $1 million worth of the token within the past two hours.

Telegram meme coins have been in the spotlight for a while due to the overly successful mini-Apps technology on the messaging platform.

It is required that systems A, B, and C use the sso service to log in to systems A, B, and C to obtain three tokens: Atoken, Btoken, and Ctoken respectively. After one of the systems actively logs out, the other two systems also log out. All Atoken, Btoken, and Ctoken invalidation record tokenpom file introduces dependence on Redis database dependence on hutool: used to parse tokenorg.springframework.bootspring-boot-starter-data-rediscn.hutoolhutool-all5.7.13 token storage class to implement AuthJdbcToken
