300字范文,内容丰富有趣,生活中的好帮手!
300字范文 > Spring Boot——HTTP访问重定向到HTTPS解决方案

Spring Boot——HTTP访问重定向到HTTPS解决方案

时间:2022-08-31 09:11:44

相关推荐

Spring Boot——HTTP访问重定向到HTTPS解决方案

解决方案

方法一:在启动类也就是@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

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。