1package cn.xiaojf.aibus.configure; 2 3import org.apache.catalina.Context; 4import org.apache.catalina.connector.Connector; 5import org.apache.coyote.http11.Http11NioProtocol; 6import org.apache.tomcat.util.descriptor.web.SecurityCollection; 7import org.apache.tomcat.util.descriptor.web.SecurityConstraint; 8import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory; 9import org.springframework.boot.web.servlet.server.ServletWebServerFactory; 10import org.springframework.context.annotation.Bean; 11import org.springframework.context.annotation.Configuration; 12import org.springframework.context.annotation.Profile; 13 14/** 15 * http ssl 配置 16 * @author xiaojf 2019/9/21 20:07 17 */ 18@Configuration 19@Profile("prod") 20public class HttpsConfigure { 21 22 @Bean 23 public ServletWebServerFactory servletWebServerFactory() { 24 TomcatServletWebServerFactory factory = new TomcatServletWebServerFactory() { 25 @Override 26 protected void postProcessContext(Context context) { 27 SecurityConstraint securityConstraint = new SecurityConstraint(); 28 securityConstraint.setUserConstraint("CONFIDENTIAL"); 29 SecurityCollection securityCollection = new SecurityCollection(); 30 securityCollection.addPattern("/*"); 31 securityConstraint.addCollection(securityCollection); 32 context.addConstraint(securityConstraint); 33 } 34 }; 35 factory.addAdditionalTomcatConnectors(redirectConnector()); 36 return factory; 37 } 38 39 private Connector redirectConnector() { 40 Connector connector = new Connector(Http11NioProtocol.class.getName()); 41 connector.setScheme("http"); 42 connector.setPort(8100); 43 connector.setSecure(false); 44 connector.setRedirectPort(443); 45 return connector; 46 } 47}
修改配置文件
1server: 2 ssl: 3 key-store: classpath:ssl/2833975_www.renyimao.cn.pfx 4 key-store-password: KzwpacCY 5 keyStoreType: PKCS12 6 port: 443 #启动端口
文件目录
