SSL证书与SpringBoot的使用

前端使用ajax请求后端本机的8888端口上的接口,页面以https协议访问情况下无法正常访问接口,但是改回http就可以了。

后端使用Oracle JDK 11.0.9,SpringBoot 2.3.4,前端的SSL证书是从宝塔面板申请的。

配置文件需要配置相关SSL证书;

并且还需要加载一个bean

@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;
	}