小程序開發後臺時,必需要以https才能訪問,賊難受!配置一下便可解決。web
第一步咱們在springboot的application中配置以下代碼spring
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.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer;
import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;
import org.springframework.boot.context.embedded.EmbeddedServletContainerFactory;
import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory;
import org.springframework.context.annotation.Bean;apache
@SpringBootApplication
public class SellApplication implements EmbeddedServletContainerCustomizer {小程序
public static void main(String[] args) {
SpringApplication.run(SellApplication.class, args);
}tomcat
//攔截全部請求
@Bean
public EmbeddedServletContainerFactory servletContainer() {
TomcatEmbeddedServletContainerFactory tomcat = new TomcatEmbeddedServletContainerFactory() {
@Override
protected 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;
}springboot
//配置http轉https
@Bean
public Connector httpConnector() {
Connector connector = new Connector(TomcatEmbeddedServletContainerFactory.DEFAULT_PROTOCOL);
connector.setScheme("http");
//Connector監聽的http的端口號
connector.setPort(80);
connector.setSecure(false);
//監聽到http的端口號後轉向到的https的端口號
connector.setRedirectPort(443);
return connector;
}app
//這裏設置默認端口爲443,即https的
@Override
public void customize(ConfigurableEmbeddedServletContainer container) {
container.setPort(443);
}
}
第二步咱們在springboot的application.properties中配置以下代碼ide
server.port=443
server.ssl.key-store=classpath:1624137_www.aimeimao.com.cn.pfx
server.ssl.key-store-password=8tCUBJsH
server.ssl.keyStoreType=PKCS12post
註釋:spa
1.server.port 端口號
2.server.ssl.key-store 證書文件
3.server.ssl.key-store-password 證書密碼
4.server.ssl.keyStoreType 證書類型
配置以上步驟便可完成行雲流水的操做!