Java 客户端使用 Openssl 和客户端身份验证连接到服务器

发布于 2024-07-10 07:14:13 字数 153 浏览 7 评论 0 原文

我必须编写一个 Java 客户端来连接到 SSL 服务器。 服务器使用 openssl 证书,并配置为进行客户端身份验证。

我似乎无法在网上找到任何有用的资源来帮助我(我对 openssl 和 SSL 都一无所知)来了解谁来实施我的客户端。

帮助!

I have to write a Java Client to connect to an SSL server. The server uses openssl certificate, and is configured to do Client Auth.

I can't seem to locate any useful resources online that can help me (who doesn't know anything about openssl and much about SSL) to understand who to go about implementing my Client Side.

Help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

对你的占有欲 2024-07-17 07:14:13

这里的不同之处在于您正在使用客户端身份验证,因此您需要私钥和证书来识别自己的身份。 您可以在初始化 SSLContext 时通过指定 KeyManager 将其提供给 JSSE。

可定制的设置

以下是基本步骤。 Java 6 中的 JSSE API 得到了显着改进,但我会坚持使用 Java 5,以防您陷入该版本的困境。

KeyStore tks = KeyStore.getInstance(KeyStore.getDefaultType());
tks.load(...); /* Load the trust key store with root CAs. */
TrustManagerFactory tmf = 
  TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
tmf.init(tks);
KeyStore iks = KeyStore.getInstance(KeyStore.getDefaultType());
iks.load(...); /* Load the identity key store with your key/cert. */
KeyManagerFactory kmf = 
  KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
kmf.init(iks, password);
SSLContext ctx = SSLContext.getInstance("TLS");
ctx.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);
SocketFactory factory = ctx.getSocketFactory();
Socket socket = factory.createSocket(host, port);

系统配置

使用 SunJSSE 提供程序时,可以使用替代的“零配置”方案。 我相信许多其他提供商(例如 IBM)也遵循相同的模式并且也能发挥作用。 该机制使用 系统属性, 并由 JSSE 参考指南。

对于客户端身份验证,重要属性javax.net.ssl.keyStorejavax.net.ssl.keyStorePassword。 这些值应分别是用户密钥存储的路径和该密钥存储的“密钥条目”的密码。

使用这些属性时,您可以创建一个支持客户端身份验证的新 SSLSocket,如下所示:

SocketFactory factory = SSLSocketFactory.getDefault();
Socket socket = factory.createSocket(host, port);

由于您使用的是“默认”SSLSocketFactory,这取决于系统范围的属性,因此在 JVM 中创建的所有套接字将使用相同的证书进行身份验证。 如果您需要更多控制,则必须使用上面的“可自定义设置”。

The twist here is that you are using client authentication, so you need a private key and a certificate to identify yourself. You provide this to JSSE by specifying KeyManagers when you initialize an SSLContext.

Customizable Setup

Following are the basic steps. The JSSE API was significantly improved in Java 6, but I'll stick with Java 5, in case you're stuck on that version.

KeyStore tks = KeyStore.getInstance(KeyStore.getDefaultType());
tks.load(...); /* Load the trust key store with root CAs. */
TrustManagerFactory tmf = 
  TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
tmf.init(tks);
KeyStore iks = KeyStore.getInstance(KeyStore.getDefaultType());
iks.load(...); /* Load the identity key store with your key/cert. */
KeyManagerFactory kmf = 
  KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
kmf.init(iks, password);
SSLContext ctx = SSLContext.getInstance("TLS");
ctx.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);
SocketFactory factory = ctx.getSocketFactory();
Socket socket = factory.createSocket(host, port);

System Configuration

An alternative "zero-config" scenario can be used when using the SunJSSE provider. I believe many other providers (like IBM) have followed the same pattern and will work as well. The mechanism uses system properties, and is described in detail by the JSSE Reference Guide.

For client authentication, the important properties are javax.net.ssl.keyStore and javax.net.ssl.keyStorePassword. The values should be the path to the user's key store and the password for that key store's "key entries", respectively.

When using these properties, you can create a new SSLSocket that supports client authentication like this:

SocketFactory factory = SSLSocketFactory.getDefault();
Socket socket = factory.createSocket(host, port);

Since you are using the "default" SSLSocketFactory, which depends on the system-wide properties, all sockets created in the JVM will authenticate with the same certificate. If you need more control than that, you have to use the "Customizable Setup" above.

妄司 2024-07-17 07:14:13

Java 在标准 API 中包含 SSL 支持。 查看 1.5.0 javadoc 中的这些类:

SSLSocket(如果您自己执行通信逻辑)。

HttpsURLConnection 如果服务器侧面讲 HTTP

Java includes SSL support in the standard API. Have a look at these classes in the 1.5.0 javadoc:

SSLSocket if you're doing the comms logic yourself.

HttpsURLConnection if the server side speaks HTTP

枕梦 2024-07-17 07:14:13

你可以使用httpclient。 请查看此 SSL 指南

You could use httpclient. Have a look at this SSL guide.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文