解决方案
方法一:在启动类也就是@SpringBootApplication注解类中加上使用
/*** http重定向到https* @return*/@Beanpublic TomcatServletWebServerFactory servletContainer() {TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory() {@Overrideprotected 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;}@Beanpublic Connector httpConnector() {Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");connector.setScheme("http");//Connector监听的http的默认端口号connector.setPort(8080);connector.setSecure(false);//监听到http的端口号后转向到的https的端口号,也就是项目配置的portconnector.setRedirectPort(8089);return connector;}
方法二:新建一个配置类,加上@Configuration注解声明
@Configurationpublic class TomcatConfig {@BeanTomcatEmbeddedServletContainerFactory tomcatEmbeddedServletContainerFactory() {TomcatEmbeddedServletContainerFactory factory = new TomcatEmbeddedServletContainerFactory(){@Overrideprotected void postProcessContext(Context context) {SecurityConstraint constraint = new SecurityConstraint();constraint.setUserConstraint("CONFIDENTIAL");SecurityCollection collection = new SecurityCollection();collection.addPattern("/*");constraint.addCollection(collection);context.addConstraint(constraint);}};factory.addAdditionalTomcatConnectors(createTomcatConnector());return factory;}private Connector createTomcatConnector() {Connector connector = newConnector("org.apache.coyote.http11.Http11NioProtocol");connector.setScheme("http");connector.setPort(5001);connector.setSecure(false);connector.setRedirectPort(443);return connector;}}
参考文章
/baidu_37302589/article/details/100692957