- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我需要创建一个 javax.net.ssl.SSLSocketFactory
来建立 TLS 连接但忽略服务器证书的验证。不适用于 HTTP 协议(protocol)。我知道这不是正确的做法,但我需要它。我不想要也不喜欢。
我做了一个可行的实现,但验证服务器证书(因为它应该是)实现如下所示:
/**
* Provide a quick method to construct a SSLSocketFactory which is a TCP socket using TLS/SSL
* @param trustStore location of the trust store
* @param keyStore location of the key store
* @param trustStorePassword password to access the trust store
* @param keyStorePassword password to access the key store
* @return the SSLSocketFactory to create secure sockets with the provided certificates infrastructure
* @exception java.lang.Exception in case of something wrong happens
* */
static public SSLSocketFactory getSocketFactory ( final String trustStore, final String keyStore, final String trustStorePassword, final String keyStorePassword) throws Exception
{
// todo check if the CA needs or can use the password
final FileInputStream trustStoreStream = new FileInputStream(trustStore);
final FileInputStream keyStoreStream = new FileInputStream(keyStore);
// CA certificate is used to authenticate server
final KeyStore caKs = KeyStore.getInstance("JKS");
caKs.load(trustStoreStream, trustStorePassword.toCharArray());
final TrustManagerFactory tmf = TrustManagerFactory.getInstance("PKIX");
tmf.init(caKs);
trustStoreStream.close();
// client key and certificates are sent to server so it can authenticate us
final KeyStore ks = KeyStore.getInstance("JKS");
ks.load(keyStoreStream, keyStorePassword.toCharArray());
final KeyManagerFactory kmf = KeyManagerFactory.getInstance("PKIX");
kmf.init(ks, keyStorePassword.toCharArray());
keyStoreStream.close();
// finally, create SSL socket factory
final SSLContext context = SSLContext.getInstance("TLSv1.2");
context.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);
return context.getSocketFactory();
}
最佳答案
解决方案是创建一个虚拟 X509TrustManager
类,它基本上接受所有服务器证书而不进行检查。
然后创建一个实例并将其作为 SSLContext::init
调用中的第二个参数传递。
但说真的,这是个坏主意。如果您不打算检查 CERT,则使用 SSL 毫无意义或更糟1。
1 - 这是我的理论。您的安全人员告诉您,通过不安全的 channel 使用 MTTQ 是危险的。或者他们已经阻止了端口 1883。但是你有一个 MTTQ/SSL 服务器,你不会为获得适当的 CERT 而烦恼。所以你只是想把一些“有效”的东西放在一起。你猜怎么了。如果告诉您使用 SSL 的人发现了,他们会 have your guts for garters!如果您不是出于这个原因而这样做,那么……好吧……您所做的事情毫无意义。即使是自签名服务器 CERT 也比这更好。您只需将其添加为客户端 keystore 中的受信任证书。
关于java - SSLSocketFactory 忽略服务器证书,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49047805/
在我的项目中,我使用的是 java 8,并且在 mu pom.xml 以及我的 eclipse 中配置了相同的配置。但是每当我尝试通过 mvn install 编译我的代码时,它就会抛出以下错误。我也
我需要创建一个 javax.net.ssl.SSLSocketFactory 来建立 TLS 连接但忽略服务器证书的验证。不适用于 HTTP 协议(protocol)。我知道这不是正确的做法,但我需要
我有一个协议(protocol),其中有一个升级到 SSL/TLS 的普通普通套接字。我需要能够控制处理 SSL 握手的超时以及其他参数,例如允许的协议(protocol)版本。 (例如关闭 SSLv
我正在尝试创建自己的 javax.net.ssl.SSLSocketFactory 实现,为了捕获所有由第三方库发送的 HTTP/SSL 请求,并记录它们。这是在该库中调用工厂的方式(它是 commo
我有一个 SSLSocketFactory 和一个 TrustManagerFactory,如下所示: TrustManagerFactory tmf = TrustManagerFactory.ge
我正在构建一个客户端应用程序,它将通过 soap 消息连接到第三方服务器。我正在使用在 JBoss 4.2.3 上运行的 jax-ws 2.1.9 和 jdk 1.6_18。 问题本身是配置为仅用于测
我试图绕过对我的连接的 SSL 检查 - SSLSocketFactory sslsocketfactory = (SSLSocketFactory) SSLSocketFactory.getDefa
我有一个实用程序 SSLSocketFactory 类,它基本上允许您切换是否要信任所有证书和/或由于 JDK 错误而关闭 Diffie-Hellman。我最近添加了提供您自己的 SSLSocketF
根据 http://hc.apache.org/httpcomponents-client-ga/httpclient/examples/org/apache/http/examples/client
java中的SSLSocketFactory类在使用HttpsURLConnection时起到什么作用? java文档没有太大帮助。 是否有任何方法可以将 keystore 和信任库与 sslsock
在我们的产品中,我们使用 SSLSocketFactory 创建到服务器的 SSLSocket。在创建到服务器的套接字时,我们根据产品配置使用两种方法之一: (1) 我们创建一个标准套接字,然后将其包
Spring-Ldap 1.3.1 为了使用 TLS 测试 spring-ldap,我创建了一个接受所有证书的 CustomSSLSocketFactory 类(我知道这个的安全问题)。 运行测试结果
在我们的应用程序中,当应用程序与服务器通信时,我会额外检查证书(公钥固定)(针对 MITMA)。为了与服务器通信,我使用 HttpClient。另外我在生产中有一些代理服务器,所以我需要使用 SNI。
我当前的 TCP 客户端正在使用 org.apache.http.conn.ssl.SSLSocketFactory 构建套接字我正在迁移到 import javax.net.ssl.SSLSocke
我有一台带有自签名证书的服务器。我已经在我的计算机上使用 keytool 导入它并使用 -Djavax.net.ssl.trustStore=blabla 编译参数。当我尝试运行以下代码时: SSLS
我的代码在这里: SSLContext sslContext = SSLContext.getInstance("TLS"); sslContext.init(null, getAllCerts(),
在 WebSphere 中部署 Spring Boot 应用程序。由于它们的 SSL 配置,我们需要明确指定 SSLSocketFactory 以确保应用程序使用 WebSphere 证书而不是默认的
我正在使用 OkHttp,我需要忽略 SSL 错误以进行应用程序调试。这曾经在 Java 8 中有效。 final TrustManager[] trustAllCerts = new TrustMa
SSLSocketFactory 提供 getDefaultCipherSuites(默认情况下在套接字上启用的密码)和 getSupportedCipherSuites(如果需要,可以启用的密码)。
我创建了一个自定义 SSLSocketFactory 类并将其设置如下 ldapEnv.put(Context.SECURITY_PROTOCOL, "ssl"); ldapEnv.put(FACTO
我是一名优秀的程序员,十分优秀!