照著網上的步驟
1,先生成證書
2,證書放到resources下
3,配置ssl
4,增加config類
做完后, 一啟動, 就報錯
org.apache.catalina.LifecycleException: Protocol handler start failed
org.springframework.boot.web.embedded.tomcat.ConnectorStartFailedException: Connector configured to listen on port 8443 failed to start
解決報錯, 注釋掉ssl配置, 再啟動 ,就成功啟動, 但是沒有https的作用了
下面貼出詳細操作
生成證書
keytool -genkey -alias your.alias -keypass yourpass1 -keyalg RSA -keysize 2048 -validity 3650 -keystore E:/httpsKeys/keystore.keystore -storepass 121233
yml配置, port 不管是8080,8081,8443, 還是18080 ..... 隨便什么都會報錯, 只有注釋ssl配置才能啟動
server:
port: 8443
tomcat:
uri-encoding: UTF-8
servlet:
context-path: /
ssl:
key-store: classpath:keystore.keystore # 證書名字
#key-store: classpath:keystore.keystore
key-store-password: 121233 # 密鑰庫密碼
#key-password: yourpass1 #springboot2.0不需要配置這個 T-T
key-store-type: PKCS12
key-alias: your.alias
配置類
package com.kft.automobile.config.https;
import org.apache.catalina.Context;
import org.apache.catalina.connector.Connector;
import org.apache.tomcat.util.descriptor.web.SecurityCollection;
import org.apache.tomcat.util.descriptor.web.SecurityConstraint;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class HttpsConnector {
@Bean
public Connector connector() {
Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
connector.setScheme("http");
connector.setPort(8081);//不管8080還是8081都一樣
connector.setSecure(false);
connector.setRedirectPort(8443);//
return connector;
}
@Bean
public TomcatServletWebServerFactory tomcatServletWebServerFactory() {
TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory() {
@Override
protected void postProcessContext(Context context) {
SecurityConstraint securityConstraint = new SecurityConstraint();
securityConstraint.setUserConstraint("CONFIDENTIAL");
SecurityCollection collection = new SecurityCollection();
collection.addPattern("/*");
securityConstraint.addCollection(collection);
context.addConstraint(securityConstraint);
}
};
tomcat.addAdditionalTomcatConnectors(connector());
return tomcat;
}
}
求救, 實在搞不懂什么原因, 網上說的埠被占用, 都不管用,
因為我一注釋掉ssl配置就能啟動, 一打開, 不管用哪個埠都沒問題
uj5u.com熱心網友回復:
你制作證書的時候沒有指定PKCS12,默認應當是JKS。你把PKCS12改成JKS就行了。或者生成證書的時候指定 -storetype PKCS12轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/194396.html
標籤:Java EE
