spring boot 系列學習記錄:http://www.cnblogs.com/jinxiaohang/p/8111057.htmlhtml
碼雲源碼地址:https://gitee.com/jinxiaohang/springbootjava
HTTPS (全稱:Hyper Text Transfer Protocol over SecureSocket Layer),是以安全爲目標的 HTTP 通道,在HTTP的基礎上經過傳輸加密和身份認證保證了傳輸過程的安全性 。HTTPS 在HTTP 的基礎下加入SSL 層,HTTPS 的安全基礎是 SSL,所以加密的詳細內容就須要 SSL。 HTTPS 存在不一樣於 HTTP 的默認端口及一個加密/身份驗證層(在 HTTP與 TCP 之間)。這個系統提供了身份驗證與加密通信方法。如今它被普遍用於萬維網上安全敏感的通信,例如交易支付等方面 。-----------來自百度百科git
經過本地jdk自帶的證書生成工具keytool。進入到項目路徑下,輸入如下命令(須要系統的環境變量配置JDK):spring
keytool -genkey -alias tomcat -storetype PKCS12 -keyalg RSA -keystore keystore.p12
步驟以下:apache
一、將生成的keystore.p12複製到resource文件下。tomcat
二、而後添加配置安全
server.port=8080 server.ssl.enabled=true server.ssl.key-store=classpath:keystore.p12 server.ssl.key-store-password=123456 server.ssl.key-store-type=PKCS12 server.ssl.key-alias=tomcat
一、若是是如今啓動項目,訪問 http://127.0.0.1:8080 會出現如下問題:springboot
二、固然能夠直接訪問 https://127.0.0.1:8080 ,或者增長一個配置類,添加如下代碼,並需改配置文件 app
@Bean public TomcatServletWebServerFactory servletContainer() { TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory() { @Override protected void postProcessContext(Context context) { SecurityConstraint constraint = new SecurityConstraint(); constraint.setUserConstraint("CONFIDENTIAL"); SecurityCollection collection = new SecurityCollection(); collection.addPattern("/*"); constraint.addCollection(collection); context.addConstraint(constraint); } }; tomcat.addAdditionalTomcatConnectors(httpConnector()); return tomcat; } @Bean public Connector httpConnector() { Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol"); connector.setScheme("http"); // Connector監聽的http的端口號 connector.setPort(8080); connector.setSecure(false); // 監聽到http的端口號後轉向到的https的端口號 connector.setRedirectPort(8443); return connector; }
server.port=8443
能夠實現HTTP下的8080端口向HTTPS下的8443端口轉發。
三、再次啓動,並訪問 http://127.0.0.1:8080,它會實現自動跳轉。ide
若是拋出如下異常:
java.lang.UnsatisfiedLinkError: org.apache.tomcat.jni.SSL.renegotiatePending(J)I at org.apache.tomcat.jni.SSL.renegotiatePending(Native Method) ~[tomcat-embed-core-9.0.13.jar!/:9.0.13] at org.apache.tomcat.util.net.openssl.OpenSSLEngine.getHandshakeStatus(OpenSSLEngine.java:1021) ~[tomcat-embed-core-9.0.13.jar!/:9.0.13] at org.apache.tomcat.util.net.openssl.OpenSSLEngine.wrap(OpenSSLEngine.java:457) ~[tomcat-embed-core-9.0.13.jar!/:9.0.13] at javax.net.ssl.SSLEngine.wrap(SSLEngine.java:469) ~[na:1.8.0_192] at org.apache.tomcat.util.net.SecureNioChannel.handshakeWrap(SecureNioChannel.java:440) ~[tomcat-embed-core-9.0.13.jar!/:9.0.13] at org.apache.tomcat.util.net.SecureNioChannel.handshake(SecureNioChannel.java:211) ~[tomcat-embed-core-9.0.13.jar!/:9.0.13] at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1394) ~[tomcat-embed-core-9.0.13.jar!/:9.0.13] at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) [tomcat-embed-core-9.0.13.jar!/:9.0.13] at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) [na:1.8.0_192] at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) [na:1.8.0_192] at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) [tomcat-embed-core-9.0.13.jar!/:9.0.13] at java.lang.Thread.run(Thread.java:748) [na:1.8.0_192]
嘗試更換springboot和tomcat的版本,參考:https://zhuanlan.zhihu.com/p/54204871
我使用的springboot:2.1.1.RELEASE,tomcat:9.0.12,能夠解決上述問題。
觀察啓動時輸入的日誌,他是有提示這樣的信息:
2019-11-21 15:25:52.894 INFO 22216 --- [ main] o.a.catalina.core.AprLifecycleListener : An older version [1.2.17] of the APR based Apache Tomcat Native library is installed, while Tomcat recommends a minimum version of [1.2.23]
意思是Apache Tomcat Native library的版本太低,須要升級本地的Apache Tomcat Native library版本。