[英]Java: how to add SSL client-side authentication
I have this code to connect the server with a client using SSL, and now I want to add client-side authentication: 我有这个代码使用SSL连接服务器和客户端,现在我想添加客户端身份验证:
(I have a server keystore (JCEKS type) and a client keystore (JKS type), the server uses a truststore (cacerts) where I imported both certificates because I also want to use this truststore for client authentication) (我有一个服务器密钥库(JCEKS类型)和一个客户端密钥库(JKS类型),服务器使用信任库(cacerts),我导入了两个证书,因为我也想使用此信任库进行客户端身份验证)
Client code: 客户代码:
System.setProperty("javax.net.ssl.trustStore", cerServer);
System.setProperty("javax.net.ssl.trustStoreType","JCEKS");
System.setProperty("javax.net.ssl.trustStorePassword", pwdCacerts);
SSLSocketFactory sslsocketfactory = (SSLSocketFactory) SSLSocketFactory.getDefault();
SSLSocket sslsocket = (SSLSocket) sslsocketfactory.createSocket("localhost", port);
Server Code: 服务器代码:
KeyStore ks = LoadKeyStore(new File(serverKeyStore), pwdKeyStore, "JCEKS");
KeyManagerFactory kmf;
kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
kmf.init(ks, pwdKeyStore.toCharArray());
SSLContext sc = SSLContext.getInstance("SSL");
sc.init(kmf.getKeyManagers(),null, null);
SSLServerSocketFactory ssf = sc.getServerSocketFactory();
sslserversocket = (SSLServerSocket) ssf.createServerSocket(port);
thanks in advance for any help. 提前感谢您的帮助。
edit: I add this code in the server side: 编辑:我在服务器端添加此代码:
System.setProperty("javax.net.ssl.trustStore", cacerts);
System.setProperty("javax.net.ssl.trustStoreType","JKS");
System.setProperty("javax.net.ssl.trustStorePassword", pwdCacerts);
but if I delete the client certificate in cacerts, the connection doesn't give me error and for that I think it's wrong that way 但如果我在cacerts中删除客户端证书,连接不会给我错误,因此我认为这是错误的
If you want your system to use client-certificate authentication, you'll need 如果您希望系统使用客户端证书身份验证,则需要
the server to request (or require) a client certificate. 服务器请求(或要求)客户端证书。 This is done by setting setWantClientAuth(true)
on the server socket (or setNeedClientAuth
, respectively). 这是通过在服务器套接字(或分别为setNeedClientAuth
setWantClientAuth(true)
上设置setWantClientAuth(true)
来完成的。 You'll also need the server to advertise the CA it accepts, which is normally done by using a truststore on the server that contains the CA by which the client-certificate chain was issued (this seems to be what you've done by setting javax.net.ssl.trustStore*
on the server). 您还需要服务器通告它接受的CA,这通常是通过在服务器上使用信任库来完成的,该信任库包含颁发客户端证书链的CA(这似乎是您通过设置完成的操作)服务器上的javax.net.ssl.trustStore*
。
the client to be configured with a keystore containing the client certificate (possible the chain if there are intermediate CAs) and its private key. 要配置包含客户端证书的密钥库的客户端(如果存在中间CA,则可能是链)及其私钥。 This can be done by setting the javax.net.ssl.keyStore*
(which may affect other connections) or by using a KeyManagerFactory
in the same way as you've done it on the server side. 这可以通过设置javax.net.ssl.keyStore*
(可能影响其他连接)或使用KeyManagerFactory
以与在服务器端完成的方式相同的方式来完成。
If you use setWantClientAuth(true)
, you might still not get an error, since the server will accept connections that don't have a client-certificate (the server would then check the SSLSession
's peer certificates to see whether there was a cert or not). 如果您使用setWantClientAuth(true)
,您可能仍然不会收到错误,因为服务器将接受没有客户端证书的连接(然后服务器将检查SSLSession
的对等证书以查看是否存在证书或不)。 setNeedClientAuth(true)
would break the connection when the client doesn't present a certificate. 当客户端不提供证书时, setNeedClientAuth(true)
将断开连接。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.