java 调用tomcat api,Tomcat采用双向认证https协议通过JavaAPI调用(一)配置SSL

本文详细介绍了HTTPS双向认证的过程,包括客户端和服务端的证书生成、导入及Tomcat的配置。首先,通过keytool生成服务器和客户端的密钥库及证书,然后将客户端证书导入服务器信任库,最后在Tomcat的server.xml中配置SSL,启用双向认证。完成这些步骤后,用户可以通过HTTPS访问网站,确保通信安全。
摘要由CSDN通过智能技术生成

双向认证(个人理解):

客户端认证:

客户端通过浏览器访问某一网站时,如果该网站为HTTPS网站,浏览器会自动检测系统中是否存在该网站的信任证书,如果没有信任证书,浏览器一般会拒绝访问,IE会有一个继续访问的链接,但地址栏是红色,给予用户警示作用,即客户端验证服务端并不是强制性的,可以没有服务端的信任证书,当然是否继续访问完全取决于用户自己。如何去除地址栏的红色警告呢?后续会介绍导入服务端证书到浏览器的方法。

服务端认证:

服务端需要获取到客户端通过浏览器发送过来的认证证书,该证书在服务端的证书库中已存在,仅仅是个匹配过程,匹配成功即通过认证,可继续访问网站资源,反之则无法显示网页,后续有截图。

基本逻辑:

1、生成服务端密钥库并导出证书;

2、生成客户端密钥库并导出证书;

3、根据服务端密钥库生成客户端信任的证书;

4、将客户端证书导入服务端密钥库;

5、将服务端证书导入浏览器。

构建演示系统

演示环境:

JDK:1.8

Tomcat:apache-tomcat-8.0.15

开发工具:MyEclipse 10

一、生成密钥库和证书

可参考以下密钥生成脚本,根据实际情况做必要的修改,其中需要注意的是:服务端的密钥库参数“CN”必须与服务端的IP地址相同,否则会报错,客户端的任意。

1、生成服务器证书库

keytool -validity 365 -genkey -v -alias server -keyalg RSA -keystore F:/keys/server.keystore -dname "CN=127.0.0.1,OU=icesoft,O=icesoft,L=Haidian,ST=Beijing,c=cn" -storepass 123456 -keypass 123456

2、生成客户端证书库

keytool -validity 365 -genkeypair -v -alias client -keyalg RSA -storetype PKCS12 -keystore F:/keys/client.p12 -dname "CN=client,OU=icesoft,O=icesoft,L=Haidian,ST=Beijing,c=cn" -storepass 123456 -keypass 123456

3、从客户端证书库中导出客户端证书

keytool -export -v -alias client -keystore F:/keys/client.p12 -storetype PKCS12 -storepass 123456 -rfc -file F:/keys/client.cer

4、从服务器证书库中导出服务器证书

keytool -export -v -alias server -keystore F:/keys/server.keystore -storepass 123456 -rfc -file F:/keys/server.cer

5、生成客户端信任证书库(由服务端证书生成的证书库)

keytool -import -v -alias server -file F:/keys/server.cer -keystore F:/keys/client.truststore -storepass 123456

6、将客户端证书导入到服务器证书库(使得服务器信任客户端证书)

keytool -import -v -alias client -file F:/keys/client.cer -keystore F:/keys/server.keystore -storepass 123456

7、查看证书库中的全部证书

keytool -list -keystore F:/keys/server.keystore -storepass 123456

二、Tomat配置

使用文本编辑器编辑${catalina.base}/conf/server.xml

找到Connector port="8443"的标签,取消注释,并修改成如下:

maxThreads="150" scheme="https" secure="true"

28160144_LxvQ.gif clientAuth="true" sslProtocol="TLS"

28160144_LxvQ.gif keystoreFile="${catalina.base}/key/server.keystore" keystorePass="123456"

28160144_LxvQ.gif truststoreFile="${catalina.base}/key/server.keystore" truststorePass="123456"/>

备注:

keystoreFile:指定服务器密钥库,可以配置成绝对路径,如“D:/key/server.keystore”,本例中是在Tomcat目录中创建了一个名称为key的文件夹,仅供参考。

keystorePass:密钥库生成时的密码

truststoreFile:受信任密钥库,和密钥库相同即可

truststorePass:受信任密钥库密码

使用浏览器访问:

将step.2(client.p12)双击,导入到个人证书。

ee3f042c16870344ffb09b4ef069fb8e.png

32949ef94a359fb17c4a156b003b4881.png

710f176233f8c89c887b799e4688ab63.png

d02dc15984cf05397713dc1a017decde.png

完成之后就可以通过 https://127.0.0.1:8443访问了

配置web.xml强制使用https访问

SSL

/*

CONFIDENTIAL

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
================================================================================ Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. ================================================================================ Apache Tomcat Version 8.0.15 Release Notes ========= CONTENTS: ========= * Dependency Changes * API Stability * Bundled APIs * Web application reloading and static fields in shared libraries * Security manager URLs * Symlinking static resources * Viewing the Tomcat Change Log * Cryptographic software notice * When all else fails =================== Dependency Changes: =================== Tomcat 8.0 is designed to run on Java SE 7 and later. ============== API Stability: ============== The public interfaces for the following classes are fixed and will not be changed at all during the remaining lifetime of the 8.x series: - All classes in the javax namespace The public interfaces for the following classes may be added to in order to resolve bugs and/or add new features. No existing interface method will be removed or changed although it may be deprecated. - org.apache.catalina.* (excluding sub-packages) Note: As Tomcat 8 matures, the above list will be added to. The list is not c
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值