赞
踩
参考博客:Https单向认证和双向认证
SSL(Secure Sockets Layer 安全套接层)就是一种协议(规范),用于保障客户端和服务器端通信的安全,以免通信时传输的信息被窃取或者修改。
客户端和服务器端在进行握手(客户端和服务器建立连接和交换参数的过程称之为握手)时会产生一个“对话密钥”(session key),用来加密接下来的数据传输,解密时也是用的这个“对话密钥”,而这个“对话密钥”只有客户端和服务器端知道。也就是说只要这个“对话密钥”不被破解,就能保证安全。
2. 客户端证书和服务器端证书
客户端证书和服务器端证书用于证明自己的身份,就好比每个人都有一张身份证,这种身份证是唯一的。一般来说,只要有服务器端的证书就可以了,但是有时需要客户端提供自己的证书,已证明其身份
Keytool 是一个java 数据证书的管理工具 ,Keytool 将密钥(key)和证书(certificates)存在一个称为keystore的文件中 在keystore里,包含两种数据:
密钥实体(Key entity)——密钥(secret key)又或者是私钥和配对公钥(采用非对称加密)
可信任的证书实体(trusted certificate entries)——只包含公钥
-genkeypair
在用户主目录中创建一个默认文件”.keystore”,还会产生一个mykey的别名,mykey中包含用户的公钥、私钥和证书(在没有指定生成位置的情况下,keystore会存在用户系统默认目录)-alias
产生别名 每个keystore都关联这一个独一无二的alias,这个alias通常不区分大小写-keystore
指定密钥库的路径(产生的各类信息将不在.keystore文件中)-keyalg
指定密钥的算法 (如 RSA,DSA,默认值为:DSA)-validity
指定创建的证书有效期多少天(默认 90)-keysize
指定密钥长度 (默认 1024-storepass
指定密钥库的密码(获取keystore信息所需的密码)-keypass
指定别名条目的密码(私钥的密码)-dname
指定证书发行者信息 其中: “CN=名字与姓氏,OU=组织单位名称,O=组织名称,L=城市或区域名 称,ST=州或省份名称,C=单位的两字母国家代码”-list
显示密钥库中的证书信息如:keytool -list -v –keystore path/to/keystore -storepass password-v
显示密钥库中的证书详细信息-exportcert
导出指定别名的证书,如:keytool - exportcert -alias theAlias -keystore path/to/keystore -file path/to/keystore/cert -storepass pass-file
参数指定导出到文件的文件名-delete
删除密钥库中某条目 keytool -delete -alias theAlias -keystore path/to/keystore –storepass pass-printcert
控制台打印证书的详细信息,如:keytool -printcert -file path/to/keystore/cert -v-keypasswd
修改密钥库中指定条目口令 keytool -keypasswd -alias theAlias -keypass oldPass -new newPass -storepass keystorePass -keystore path/to/keystore-storepasswd
修改keystore口令 keytool -storepasswd -keystore path/to/keystore -storepass oldPass -new newPass-importcert
将已签名数字证书导入密钥库 keytool -importcert -alias certAlias -keystore path/to/keystore -file path/to/keystore/cert 一般证书可以使用权威机构颁发的证书,如:veri sign,百度使用的就是veri sign颁发的证书,这样的权威证书机构是受信任的,但是这些机构颁发的证书往往是需要收费的,这样的证书也难得到。对于小型企业来说为了节约成本,常常使用自签名的证书。
接下来使用JDK keytool工具来签发证书,如果未安装JDK,请先安装JDK。本文所有的证书文件都放到F:\ca,您可以选择一个目录来存放。这边最好使用管理员运行命令窗口。(只是个人建议)
简单流程图:
keytool -genkeypair -v -alias server -keyalg RSA -validity 3650 -keystore server.keystore -storepass 123456 -keypass 123456 -dname "CN=127.0.0.1,OU=Server,O=Asia,L=Zz,ST=FJ,C=CN"
keytool -exportcert -alias server -keystore ./server.keystore -file ./server.cer -storepass 123456
keytool -importcert -alias serverca -keystore ./server_trust.keystore -file ./server.cer -storepass 123456
keytool -genkeypair -v -alias client -dname "CN=Client" -keyalg RSA -validity 3650 -keypass 123456 -keystore ./client.p12 -storepass 123456 -storetype PKCS12
keytool -exportcert -alias client -file ./client.cer -keystore ./client.p12 -storepass 123456 -storetype PKCS12
keytool -importcert -alias clientca -keystore ./server_trust.keystore -file ./client.cer -storepass 123456
keytool -list -keystore ./server_trust.keystore -storepass 123456
这时候不出意外F:\ca文件夹里有五个文件
tomcat的webapps目录下创建ca目录,并将index.jsp文件放到该目录下
<%@ page language="java" contentType="text/html;charset=utf-8" pageEncoding="UTF-8" %> <%@ page import="java.util.Enumeration" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8"/> <title>test</title> </head> <body> <p>request属性信息</p> <pre> <% for(Enumeration en = request.getAttributeNames();en.hasMoreElements();){ String name = (String) en.nextElement(); out.println(name); out.println(" = " + request.getAttribute(name)); out.println(); } %> </pre> </body> </html>
ca目录下创建WEB-INF目录,WEB-INF目录下创建web.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<display-name>ca</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
将证书文件放到tomcat根目录下
编辑conf/server.xml文件加入如下的配置:
<Connector port="8443" protocol="org.apache.coyote.http11.Http11NioProtocol" SSLEnabled="true"
maxThreads="150" scheme="https" secure="true"
clientAuth="true" sslProtocol="TLS"
keystoreFile="${catalina.base}/server.keystore" keystorePass="123456"
truststoreFile ="${catalina.base}/server_trust.keystore" truststorePass="123456"/>
说明:
启动tomcat
服务器启动后,访问出现以下页面
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ZIbKK2L3-1643042214683)(https://s2.loli.net/2022/01/25/QwMErbZSIT7x3PH.png)]
双击client.key.p12文件进行证书安装
获取 SSLSocketFactory
import javax.net.ssl.*; import java.io.FileInputStream; import java.security.KeyStore; import java.security.SecureRandom; import java.util.Optional; public class HttpConfig { public static final String PROTOCOL = "TLS"; /** * 获取keystore * * @param keystorePath keystore路径 * @param password 密码 * @return 密钥库 * @throws Exception Exception */ private static KeyStore getKeyStore(String keystorePath, String password) throws Exception { KeyStore keystore = KeyStore.getInstance("JKS"); // KeyStore keystore = KeyStore.getInstance(KeyStore.getDefaultType()); try (FileInputStream in = new FileInputStream(keystorePath);) { keystore.load(in, password.toCharArray()); return keystore; } } /** * 获取 SSLSocketFactory * @param keyManagerFactory 密钥库工厂 * @param trustFactory 信任库工厂 * @return SSLSocketFactory * @throws Exception Exception */ public static SSLSocketFactory getSSLSocketFactory(KeyManagerFactory keyManagerFactory, TrustManagerFactory trustFactory) throws Exception { // 实例化SSL上下文 SSLContext context = SSLContext.getInstance(PROTOCOL); KeyManager[] keyManagers = Optional.ofNullable(keyManagerFactory) .map(KeyManagerFactory::getKeyManagers).orElse(null); TrustManager[] trustManagers = Optional.ofNullable(trustFactory) .map(TrustManagerFactory::getTrustManagers).orElse(null); context.init(keyManagers, trustManagers, new SecureRandom()); return context.getSocketFactory(); } public static TrustManagerFactory getTrustManagersFactory(String trustStorePath, String password) throws Exception { // 实例化信任库 TrustManagerFactory trustFactory = TrustManagerFactory .getInstance(TrustManagerFactory.getDefaultAlgorithm()); KeyStore trustStore = getKeyStore(trustStorePath, password); // 初始化信任库 trustFactory.init(trustStore); return trustFactory; } public static KeyManagerFactory getKeyManagerFactory(String keystorePath, String password) throws Exception { KeyManagerFactory factory = KeyManagerFactory .getInstance(KeyManagerFactory.getDefaultAlgorithm()); // 获取密钥库 KeyStore keyStore = getKeyStore(keystorePath, password); // 初始化密钥工厂 factory.init(keyStore, password.toCharArray()); return factory; } }
测试类
import org.junit.Test; import javax.net.ssl.HttpsURLConnection; import javax.net.ssl.KeyManagerFactory; import javax.net.ssl.SSLSocketFactory; import javax.net.ssl.TrustManagerFactory; import java.io.DataInputStream; import java.io.IOException; import java.net.URL; public class HttpsRequestTest { private String password = "123456"; private String trustStorePath = "E:\\Program Files\\Tomcat\\apache-tomcat-7.0.77\\server_trust.keystore"; private String keyStorePath = "E:\\Program Files\\Tomcat\\apache-tomcat-7.0.77\\server.keystore"; // 服务器服务地址(注意:笔者这里用localhost会报一个签名不匹配问题) private String httpUrl = "https://127.0.0.1:8443/ca/"; @Test public void twoWayAuthentication() throws Exception { URL url = new URL(httpUrl); HttpsURLConnection conn = (HttpsURLConnection) url.openConnection(); // 打开输入输出流 conn.setDoInput(true); //域名校验 conn.setHostnameVerifier((k, t) -> true); // 双向认证 TrustManagerFactory trustManagersFactory = HttpConfig.getTrustManagersFactory(trustStorePath, password); KeyManagerFactory keyManagerFactory = HttpConfig .getKeyManagerFactory(keyStorePath, password); SSLSocketFactory sslSocketFactory = HttpConfig .getSSLSocketFactory(keyManagerFactory, trustManagersFactory); conn.setSSLSocketFactory(sslSocketFactory); conn.connect(); receiveData(conn); conn.disconnect(); } private void receiveData(HttpsURLConnection conn) throws IOException { int length = conn.getContentLength(); byte[] data = null; if (length != -1) { DataInputStream input = new DataInputStream(conn.getInputStream()); data = new byte[length]; input.readFully(data); input.close(); System.out.println(new String(data)); } } }
测试结果
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。