springboot設定Https請求
阿新 • • 發佈:2020-07-04
1.首先去阿里雲購買個證書,也有免費的,但是免費的只能使用一年,證書需要繫結域名
2.將證書放進專案
3.配置YML
server:
ssl:
key-store: 55555.pfx
key-store-password: 55555
keyStoreType: PKCS12
connectionTimeout: 20000
port: 8888
重點來了,配置請求轉發
@Configuration
public class WebMvcconfig implements WebMvcConfigurer {
@Bean
public TomcatServletWebServerFactory servletContainer() {TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory() {
@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;
}
@Bean
public Connector httpConnector() {
Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
connector.setScheme("http");
// Connector監聽的http的埠號
connector.setPort(8080);
connector.setSecure(false);
// 監聽到http的埠號後轉向到的https的埠號
connector.setRedirectPort(8888);
return connector;
}
}
如果請求報錯:java.lang.UnsatisfiedLinkError: org.apache.tomcat.jni.SSL.renegotiatePending(J)I問題
在pom.xml中加入
<properties> <tomcat.version>9.0.12</tomcat.version> </properties>
<dependency> <groupId>org.apache.tomcat</groupId> <artifactId>tomcat-juli</artifactId> <version>${tomcat.version}</version> </dependency>
然後執行,請求成功!