通信端无需向对方证明自己的身份,则称该端处于“客户模式”,否则称其处于“服务器模式”,无论是客户端还是服务器端,都可处于“客户模式”或者“服务器模式”
首先生成服务器端认证证书,使用java自带的keytool工具:
其中:
-genkey:生成一对非对称密钥
-keyalg:加密算法
-keystore:证书存放路径
-alias:密钥对别名,该别名是公开的
相同的方式,生成客户端认证证书,不过命名为client_rsa.key,别名为clientkey
使用jdk1.5,唯一需要引入的包为log4j-1.2.14.jar
客户端认证:
package com.test.client.auth;
import java.io.FileInputStream;
import java.security.KeyStore;
import java.util.Properties;
import javax.net.ssl.KeyManager;
import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.TrustManagerFactory;
import com.test.server.config.Configuration;
public class Auth {
private static SSLContext sslContext;
public static SSLContext getSSLContext() throws Exception{
Properties p = Configuration.getConfig();
String protocol = p.getProperty("protocol");
String sCertificateFile = p.getProperty("serverCertificateFile");
String sCertificatePwd = p.getProperty("serverCertificatePwd");
String sMainPwd = p.getProperty("serverMainPwd");
String cCertificateFile = p.getProperty("clientCertificateFile");
String cCertificatePwd = p.getProperty("clientCertificatePwd");
String cMainPwd = p.getProperty("clientMainPwd");
//KeyStore class is used to save certificate.
char[] c_pwd = sCertificatePwd.toCharArray();
KeyStore keyStore = KeyStore.getInstance("JKS");
keyStore.load(new FileInputStream(sCertificateFile), c_pwd);
//TrustManagerFactory class is used to create TrustManager class.
TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance("SunX509");
char[] m_pwd = sMainPwd.toCharArray();
trustManagerFactory.init(keyStore);
//TrustManager class is used to decide weather to trust the certificate
//or not.
TrustManager[] tms = trustManagerFactory.getTrustManagers();
KeyManager[] kms = null;
if(Configuration.getConfig().getProperty("authority").equals("2")){
//KeyStore class is used to save certificate.
c_pwd = cCertificatePwd.toCharArray();
keyStore = KeyStore.getInstance("JKS");
keyStore.load(new FileInputStream(cCertificateFile), c_pwd);
//KeyManagerFactory class is used to create KeyManager class.
KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance("SunX509");
m_pwd = cMainPwd.toCharArray();
keyManagerFactory.init(keyStore, m_pwd);
//KeyManager class is used to choose a certificate
//to prove the identity of the client side.
kms = keyManagerFactory.getKeyManagers();
}
//SSLContext class is used to set all the properties about secure communication.
//Such as protocol type and so on.
sslContext = SSLContext.getInstance(protocol);
sslContext.init(kms, tms, null);
return sslContext;
}
}
客户端主程序:
package com.test.client;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.util.Properties;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory;
import org.apache.log4j.Logger;
import com.test.client.auth.Auth;
import com.t