首页>代码>Spring Boot配置SSL证书支持HTTPS协议访问Tomcat服务>/ssl-https/src/main/java/com/memorynotfound/spring.boot.tomcat/TomcatRedirectHttpToHttpsConfig.java
package com.memorynotfound.spring.boot.tomcat;
import org.apache.catalina.Context;
import org.apache.catalina.connector.Connector;
import org.apache.tomcat.util.descriptor.web.SecurityCollection;
import org.apache.tomcat.util.descriptor.web.SecurityConstraint;
import org.springframework.boot.context.embedded.EmbeddedServletContainerFactory;
import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory;
import org.springframework.context.annotation.Configuration;
import javax.servlet.annotation.ServletSecurity.TransportGuarantee;
@Configuration
public class TomcatRedirectHttpToHttpsConfig {
public EmbeddedServletContainerFactory servletContainer(){
TomcatEmbeddedServletContainerFactory tomcat = new TomcatEmbeddedServletContainerFactory(){
@Override
protected void postProcessContext(Context context) {
SecurityConstraint securityConstraint = new SecurityConstraint();
securityConstraint.setUserConstraint(TransportGuarantee.CONFIDENTIAL.name());
SecurityCollection collection = new SecurityCollection();
collection.addPattern("/*");
securityConstraint.addCollection(collection);
context.addConstraint(securityConstraint);
}
};
tomcat.addAdditionalTomcatConnectors(redirectConnector());
return tomcat;
}
private Connector redirectConnector(){
Connector connector = new Connector(TomcatEmbeddedServletContainerFactory.DEFAULT_PROTOCOL);
connector.setScheme("http");
connector.setPort(8080);
connector.setSecure(false);
connector.setRedirectPort(8443);
return connector;
}
}
最近下载更多
ding1336610127 LV9
2021年3月16日
fu987654 LV1
2020年12月29日
最近浏览更多
微信网友_6184005683466240 LV11
2023年6月11日
whfuai LV14
2023年2月3日
280286951 LV17
2021年6月21日
bianzw LV2
2021年6月16日
15195859092 LV2
2021年5月24日
zhulei886 LV14
2021年5月19日
LinAllen
2021年5月10日
暂无贡献等级
username123456 LV1
2021年5月1日
Fahaxiki
2021年4月25日
暂无贡献等级
a992013093 LV15
2021年4月21日

