gpt4 book ai didi

spring-boot - 如何将自签名 SSL 证书添加到 jHipster 示例应用程序?

转载 作者:行者123 更新时间:2023-12-03 08:45:17 30 4
gpt4 key购买 nike

我已经创建了示例 jHipster 应用程序。现在我想添加自签名 SSL 证书并在本地测试以访问 https。如何做到这一点?

最佳答案

这些说明适用于 JHipster 所基于的所有 Spring Boot 应用程序。我已经在新生成的 JHipster 2.7 上对此进行了测试项目。

从头开始时,您需要完成以下步骤:

  • 生成自签名证书
  • Spring Boot documentation 中所述,将 SSL 属性添加到您的 application.properties 或 application.yml
  • (可选)将 HTTP 重定向到 HTTPS


  • 生成自签名证书

    首先你需要在你的项目目录中生成你的自签名证书,这可以通过 keytool 来完成。 ,这是Java提供的实用脚本:
    keytool -genkey -alias tomcat -storetype PKCS12 -keyalg RSA -keysize 2048 -keystore keystore.p12 -validity 3650
    Enter keystore password:
    Re-enter new password:
    What is your first and last name?
    [Unknown]:
    What is the name of your organizational unit?
    [Unknown]:
    What is the name of your organization?
    [Unknown]:
    What is the name of your City or Locality?
    [Unknown]:
    What is the name of your State or Province?
    [Unknown]:
    What is the two-letter country code for this unit?
    [Unknown]:
    Is CN=Unknown, OU=Unknown, O=Unknown, L=Unknown, ST=Unknown, C=Unknown correct?
    [no]: yes

    我选择了密码 mypassword所以这是我将在下一步中使用的。完成此操作后,您将看到 keystore.p12在您当前的目录中。

    将 SSL 属性添加到您的 application.propertiesapplication.ymlSpring Boot documentation 中所述

    现在您需要为 Tomcat 添加 HTTPS 连接器属性。您可以在 src/main/resources/ 中找到属性 (yml) 文件。你需要更新 application.yml (或者如果它仅用于 application-dev.yml 中的开发,具有以下属性:
    server:
    ssl:
    key-store: keystore.p12
    key-store-password: mypassword
    keyStoreType: PKCS12
    keyAlias: tomcat

    现在,您可以使用 mvn clean package 使用 Maven(或 Gradle,如果您为 JHipster 应用程序选择它)打包您的应用程序。并使用 运行应用程序mvn spring-boot:运行 .您现在可以通过 访问您的应用程序https://localhost:8080

    为简单起见,我没有更改端口,但理想情况下您也应该在属性文件中更改它,但我将其省略,因为它们已经在 application-dev.yml 中定义和 application-prod.yml所以你必须在那里更改它或将其删除并将其放在一般 application.yml

    (可选)添加重定向 HTTP 到 HTTPS

    您只能通过 application.properties 启用一种协议(protocol),因此当您像上面那样执行此操作时,只有 HTTPS 才能工作。如果您希望 HTTP 也可以工作,并重定向到 HTTPS,您必须添加 @Configuration像下面这样的类
    @Bean
    public EmbeddedServletContainerFactory servletContainer() {
    TomcatEmbeddedServletContainerFactory tomcat = new TomcatEmbeddedServletContainerFactory() {
    @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(8080);
    connector.setSecure(false);
    connector.setRedirectPort(8443);

    return connector;
    }

    这个回复基本上是我关于同一主题的博客文章的副本: http://www.drissamri.be/blog/java/enable-https-in-spring-boot/

    关于spring-boot - 如何将自签名 SSL 证书添加到 jHipster 示例应用程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29522114/

    30 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com