當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
Spring Boot——HTTP访问重定向到HTTPS解决方案
生活随笔
收集整理的這篇文章主要介紹了
Spring Boot——HTTP访问重定向到HTTPS解决方案
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
解決方案?
方法一:在啟動類也就是@SpringBootApplication注解類中加上使用?
/*** http重定向到https* @return*/ @Bean public 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; }@Bean public Connector httpConnector() {Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");connector.setScheme("http");//Connector監(jiān)聽的http的默認端口號connector.setPort(8080);connector.setSecure(false);//監(jiān)聽到http的端口號后轉(zhuǎn)向到的https的端口號,也就是項目配置的portconnector.setRedirectPort(8089);return connector; }方法二:新建一個配置類,加上@Configuration注解聲明
@Configuration public 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;} }參考文章
https://blog.csdn.net/baidu_37302589/article/details/100692957
總結(jié)
以上是生活随笔為你收集整理的Spring Boot——HTTP访问重定向到HTTPS解决方案的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Spring Boot——内置Tomca
- 下一篇: Spring Security——log