使用騰訊雲服務器,運行項目時,項目開啟端口443
同時實現80端口自動轉為443端口代碼:
package com.jofiy.gaogao.config; 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.beans.factory.annotation.Value; import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class CommonConfig { @Value("${http.port}") private Integer httpPort; @Value("${server.port}") private Integer serverPort; @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(httpPort); connector.setSecure(false); //監聽到http的端口號后轉向到的https的端口號 connector.setRedirectPort(serverPort); return connector; } }
然后打包,部署項目至服務器,運行,一直報錯
直到使用安裝nginx排查一下問題時,找到一篇文章這樣說:sudo su root ,
再次運行項目:問題得到解決