最近在阿里云购买了一个云服务器ECS,简单的部署了我的网站,使用ip地址访问一切正常,我以前在TK上申请了一个免费域名,就绑定了二级域名到阿里云,第一天正常访问,次日就不行了,访问站点直接提示域名未注册(未备案.....),没法访问,显然被阿里云拦截了,经过IP地址访问仍是正常的。html
后来GOOGLE了一下,通常是备案域名和主机。。。,还有一种方式是启用HTTPS,http是明文传输,阿里云可以看到我访问的域名,发如今域名未注册而后拦截,但HTTPS是加密的,因此能够正常访问(哈哈)。java
引用连接:web
https://tomcat.apache.org/tomcat-6.0-doc/ssl-howto.htmlapache
http://www.oschina.net/question/12_23148tomcat
http://www.cnblogs.com/f1194361820/p/4748590.html服务器
基本步骤:app
loiane:bin loiane$ keytool -genkey -alias tomcat -keyalg RSA Enter keystore password: password Re-enter new password: password What is your first and last name? [Unknown]: Loiane Groner What is the name of your organizational unit? [Unknown]: home What is the name of your organization? [Unknown]: home What is the name of your City or Locality? [Unknown]: Sao Paulo What is the name of your State or Province? [Unknown]: SP What is the two-letter country code for this unit? [Unknown]: BR Is CN=Loiane Groner, OU=home, O=home, L=Sao Paulo, ST=SP, C=BR correct? [no]: yes Enter key password for (RETURN if same as keystore password): password Re-enter new password: password
这样就在用户的主目录下建立了一个 .keystore 文件测试
打开 server.xml 找到下面被注释的这段网站
<!-- <Connector port="8443" protocol="HTTP/1.1" SSLEnabled="true" maxThreads="150" scheme="https" secure="true" clientAuth="false" sslProtocol="TLS" /> -->
取消注释,并将内容改成this
Connector SSLEnabled="true" acceptCount="100" clientAuth="false" disableUploadTimeout="true" enableLookups="false" maxThreads="25" port="8443" keystoreFile="${user.home}/.keystore" keystorePass="password" protocol="org.apache.coyote.http11.Http11NioProtocol" scheme="https" secure="true" sslProtocol="TLS" />
测试1:只启用HTTPS,不启用HTTP
在server.xml中,为8080端口的Connector加上注释,而且去掉8443端口的Connector的注释。而后启动tomcat。
分别经过http\https访问docs:
http://localhost:8080/docs、https://localhost:8443/docs
结果:只有https能够访问。
测试2:HTTP、HTTPS同时启用
在server.xml中,去掉8080、8443端口的Connector的注释而后启动tomcat。
分别用http\https访问docs:
http://localhost:8080/docs、https://localhost:8443/docs
结果:二者均可正常访问。
打开应用的 web.xml 文件,增长配置以下:
<security-constraint> <web-resource-collection> <web-resource-name>securedapp</web-resource-name> <url-pattern>/*</url-pattern> </web-resource-collection> <user-data-constraint> <transport-guarantee>CONFIDENTIAL</transport-guarantee> </user-data-constraint> </security-constraint>
将 URL 映射设为 /* ,这样你的整个应用都要求是 HTTPS 访问,而 transport-guarantee 标签设置为 CONFIDENTIAL 以便使应用支持 SSL。
若是你但愿关闭 SSL ,只须要将 CONFIDENTIAL 改成 NONE 便可
文章到这里基本上就应该结束了,但是,当我经过https访问网站时问题提示“There is a problem with this website’s security certificate.”,本身还好说无所谓,可是别人访问的时候也提示,可能很大程度上,别人可能就不会再访问了。。。。,
因此须要SSL证书服务,网上也有不少免费的(https://www.zhihu.com/question/19578422)
我选择的是https://buy.wosign.com/free/
Create a local Certificate Signing Request (CSR)
使用你以前建立的.keystore
keytool -certreq -keyalg RSA -alias tomcat -file certreq.csr -keystore <your_keystore_filename>
通常顺利的话最终你能够拿到一些*.crt文件,
接下来就是把这些文件导入到.keystore文件里。
好比我拿到的是四个压缩包
for Apache.zip
for IIS.zip
for Nginx.zip
for Other Server.zip
而后我使用的是for Other Server,里面有四个.crt
1_cross_Intermediate.crt
2_issuer_Intermediate.crt
3_user_contacts09.ml.crt
root.crt
先执行root,1_cross_Intermediate.crt,2_issuer_Intermediate.crt
keytool -import -alias <root> -keystore <your_keystore_filename> -trustcacerts -file <filename_of_the_chain_certificate>
最后执行 3_user_contacts09.ml.crt
keytool -import -alias tomcat -keystore <your_keystore_filename> -file <your_certificate_filename>
最后获得的.keystore直接替换以前的就能够了,如今再次访问就不会提示你证书问题了,哈哈。。。。。