获取证书
- 打开阿里云,在这里免费获取证书就可以了
- 下载对应的证书,这里我下载是tomcat,其实更建议使用nginx做认证(等我以后研究).
接下来我们来设置springboot项目
- 首先修改配置
- 配置tomcat
@Bean
public TomcatServletWebServerFactory servletContainer() { //springboot2 新变化
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(initiateHttpConnector());
return tomcat;
}
private Connector initiateHttpConnector() {
Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
connector.setScheme("http");
connector.setPort(port);//http端口
connector.setSecure(true);//这个坑,网上普遍为false重定向容易出错,我这里设置为true
connector.setRedirectPort(httpsPort);
return connector;
}
评论区