SpringBoot2 http转https

2018-03-08  本文已影响0人  乐逍遥_bbc1

在SpringBoot1.5的http转https见:

https://www.cnblogs.com/xxt19970908/p/6736370.html

升级到SpringBoot2.0发现报错。解决方案如下:

完整的代码在github

//Srping2.0的转换代码

@Bean

public ServletWebServerFactory serletContainer(){

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;

}

这样就可以解决  Springboot2 http自动跳转到https,实现了自动转向功能,利用tomcat的redirect功能,进行http到https的redirect.

原因是Springboot2取消了EmbeddedServletContainerFactory  TomcatEmbeddedServletContainerFactory

上一篇下一篇

猜你喜欢

热点阅读