简体   繁体   English

在Spring Boot中如何从http重定向到https?

[英]How to redirect from http to https in Spring Boot?

my website is using https protocol. 我的网站正在使用https协议。 When I do a action like submit a form, it redirect to http, not to https. 当我执行诸如提交表单之类的操作时,它会重定向到http,而不是https。 I used this way. 我用这种方式。

@Bean
public EmbeddedServletContainerFactory servletContainer() {
   TomcatEmbeddedServletContainerFactory tomcat = new TomcatEmbeddedServletContainerFactory() {
      @Override
      protected void postProcessContext(Context context) {
         SecurityConstraint securityConstraint = new SecurityConstraint();
         securityConstraint.setUserConstraint("CONFIDENTIAL");
         SecurityCollection collection = new SecurityCollection();
         collection.addPattern("/*");
         securityConstraint.addCollection(collection);
         context.addConstraint(securityConstraint);
      }
   };
   tomcat.addAdditionalTomcatConnectors(createHttpConnector());
   return tomcat;
}


private Connector createHttpConnector() {
   Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
   connector.setScheme("http");
   connector.setPort(8080);
   connector.setSecure(false);
   connector.setRedirectPort(8443);
   return connector;
}

But It show error when building "Address already in use: bind". 但是在构建“地址已在使用中:绑定”时显示错误。 Could you hep me resolve this problem? 您能帮我解决这个问题吗?

you have to kill process for that particular port 您必须终止该特定端口的进程

In windows 在Windows中

netstat -ano

will list all the protocols, ports and processes listening . 将列出所有监听的协议,端口和进程。 Use 采用

taskkill -pid "proces to kill" /f

to kill the process listening to the port. 杀死监听端口的进程。 eg 例如

 taskkill -pid 431 /f

In Linux 在Linux中

If you know what port the process is running you can type: lsof -i:<port> . 如果您知道进程正在运行哪个端口,则可以键入: lsof -i:<port>

For instance, lsof -i:8080 , to list the process (pid) running on port 8080. 例如, lsof -i:8080 ,以列出在端口8080上运行的进程(pid)。

Then kill the process with kill <pid> . 然后使用kill <pid>进程。

If after running above step it does not work than verify your step 如果在执行上述步骤后仍然无法工作,请验证您的步骤

For Enabling HTTPS in Spring Boot follow these three step. 要在Spring Boot中启用HTTPS,请遵循以下三个步骤。

Step 1.Get yourself a SSL certificate: generate a self-signed certifcate or get one from a Certificate Authority 步骤1.获取SSL证书:生成自签名证书或从证书颁发机构获取证书

Step 2.Enable HTTPS in Spring Boot. 第2步:在Spring Boot中启用HTTPS。

  • add below configuration in application.properties file at location src/main/resources 在application.properties文件中的src / main / resources位置添加以下配置

     server.port: 8443 server.ssl.key-store: <keystore> (egkeystore.p12) server.ssl.key-store-password: <key-store-password> (egmypassword) server.ssl.keyStoreType: <keyStoreType>(egPKCS12) server.ssl.keyAlias: tomcat 

Step 3.Redirect HTTP to HTTPS (Which you already completed) 第3步:将HTTP重定向到HTTPS(已经完成)

For more details refer Enable HTTPS in Spring Boot 有关更多详细信息,请参阅在Spring Boot中启用HTTPS。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM