如果项目打war包,则可以使用tomcat的配置,在tomcat的配置文件中修改相关信息即可
<Connector port="80" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="443"
maxPostSize="10485760" />
<Connector port="443" protocol="org.apache.coyote.http11.Http11NioProtocol"
maxThreads="150" SSLEnabled="true">
<SSLHostConfig>
<Certificate certificateKeystoreFile="加密文件路径.pfx"
certificateKeystorePassword="JmjRWhlc"
certificateKeystoreType="PKCS12" />
</SSLHostConfig>
</Connector>
如果项目打jar包,那么需要我们在项目的配置文件中配置相关信息
其中/home/mojing/mojing.kuesee.com.pfx是加密文件的绝对路径,记得开放权限
chmod -R 777 /home/mojing/mojing.kuesee.com.pfx
这时springboot的ssl配置已经完成,然而,这时我们只能通过https访问,如果用户通过http访问则访问不了,那么如果我们需要用户能够通过http访问,该如何配置呢?
其实很简单,我们需要在项目的启动类中添加如下内容,当请求过来时,直接重定向的https,配置如下
@Bean
public Connector connector(){
Connector connector=new Connector("org.apache.coyote.http11.Http11NioProtocol");
connector.setScheme("http");
connector.setPort(80);
connector.setSecure(false);
connector.setRedirectPort(443);
return connector;
}
@Bean
public TomcatServletWebServerFactory tomcatServletWebServerFactory(Connector connector){
TomcatServletWebServerFactory tomcat=new TomcatServletWebServerFactory(){
@Override
protected void postProcessContext(Context context) {
SecurityConstraint securityConstraint=new SecurityConstraint();
securityConstraint.setUserConstraint("CONFIDENTIAL");
SecurityCollection collection=new SecurityCollection();
collection.addPattern("/*");
securityConstraint.addCollection(collection);
context.addConstraint(securityConstraint);
}
};
tomcat.addAdditionalTomcatConnectors(connector);
return tomcat;
}
此时,http转https已经完成。