基于springboot设置Https请求过程解析
1.首先去阿里云购买个证书,也有免费的,但是免费的只能使用一年,证书需要绑定域名
2.将证书放进项目
3.配置YML
server: ssl: key-store:55555.pfx key-store-password:55555 keyStoreType:PKCS12 connectionTimeout:20000 port:8888
重点来了,配置请求转发
@Configuration
publicclassWebMvcconfigimplementsWebMvcConfigurer{
@Bean
publicTomcatServletWebServerFactoryservletContainer(){
TomcatServletWebServerFactorytomcat=newTomcatServletWebServerFactory(){
@Override
protectedvoidpostProcessContext(Contextcontext){
SecurityConstraintconstraint=newSecurityConstraint();
constraint.setUserConstraint("CONFIDENTIAL");
SecurityCollectioncollection=newSecurityCollection();
collection.addPattern("/*");
constraint.addCollection(collection);
context.addConstraint(constraint);
}
};
tomcat.addAdditionalTomcatConnectors(httpConnector());
returntomcat;
}
@Bean
publicConnectorhttpConnector(){
Connectorconnector=newConnector("org.apache.coyote.http11.Http11NioProtocol");
connector.setScheme("http");
//Connector监听的http的端口号
connector.setPort(8080);
connector.setSecure(false);
//监听到http的端口号后转向到的https的端口号
connector.setRedirectPort(8888);
returnconnector;
}
}
如果请求报错:java.lang.UnsatisfiedLinkError:org.apache.tomcat.jni.SSL.renegotiatePending(J)I问题
在pom.xml中加入
9.0.12
org.apache.tomcat tomcat-juli ${tomcat.version}
然后运行,请求成功!
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。