您的位置:首页 > 理论基础 > 计算机网络

Tomcat配置https与http自动跳转和tomcat单双向SSL配置及IE访问HTTPS下载文件失败

2014-09-27 15:47 911 查看

Tomcat配置https与http自动跳转

https介绍:

HTTPS(全称:Hypertext Transfer Protocol over Secure Socket Layer),是以安全为目标

的HTTP通道,简单讲是HTTP的安全版。即HTTP下加入SSL层,HTTPS的安全基础是SSL,因此

加密的详细内容就需要SSL。 它是一个URI scheme(抽象标识符体系),句法类同http:体系

。用于安全的HTTP数据传输。https:URL表明它使用了HTTPS,但HTTPS存在不同于HTTP的默

认端口及一个加密/身份验证层(在HTTP与TCP之间)。这个系统的最初研发由网景公司进行

,提供了身份验证与加密通讯方法,现在它被广泛用于万维网上安全敏感的通讯,例如交易支

付方面

与http区别:

一、https协议需要到ca申请证书,一般免费证书很少,需要交费。

二、http是超文本传输协议,信息是明文传输,https 则是具有安全性的ssl加密传输协议。

三、http和https使用的是完全不同的连接方式,用的端口也不一样,前者是80,后者是443。

四、http的连接很简单,是无状态的;HTTPS协议是由SSL+HTTP协议构建的可进行加密传输

、身份认证的网络协议,比http协议安全。

SSL协议:

  SSL安全套接层协议(Secure Socket Layer)

  为Netscape所研发,用以保障在Internet上数据传输之安全,利用数据加密(Encryption)技

术,可确保数据在网络上之传输过程中不会被截取及窃听。目前一般通用之规格为40 bit之安

全标准,美国则已推出128 bit之更高安全标准,但限制出境。只要3.0版本以上之IE.或

Netscape浏览器即可支持SSL。

  当前版本为3.0。它已被广泛地用于Web浏览器与服务器之间的身份认证和加密数据传输

。SSL协议位于TCP/IP协议与各种应用层协议之间,是一种国际标准的加密及身份认证通信协

议,为TCP提供一个可靠的端到端的安全服务,为两个通讯个体之间提供保密性和完整性(身份

鉴别)。SSL协议可分为两层:SSL记录协议(SSL Record Protocol):它建立在可靠的传输协

议(如TCP)之上,为高层协议提供数据封装、压缩、加密等基本功能的支持。SSL握手协议

(SSL Handshake Protocol):它建立在SSL记录协议之上,用于在实际的数据传输开始前,

通讯双方进行身份认证、协商加密算法、交换加密密钥等。

如何配置:

1、生成服务器端证书文件

可以使用Windows系统或者Linux系统

(1) Windows环境

条件:已经安装JDK

步骤:

l 进入%JAVA_HOME%/bin目录

l 执行命令

keytool -genkey -alias tomcat -keyalg RSA -keystore F:\tomcat.keystore -validity 36500

参数简要说明:“F:\tomcat.keystore”含义是将证书文件保存在F盘,证书文件名称是

tomcat.keystore ;“-validity 36500”含义是证书有效期,36500表示100年,默认值是90天

l 在命令行填写必要的参数:

A、输入keystore密码:此处需要输入大于6个字符的字符串

B、“您的名字与姓氏是什么?”这是必填项,并且必须是TOMCAT部署主机的域名或者IP[如:

gbcom.com 或者 10.1.25.251],就是你将来要在浏览器中输入的访问地址

C、 “你的组织单位名称是什么?”、“您的组织名称是什么?”、“您所在城市或区域名称是什么

?”、“您所在的州或者省份名称是什么?”、“该单位的两字母国家代码是什么?”可以按照需要

填写也可以不填写直接回车,在系统询问“正确吗?”时,对照输入信息,如果符合要求则使用

键盘输入字母“y”,否则输入“n”重新填写上面的信息

D、输入<tomcat>的主密码,这项较为重要,会在tomcat配置文件中使用,建议输入与

keystore的密码一致,设置其它密码也可以

l 完成上述输入后,直接回车则在你在第二步中定义的位置找到生成的文件

(2) Linux环境

条件:安装了JDK

步骤:

l 进入$JAVA_HOME/bin目录

l 执行命令

./keytool -genkey -alias tomcat -keyalg RSA -keystore /usr/local/ac/web/tomcat.keystore -

validity 36500

参数简要说明:“/etc/tomcat.keystore”含义是将证书文件保存在路径/usr/local/ac/web/下,证

书文件名称是tomcat.keystore ;“-validity 36500”含义是证书有效期,36500表示100年,默认

值是90天

说明:

A、Enter keystore password:此处需要输入大于6个字符的字符串

B、“What is your first and last name?”这是必填项,并且必须是TOMCAT部署主机的域名或者

IP[如:gbcom.com 或者 10.1.25.251],就是你将来要在浏览器中输入的访问地址

C、“What is the name of your organizational unit?”、“What is the name of your

organization?”、“What is the name of your City or Locality?”、“What is the name of your

State or Province?”、“What is the two-letter country code for this unit?”可以按照需要填写也

可以不填写直接回车,在系统询问“correct?”时,对照输入信息,如果符合要求则使用键盘输

入字母“y”,否则输入“n”重新填写上面的信息

D、Enter key password for <tomcat>,这项较为重要,会在tomcat配置文件中使用,建议输

入与keystore的密码一致,设置其它密码也可以

l 完成上述输入后,直接回车则在你在第二步中定义的位置找到生成的文件

2、配置TOMCAT服务器

(1) 如果你是在Windows环境中生成证书文件,则需要将生成的证书tomcat.keystore拷贝

到Tomcat将要引用的位置,假设tomcat的应用证书的路径是“/etc/tomcat.keystore”,则需要

将证书文件拷贝到“etc/”下;如果是在Linux环境按照上述介绍的步骤生成证书文件的话,此时

证书文件已经在“etc/”下。

(2) 配置Tomcat,打开$CATALINA_HOME/conf/server.xml,修改如下,

<Connector port="8080" protocol="HTTP/1.1"

connectionTimeout="20000"

redirectPort="8443" />

修改参数=>

<Connector port="80" protocol="HTTP/1.1"

connectionTimeout="20000"

redirectPort="443" />

<!--

<Connector port="8443" protocol="HTTP/1.1" SSLEnabled="true"

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

clientAuth="false" sslProtocol="TLS"/>

-->

去掉注释且修改参数=>

<Connector port="443" protocol="HTTP/1.1" SSLEnabled="true"

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

clientAuth="false" sslProtocol="TLS" keystoreFile="/etc/tomcat.keystore"

keystorePass="www.gbcom.com.cn"/>

注释:标识为淡蓝色的两个参数,分别是证书文件的位置和<tomcat>的主密码,在证书文件

生成过程中做了设置

<!--

<Connector port="8009" enableLookups="false" protocol="AJP/1.3" redirectPort="8443"

/>

-->

修改参数=>

<Connector port="8009" enableLookups="false" protocol="AJP/1.3" redirectPort="443" />

(3) 打开$CATALINA_HOME/conf/web.xml,在该文件末尾增加:

2.强制https访问

在tomcat\conf\web.xml中的</welcome-file-list>后面加上这样一段:

<login-config>

<!-- Authorization setting for SSL -->

<auth-method>CLIENT-CERT</auth-method>

<realm-name>Client Cert Users-only Area</realm-name>

</login-config>

<security-constraint>

<!-- Authorization setting for SSL -->

<web-resource-collection >

<web-resource-name >SSL</web-resource-name>

<url-pattern>/*</url-pattern>

</web-resource-collection>

<user-data-constraint>

<transport-guarantee>CONFIDENTIAL</transport-guarantee>

</user-data-constraint>

</security-constraint>

3、上述配置完成后,重启TOMCAT后即可以使用SSL。IE地址栏中可以直接输入地址不必输入

“http://” 或者 “https://” ;也可以输入 “http:// ” 会跳转成为 “https://” 来登录

4、注意事项:

(1) 生成证书的时间,如果IE客户端所在机器的时间早于证书生效时间,或者晚于有效时

间,IE会提示“该安全证书已到期或还未生效”

(2) 如果IE提示“安全证书上的名称无效或者与站点名称不匹配”,则是由生成证书时填写

的服务器所在主机的域名“您的名字与姓氏是什么?”/“What is your first and last name?”不正

确引起的

5、遗留问题:

(1)如果AC主机不能通过域名查找,必须使用IP,但是这个IP只有在配置后才能确定,这样

证书就必须在AC确定IP地址后才能生成

(2)证书文件只能绑定一个IP地址,假设有10.1.25.250 和 192.168.1.250 两个IP地址,在证

书生成文件时,如使用了10.1.25.250,通过IE就只能使用10.1.25.250 来访问AC-WEB,

192.168.1.250是无法访问AC-WEB的。



https跳转到http session丢失问题

解决之前首先我们要明白,服务端跟踪客户端的会话是通过 浏览器每次提交的一个cookie表示

这个cookie的key是JSESSIONID,value是session的id,浏览器请求时候就会带上这个,服务端

就可以成功跟踪客户端了。

用户直接访问https的登录页面,在登录action成功(调用业务层服务验证用户和密码成功后)

后增加如下代码

Java代码

HttpServletRequest request = ServletActionContext.getRequest();

request.getSession(false).invalidate();

request.getSession(true);

HttpServletResponse response = ServletActionContext.getResponse();

Cookie cookie = newCookie("JSESSIONID",request.getSession().getId());

response.addCookie(cookie);

即保证服务端与发送到客户端Cookie中的JSESSIONID值一致就行了。



tomcat单向SSL配置

1、通过keytools生成keystore

keytool -genkey -alias tomcat -keyalg RSA -keypass changeit -storepass changeit -keystore

d:\server.keystore

注意CN必须域名

比如以后通过https://localhost:8443/path/ 访问网站

这时候CN = localhost

2、tomcat 打开SSL配置

server.xml配置

<Connector port="8443" protocol="HTTP/1.1" SSLEnabled="true"

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

clientAuth="false" sslProtocol="TLS"

keystoreFile="d:/server.eystore" keystorePass="changeit"/>

<Connector port="8443" protocol="HTTP/1.1" SSLEnabled="true"

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

clientAuth="false" sslProtocol="TLS"

keystoreFile="d:/server.eystore" keystorePass="changeit"/>

3、 直接访问

浏览器输入https://localhost:8443/path/ 这个时候由于证书不是第三方颁发的所以会提示认证

安全证书有问题。

4、导出x509证书

keytool -export -alias tomcat -file d:\server.cer -keystore d:\server.keystore.

先导出一个x509证书

5、新建client信任的keystore.

keytool -genkey -alias trust -keyalg RSA -keypass changeit -storepass changeit -keystore

d:\trust.keystore

6、添加服务器端证书进入本地信任keystore

keytool -import -v -alias tomcat -file d:\server.cer -keystore d:\trust.keystore

7、java 测试代码

import java.io.IOException;

import java.net.MalformedURLException;

import java.net.URL;

import javax.net.ssl.HttpsURLConnection;

public class TClient {

public static void main(String[] args) throws Exception {

System.setProperty("javax.net.ssl.trustStore", "d:/trust.keystore" );

new TClient().test();

}

private void test() {

String https_url = "https://localhost:8443/path/login.jsp";

URL url;

try {

url = new URL(https_url);

HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();

connection.setDoOutput(true);

connection.setRequestMethod("POST");

connection.getOutputStream().flush();

connection.getOutputStream().close();

System.out.println( connection.getPeerPrincipal().toString() );

} catch (MalformedURLException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

}

}

tomcat 启动ssl(双向认证)

1、通过keytools生成serverkeystore

keytool -genkey -alias tomcat -keyalg RSA -keypass changeit -storepass changeit -keystore d:\server.keystore

注意CN必须域名

比如以后通过https://localhost:8443/path/ 访问网站

这时候CN = localhost

2、导出x509证书

keytool -export -alias tomcat -file d:\server.cer -keystore d:\server.keystore.

先导出一个x509证书

3、新建client信任的trustclientkeystore.

keytool -genkey -alias trust -keyalg RSA -keypass changeit -storepass changeit -keystore d:\trust.keystore

4、添加服务器端证书进入本地信任trustclientkeystore.

keytool -import -v -alias tomcat -file d:\server.cer -keystore d:\trust.keystore

前面不变

5、通过keytools生成clientkeystore

keytool -genkey -alias client -keyalg RSA -keypass changeit -storepass changeit -keystore d:\client.keystore

6、导出x509证书

keytool -export -alias client -file d:\client.cer -keystore d:\client.keystore.

7、新建server信任的trustserverkeystore.

keytool -genkey -alias trustserver -keyalg RSA -keypass changeit -storepass changeit -keystore d:\trustserver.keystore

8、添加本地证书进入服务器信任trustserverkeystore.

keytool -import -v -alias client -file d:\client.cer -keystore d:\trustserver.keystore

到目前为止就有2个keystore 2个trustkeystore

9、tomcat 配置

Xml代码

<Connector port="8443" protocol="HTTP/1.1" SSLEnabled="true"

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

[color=red]clientAuth="true"[/color] sslProtocol="TLS"

keystoreFile="d:/server.keystore" keystorePass="changeit"

truststoreFile="d:/trustserver.keystore" truststorePass="changeit"

/>

<Connector port="8443" protocol="HTTP/1.1" SSLEnabled="true"

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

[color=red]clientAuth="true"[/color] sslProtocol="TLS"

keystoreFile="d:/server.keystore" keystorePass="changeit"

truststoreFile="d:/trustserver.keystore" truststorePass="changeit"

/>

clientAuth 设置为 true

10、javacode

Java代码

import java.io.File;

import java.io.FileInputStream;

import java.security.KeyStore;

import org.apache.http.HttpResponse;

import org.apache.http.client.HttpClient;

import org.apache.http.client.methods.HttpPost;

import org.apache.http.conn.scheme.Scheme;

import org.apache.http.conn.ssl.SSLSocketFactory;

import org.apache.http.impl.client.DefaultHttpClient;

public class Client {

/**

* @param args

* @throws Exception

*/

public static void main(String[] args) throws Exception {

HttpClient httpclient = new DefaultHttpClient();

KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());

KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());

FileInputStream keyStoreIn = new FileInputStream(new File("d:/client.keystore"));

FileInputStream trustStoreIn = new FileInputStream(new File("d:/trust.keystore"));

try {

keyStore.load(keyStoreIn, "123456".toCharArray());

trustStore.load(trustStoreIn, "123456".toCharArray());

} finally {

keyStoreIn.close();

trustStoreIn.close();

}

SSLSocketFactory socketFactory = new SSLSocketFactory(keyStore, "123456", trustStore);

httpclient.getConnectionManager().getSchemeRegistry().register(new Scheme("https",socketFactory, 8443));

HttpPost httpget = new HttpPost("https://localhost:8443/SSOClient/login.html");

System.out.println("Request:" + httpget.getRequestLine());

HttpResponse response = httpclient.execute(httpget);

System.out.println(response.getStatusLine());

httpclient.getConnectionManager().shutdown();

}

}

Tomcat6配置SSL双向验证

第一步:为服务端生成证书
一、创建证书keystore

创建证书keystore的过程。
操作步骤

使用JDK自带的命令keytool创建“keystore”文件,其中包含了密钥。在命令行窗口中输入以下命令:
keytool -genkey -alias tbb -keyalg RSA -keystore d:\tbb.keystore

根据系统提示输入“keystore”的密码,例如:“password”。

根据系统提示,输入其他数据。

输入完成后,系统将询问用户输入的信息是否正确,如果正确,请输入“y”,否则请输入“n”。
注:此项要输入本机ip
输入私钥的密码,如“password”。

输入私钥的密码。

系统将在当前路径下生成一个“keystore”文件。

二、创建自签名的证书

创建自签名的证书的过程。
操作步骤

使用JDK自带的命令keytool创建自签名证书。

keytool -selfcert -alias tbb -keystore d:\tbb.keystore
这句话可能出现错误,应该是jdk版本问题。我没有执行这句话,而直接导出证书。
输入创建keystore时输入的密码。

对keystore进行验证。

keytool -selfcert -alias tbb -keystore d:\tbb.keystore
验证成功后,将证书导出。

keytool -export -alias tbb -keystore d:\tbb.keystore -storepass password -rfc -file d:\tbb.cer
系统将根据前面的keystore文件在当前目录导出一个cer文件。
三、导入证书到 受信任的根证书颁发机构

四、配置tomcat的server.xml(tomcat7.0)
[html] view
plaincopy

1.
<Connector port="8443" protocol="HTTP/1.1" SSLEnabled="true"
2.
maxThreads="150" scheme="https" secure="true"
3.
clientAuth="false" sslProtocol="TLS" keystorePass="yourpasswd" keystoreFile="your keystore" />

配置成功截图

第二步:为客户端生成证书

下一步是为浏览器生成证书,以便让服务器来验证它。为了能将证书顺利导入至IE和Firefox,证书格式应该是PKCS12,因此,使用如下命令生成:d:\>keytool
-genkey -alias MyKey -keyalg RSA -storetype PKCS12 -keystore d:\my.p12 -dname "CN=MyKey,OU=cn,L=cn,ST=cn,C=CN" -storepass password -keypass password

对应的证书库存放在“d:\my.p12”,客户端的CN可以是任意值。稍候,我们将把这个“my.p12”证书库导入到IE和Firefox中。

第三步:让服务器信任客户端证书

由于是双向SSL认证,服务器必须要信任客户端证书,因此,必须把客户端证书添加为服务器的信任认证。由于不能直接将PKCS12格式的证书库导入,我们必须先把客户端证书导出为一个单独的CER文件,使用如下命令:d:\>keytool
-export -alias MyKey -keystore d:\my.p12 -storetype PKCS12 -storepass password -rfc -file d:\my.cer

通过以上命令,客户端证书就被我们导出到“d:\my.cer”文件了。下一步,是将该文件导入到服务器的证书库,添加为一个信任证书:d:\>keytool -import -v -file d:\my.cer -keystore d:\tomcat.keystore
-storepass password

通过list命令查看服务器的证书库,我们可以看到两个输入,一个是服务器证书,一个是受信任的客户端证书:keytool -list -keystore tbb.keystore -storepass password

第四步:配置Tomcat服务器

打开Tomcat根目录下的/conf/server.xml,找到如下配置段,修改如下:

<Connector port="8443" protocol="HTTP/1.1" SSLEnabled="true"

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

clientAuth="true" sslProtocol="TLS"

keystoreFile="d:/tomcat.keystore" keystorePass="password"

truststoreFile="d:/tomcat.keystore" truststorePass="password"

/>

其中,clientAuth指定是否需要验证客户端证书,如果该设置为“false”,则为单向SSL验证,SSL配置可到此结束。如果clientAuth设置为“true”,表示强制双向SSL验证,必须验证客户端证书。如果clientAuth设置为“want”,则表示可以验证客户端证书,但如果客户端没有有效证书,也不强制验证。

第五步:导入客户端证书

把my.p12导入到个人

打开网页会提示这个“确认证书”点击确定就可以打开网页了,这时就是双向认证了



IE访问HTTPS链接下载文件,IE提示无法下载

原因:

(1) 在IE6/7下,使用HTTPS下载/打开文件时,通过抓包发现文件已传输,但IE提示“Internet Explorer无法下载 *** (来自 ***)。Internet Explorer无法打开该Internet站点。请求的站点不可用,或找不到。请以后再试。” ("Unable to download. Internet Explorer was unable to open this site. The requested site is either unavailable or cannot
be found. Please try again later."),下载失败。

(2) 如果下载目标有一个hyperlink,当右键选择“目标另存为...”时,提示“Internet Explorer无法下载 *** (来自 ***)。无法将文件写入高速缓存”("The file could not be written to the cache"),下载失败。

原因:

参考Microsoft KB815313316431812935323308

参考:http://www.51testing.com/?uid-160865-action-viewspace-itemid-209104

解决方案:

//确保IE识别本次为下载文件

response.setHeader("Content-Transfer-Encoding","binary");

response.setHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0");

response.setHeader("Pragma", "public");

参考http://hi.baidu.com/%CD%F5%B7%EF%C4%FE/blog/item/28290d13d79ba82bdc540115.html

[转]关于IE下用HTTPS无法下载/打开文件

url: http://www.51testing.com/html/65/160865-209104.html
现象:

(1) 在IE6/7下,使用HTTPS下载/打开文件时,通过抓包发现文件已传输,但IE提示“Internet Explorer无法下载 *** (来自 ***)。Internet Explorer无法打开该Internet站点。请求的站点不可用,或找不到。请以后再试。” ("Unable to download. Internet Explorer was unable to open this site. The requested site is either unavailable or cannot
be found. Please try again later."),下载失败。

(2) 如果下载目标有一个hyperlink,当右键选择“目标另存为...”时,提示“Internet Explorer无法下载 *** (来自 ***)。无法将文件写入高速缓存”("The file could not be written to the cache"),下载失败。

原因:

参考Microsoft KB815313316431812935323308

解决方法:

(1) 参考上述MS KB,向client注册表增加下列DWORD条目BypassSSLNoCacheCheck,值设为1;

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Internet Settings\BypassSSLNoCacheCheck

【此方式经jackpk亲测,IE8下有效】

(2) 如果不方便修改client端,检查服务器端相关程序(eg. httpd),看是否在HTTP response header中自动添加了“Pragma: no-cache”等内容。(参考 MS KB316431的“更多信息”(More Information)部分)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: