SpringBoot2 http转https

在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

你可能感兴趣的:(SpringBoot2 http转https)