如何在Spring Boot 2中配置https?

米米尔·林德(Mimir Lind)

我已经使用生成了自签名证书keytool我已添加到资源文件夹。在我的内部application.properties,我添加了

security.require-ssl=true

# The format used for the keystore 
server.ssl.key-store-type=PKCS12
# The path to the keystore containing the certificate
server.ssl.key-store=classpath:keystore.p12
# The password used to generate the certificate
server.ssl.key-store-password=
# The alias mapped to the certificate
server.ssl.key-alias=tomcat

我的配置文件:

@Bean
public EmbeddedServletContainerFactory servletContainer() {
    EmbeddedServletContainerFactory tomcat = new EmbeddedServletContainerFactory() {
        @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(getHttpConnector());
    return tomcat;
}

但是我仍然无法使用https访问我的应用程序吗?

Slimani

不能使用EmbeddedServletContainerFactoryinSpring boot 2代替TomcatServletWebServerFactory

@Bean
public TomcatServletWebServerFactory 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(getHttpConnector());
    return tomcat;
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在Spring Boot中配置SSL?

如何在Spring Boot 2中配置Netty

如何在Spring Boot中覆盖Spring Security默认配置

如何在Spring Boot中手动配置JdbcTemplate?

如何在Spring Boot中设置重试配置获取?

如何在Spring Boot中配置拦截器?

如何在Spring Boot中配置Redis缓存?

如何在Spring Boot应用程序中配置PageableHandlerMethodArgumentResolver

如何在Spring Boot中为Camel配置Jackson ObjectMapper

如何在Spring Boot中配置CORS和基本授权?

如何在sts中更新Spring boot cli url配置?

如何在Spring Boot 2.1.9中配置SSL证书

如何在Spring Boot的application.yml中配置cassandra?

如何在Spring Boot中调试请求?

如何在Spring Boot中禁用ErrorPageFilter?

如何在spring boot中查询关系?

如何在Spring Boot Web应用程序中配置2个单独的过滤器?

如何在Spring Boot 2中定义默认处理程序

如何在Spring Boot中以Spring Security级别启用CORS

如何在Spring Boot中替换Spring ApplicationContext

如何在Heroku上为Spring Boot应用强制HTTPS?

如何在VSCode中运行Spring Boot Maven项目以及如何配置Spring Boot Web应用程序的基本URL

如何在Spring Boot中使用spring-security.xml中的配置?

如何在spring-boot中禁用spring-data-mongodb自动配置

如何在Spring Boot中选择配置文件

如何配置Spring Boot 2在两个端口(HTTP和HTTPS)上运行Tomcat?

我如何在 Spring Boot 2 应用程序中为资产管道(bertramlabs)配置静态版本控制(摘要)?

我如何在Spring Boot中在DatabaseConfig中设置属性

如何在Spring Boot中处理DeferredResult中的异常?