我制作了示例 Springboot 应用程序,其中包含以下代码application.propertied
server.ssl.enabled=true
server.ssl.key-store=src/main/resources/SpringSecurity.p12
server.ssl.key-store-password=password
server.ssl.key-store-type=PKCS12
server.ssl.key-alias=SpringSecurity
以下两种方法也在 SpringBoot 的主类中
@Bean
public ServletWebServerFactory servletContainer() {
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(redirectConnector());
return tomcat;
}
private Connector redirectConnector() {
Connector connector = new Connector(TomcatServletWebServerFactory.DEFAULT_PROTOCOL);
connector.setScheme("http");
connector.setPort(8080);
connector.setSecure(false);
connector.setRedirectPort(8082);
return connector;
}
如果我像 https://localhost:8082/hello 这样在 tomcat 中运行这个应用程序,它会像自签名一样工作。如果我点击 http://localhost:8082/hello,tomcat 会按预期显示错误的请求。事实上,它也从 8080 重定向到 8082。
现在我将其导出为.war
文件。
现在我通过上传上面的 war 文件在 AWS Elastic Beanstalk 中创建一个应用程序。
如果我运行生成的 url : http://SpringSecurity-env.eba-wwpkejwp.us-east-2.elasticbeanstalk.com/hello
,它可以正常工作,因为它是 http。
我需要在 https 上工作。有人可以建议我在 AWS/war 文件上配置/添加/删除什么吗?