转载

Spring boot 2:http请求重定向到https方法的变更

周周娱··3,830 次阅读

Spring boot2.0 版本已经推出一月有余,这次版本升级的幅度很大,很多在1.5版本的的配置属性或类被重命名或者删除,导致以前惯用的写法不再适用,其中就包括http请求重定向到https请求方法的变更。 如果在搜索引擎上搜索springboot 支持http/https,大致能搜到的方法是这样的 ```java // 在某配置类中添加如下内容 // 监听的http请求的端口,需要在application配置中添加http.port=端口号 如80 @Value("${http.port}") Integer httpPort; //正常启用的https端口 如443 @Value("${server.port}") Integer httpsPort; @Bean public EmbeddedServletContainerFactory servletContainer() { 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.addAdditionalTomcatCo...

转载来源:https://my.oschina.net/u/3491123/blog/1790458。本文仅供学习交流,如有侵权请联系站点作者。

Spring boot2.0 版本已经推出一月有余,这次版本升级的幅度很大,很多在1.5版本的的配置属性或类被重命名或者删除,导致以前惯用的写法不再适用,其中就包括http请求重定向到https请求方法的变更。

如果在搜索引擎上搜索springboot 支持http/https,大致能搜到的方法是这样的

    // 在某配置类中添加如下内容     // 监听的http请求的端口,需要在application配置中添加http.port=端口号  如80     @Value("${http.port}")     Integer httpPort;      //正常启用的https端口 如443     @Value("${server.port}")     Integer httpsPort;     @Bean 	public EmbeddedServletContainerFactory servletContainer() { 		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"); 		//Connector监听的http的端口号 		connector.setPort(httpPort); 		connector.setSecure(false); 		//监听到http的端口号后转向到的https的端口号 		connector.setRedirectPort(httpsPort); 		return connector; 	} 

但是在Spring boot2.0中EmbeddedServletContainerFactory 已经被重命名为TomcatServletWebServerFactory,导致上面这段代码不可用,所以在2.0版本我们只需把EmbeddedServletContainerFactory 重命名为TomcatServletWebServerFactory就可以了。修改后的代码大致如下

    // 在某配置类中添加如下内容     // 监听的http请求的端口,需要在application配置中添加http.port=端口号  如80     @Value("${http.port}")     Integer httpPort;      //正常启用的https端口 如443     @Value("${server.port}")     Integer httpsPort;      // springboot2 写法     @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(httpsPort);         return connector;     } 
感谢阅读,愿有所启发。