栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java Web项目模拟https部署

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

      

修改tomcat的web.xml中的配置

  
       
     CLIENT-CERT  
     Client Cert Users-only Area  
    
    
       
       
      SSL  
             
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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/926727.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号