Java Web项目模拟https部署

生成公钥和私钥

首先执行以下命令生成一个公钥和私钥(这里密码设置为password)

keytool -genkeypair -alias tomcat -keyalg  RSA  -keypass password -storepass password -keystore d:/server.keystore

关于keytool命令的说明 https://docs.oracle.com/javase/8/docs/technotes/tools/windows/keytool.html

tomcat配置ssl说明 https://tomcat.apache.org/tomcat-8.5-doc/ssl-howto.html

配置tomcat

修改tomcat的server.xml配置中Connector信息

   <Connector port="8443" protocol="org.apache.coyote.http11.Http11NioProtocol"
            maxThreads="150" SSLEnabled="true" scheme="https" secure="true"
            clientAuth="false" sslProtocol="TLS" 
            keystoreFile="d:\server.keystore"  keystorePass="password" />   

在这里插入图片描述

修改tomcat的web.xml中的配置

<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>

在这里插入图片描述

关于使用https之后的小坑

后台通过request获取协议javax.servlet.ServletRequest#getProtocol时不是返回https,而是返回http

/**
 * Returns the name and version of the protocol the request uses
 * in the form <i>protocol/majorVersion.minorVersion</i>, for 
 * example, HTTP/1.1. For HTTP servlets, the value
 * returned is the same as the value of the CGI variable 
 * <code>SERVER_PROTOCOL</code>.
 *
 * @return		a <code>String</code> containing the protocol 
 *			name and version number
 */    
public String getProtocol();

这里也说明了https并不是协议,只是针对http做了包装。(这里的s不是security,而是ssl)。
此时在前台构建路径的时候,就不能靠后台了

String contextPath = request.getContextPath();
String protocol = request.getProtocol();
protocol = protocol.substring(0, protocol.indexOf("/"));
int port = request.getServerPort();
String localBaseUrl = protocol.toLowerCase() + "://" + request.getServerName() + ":" + port + contextPath;

以上的代码最后拼接的请求路径会是http开头的而不是https,要解决此问题,直接在前台处理即可(window.location.protocol返回字符串’https‘)

// 在后台中 用https请求返回时获取的protocol仍然是http
var locProtocol =  window.location.protocol;
var winLocalBaseUrl =  window.localBaseUrl;
if ('https:' === locProtocol && winLocalBaseUrl && typeof winLocalBaseUrl == 'string'){
    window.localBaseUrl = winLocalBaseUrl.replace('http:',window.location.protocol);
    console.log("replace base url from " + winLocalBaseUrl + " to " + window.localBaseUrl);
}

如果是Spring Boot项目,使用嵌入式tomcat,只需要在配置文件中按照以下配置即可

server.port = 8443
server.ssl.key-store = classpath:sample.jks
server.ssl.key-store-password = secret
server.ssl.key-password = password

参考:https://github.com/spring-projects/spring-boot/tree/1.5.x/spring-boot-samples/spring-boot-sample-tomcat-ssl

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

lang20150928

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值