javaweb項目對https的配置01


1.准備證書生成

   a.進入到jdk下的bin目錄(如果配置了Java的環境,可以直接在cmd命令窗口中直接輸入如下命令)

 keytool -v -genkey -alias tomcat -keyalg RSA -keystore d:/tomcat.keystore  -validity 36500

  附:d:/tomcat.keystore是將生成的tomcat.keystore放到d盤根目錄下。
  "-validity 36500”含義是證書有效期,36500表示100年,默認值是90天

   b.輸入keystore密碼

   密碼任意,此處以123456為例,要記住這個密碼,之后在進行server.xml配置時需要使用。

   c.輸入名字、組織單位、組織、市、省、國家等信息

  注意事項:
  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的密碼一致,設置其它密碼也可以

  d.輸入之后會出現確認的提示

   此時輸入y,並回車。此時創建完成keystore。

進入到D盤根目錄下可以看到已經生成的tomcat.keystore
復制tomcat.keystore 到tomcat/conf/ 下

 

2.在web.xml文件中配置如下,將請求配置成https請求

<servlet>
  <servlet-name>spring</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
  <servlet-name>spring</servlet-name>
  <url-pattern>/</url-pattern>
</servlet-mapping>
<!-- 配置https請求 -->
<security-constraint> 
  <web-resource-collection > 
  <web-resource-name >SSL</web-resource-name> 
  <url-pattern>/*</url-pattern> 
</web-resource-collection>

<user-data-constraint> 
  <!-- NONE不支持https請求, CONFIDENTIAL支持https請求 -->
  <transport-guarantee>CONFIDENTIAL</transport-guarantee> 
  </user-data-constraint> 
</security-constraint>

2.打開項目的server.xml文件

3.找到文件在自帶的注釋掉的配置(這是tomcat本身帶的配置https的示例)

<!--
<Connector port="8443" protocol="org.apache.coyote.http11.Http11NioProtocol"
maxThreads="150" SSLEnabled="true" scheme="https" secure="true"
clientAuth="false" sslProtocol="TLS" />
-->

在下面添加如下配置

<Connector port="8082" protocol="HTTP/1.1" SSLEnabled="true"
maxThreads="150" scheme="https" secure="true" clientAuth="false" 
keystoreFile="D:/ezchargerfile/tomcat v8/apache-tomcat-8.0.0/conf/tomcat.keystore"
keystorePass="123456" sslProtocol="TLS"/>

保留你原來通過eclipse可視界面配置的http請求的端口並修改redirectPort="8443"為8082(8443是tomcat默認的收視率端口,你自己配置的https端口):

<Connector connectionTimeout="20000" port="8081" protocol="HTTP/1.1" redirectPort="8082"/>

去掉 redirectPort="8082"(默認的是 redirectPort="8443"),不去掉的話回出現訪問http://localhost:8081/VoucherWebService/voucher重定向到https://localhost:8082/VoucherWebService/voucher

或者直接訪問https://localhost:8082/VoucherWebService/voucher都可以。

4.配置完成后,可以看如下

 

 

參考:https://blog.csdn.net/FYWT98/article/details/82863044

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM