Https證書準備java
開發環境下,可直接用JDK自帶的keytool工具生成一個證書,正式環境可購買一個,配置過程是同樣的:web
打開cmd命令行,輸入如下命令:算法
命令解釋:spring
keytool -genkey -alias mykeystore -keypass 123456 -keyalg RSA -keysize 1024 -validity 365 -keystore D:/mykeystore.keystore -storepass 123456
根據提示輸入相關信息便可:apache
SpringMVC項目配置:跨域
一.Tomcat服務器配置tomcat
打開tomcat路徑conf文件夾下server.xml文件,本來以下內容:服務器
<Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" /> <!-- A "Connector" using the shared thread pool--> <!-- <Connector executor="tomcatThreadPool" port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" /> --> <!-- Define a SSL/TLS HTTP/1.1 Connector on port 8443 This connector uses the NIO implementation that requires the JSSE style configuration. When using the APR/native implementation, the OpenSSL style configuration is required as described in the APR/native documentation --> <!-- <Connector port="8443" protocol="org.apache.coyote.http11.Http11NioProtocol" maxThreads="150" SSLEnabled="true" scheme="https" secure="true" clientAuth="false" sslProtocol="TLS" /> -->
將8443端口配置註釋取消,並添加第一步生成的證書路徑及密碼,修改後以下所示:app
<Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443"/> <!-- A "Connector" using the shared thread pool--> <!-- <Connector executor="tomcatThreadPool" port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" /> --> <!-- Define a SSL/TLS HTTP/1.1 Connector on port 8443 This connector uses the NIO implementation that requires the JSSE style configuration. When using the APR/native implementation, the OpenSSL style configuration is required as described in the APR/native documentation --> <!-- 開啓https訪問 --> <Connector port="8443" SSLEnabled="true" clientAuth="false" keystoreFile="D:\mykeystore.keystore" keystorePass="123456" maxThreads="150" protocol="org.apache.coyote.http11.Http11NioProtocol" scheme="https" secure="true" sslProtocol="TLS"/>
二. 配置項目web.xmlcors
打開項目下web.xml,添加以下配置
<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>
至此,SpringMVC項目即完成了https的配置
SpringBoot項目配置:
一. 將第一步生成的證書放進resource文件夾
二. 配置application.yml或者application.properties文件
#修改端口號
server:
##設置https端口
port: 8444
##設置http端口,訪問此端口將被重定向到https端口
http:
port: 8080
####定義項目的訪問上下文
context-path: /mySpringBoot
##開啓Https協議
ssl:
key-store: classpath:mykeystore.keystore
key-store-password: 123456
key-store-type: jks
key-alias: mykeystore
注:此處的key-store-type應設置爲部署環境下jre裏面對應的keystore.type。打開$JAVA_HOME/jre/lib/security/java.security文件
三. 建立一個WebConfig配置類
1 package com.config; 2 3 import org.apache.catalina.Context; 4 import org.apache.catalina.connector.Connector; 5 import org.apache.tomcat.util.descriptor.web.SecurityCollection; 6 import org.apache.tomcat.util.descriptor.web.SecurityConstraint; 7 import org.springframework.beans.factory.annotation.Value; 8 import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory; 9 import org.springframework.boot.web.servlet.FilterRegistrationBean; 10 import org.springframework.context.annotation.Bean; 11 import org.springframework.context.annotation.Configuration; 12 import org.springframework.web.cors.CorsConfiguration; 13 import org.springframework.web.cors.UrlBasedCorsConfigurationSource; 14 import org.springframework.web.filter.CorsFilter; 15 16 @Configuration 17 public class WebConfig{ 18 19 @Value("${server.port}") 20 private int serverPort; 21 22 @Value("${server.http.port}") 23 private int serverHttpPort; 24 25 /** 26 * 解決跨域問題 27 * @param registry 28 */ 29 @Bean 30 public FilterRegistrationBean<CorsFilter> corsFilter() { 31 UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(); 32 CorsConfiguration config = new CorsConfiguration(); 33 config.setAllowCredentials(true); 34 // 設置你要容許的網站域名,*表示任意域名 35 config.addAllowedOrigin("*"); 36 // 表示你要容許的請求頭部信息 37 config.addAllowedHeader("*"); 38 // 設置你要容許的請求方法 39 config.addAllowedMethod("GET,POST,PUT,DELETE,HEAD,OPTIONS"); 40 source.registerCorsConfiguration("/**", config); 41 FilterRegistrationBean<CorsFilter> bean = new FilterRegistrationBean<CorsFilter>(new CorsFilter(source)); 42 // 這個順序很重要,爲避免麻煩請設置在最前 43 bean.setOrder(0); 44 return bean; 45 46 } 47 48 /** 49 * Tomcat配置Https 50 * @return 51 */ 52 @Bean 53 public TomcatServletWebServerFactory servletContainer() { 54 TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory () { 55 @Override 56 protected void postProcessContext(Context context) { 57 SecurityConstraint securityConstraint = new SecurityConstraint(); 58 securityConstraint.setUserConstraint("CONFIDENTIAL"); 59 SecurityCollection collection = new SecurityCollection(); 60 collection.addPattern("/*"); 61 securityConstraint.addCollection(collection); 62 context.addConstraint(securityConstraint); 63 } 64 }; 65 66 tomcat.addAdditionalTomcatConnectors(initiateHttpConnector()); 67 return tomcat; 68 } 69 70 /** 71 * 配置監聽端口 72 */ 73 private Connector initiateHttpConnector() { 74 Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol"); 75 connector.setScheme("http"); 76 //Connector監聽的http的端口號 77 connector.setPort(serverHttpPort); 78 connector.setSecure(false); 79 //監聽到http的端口號後轉向到的https的端口號 80 connector.setRedirectPort(serverPort); 81 return connector; 82 } 83 }
至此,SpringBoot項目即完成了https的配置