1. 程式人生 > >springboot專案配置阿里雲ssl證書,http轉https

springboot專案配置阿里雲ssl證書,http轉https

環境:springboot專案 版本:1.5.8.realse
證書型別: .pfx
伺服器:阿里雲centos主機
  • 申請阿里雲ssl證書,免費和貴的都有(或者自己用工具生成的證書用法一致)
  • 開放伺服器443埠
  • 下載證書,將證書放到resource目錄下
  • application.properties配置
#證書配置
server.ssl.key-store=classpath:xxxxx.pfx //配置證書路徑
server.ssl.key-store-password=xxxx //證書密碼
server.ssl.keyStoreType=
PKCS12 //keyStoreType就填這個 server.port=443
  • 專案的入口類Application.java注入兩個bean
	@Bean
	public EmbeddedServletContainerFactory servletContainer() {
	//springboot版本不同可能下面的類名會不同或者類的包路徑會不同
		TomcatEmbeddedServletContainerFactory tomcat = new TomcatEmbeddedServletContainerFactory() {
			@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"); //監聽http的埠號 connector.setPort(80); connector.setSecure(false); //監聽到http的埠號後轉向到的https的埠號 System.out.println("監聽到了80埠"); connector.setRedirectPort(443);//這裡的埠寫成和配置檔案一樣的埠就Ok return connector; }
按上述步驟操作後就可以直接訪問域名(不用加埠號)就可以轉換成https了

例如: baidu.com➡️https://baidu.com
在這裡插入圖片描述

在這裡插入圖片描述