[英]How to redirect from http to https in Spring Boot?
我的網站正在使用https協議。 當我執行諸如提交表單之類的操作時,它會重定向到http,而不是https。 我用這種方式。
@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;
}
但是在構建“地址已在使用中:綁定”時顯示錯誤。 您能幫我解決這個問題嗎?
您必須終止該特定端口的進程
在Windows中
netstat -ano
將列出所有監聽的協議,端口和進程。 采用
taskkill -pid "proces to kill" /f
殺死監聽端口的進程。 例如
taskkill -pid 431 /f
在Linux中
如果您知道進程正在運行哪個端口,則可以鍵入: lsof -i:<port>
。
例如, lsof -i:8080
,以列出在端口8080上運行的進程(pid)。
然后使用kill <pid>
進程。
如果在執行上述步驟后仍然無法工作,請驗證您的步驟
要在Spring Boot中啟用HTTPS,請遵循以下三個步驟。
步驟1.獲取SSL證書:生成自簽名證書或從證書頒發機構獲取證書
第2步:在Spring Boot中啟用HTTPS。
在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
第3步:將HTTP重定向到HTTPS(已經完成)
有關更多詳細信息,請參閱在Spring Boot中啟用HTTPS。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.