【问题标题】:How to add self signed SSL certificate to jHipster sample app?如何将自签名 SSL 证书添加到 jHipster 示例应用程序?
【发布时间】:2015-06-13 20:26:29
【问题描述】:

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

【问题讨论】:

    标签: spring-boot jhipster


    【解决方案1】:

    对于那些使用 webpack 而不是 gulp 的人,您可以通过两个更改来完成 Driss Amri's 答案:

    修改proxy.conf.json

    {
        "*": {
            "target": "https://localhost:8443",
            "secure": true
        }
    }
    

    这会将 API 请求重定向到新的 https 地址。 然后也改变 webpack 文件,例如这里的 webpack.dev.js 修改示例:

    module.exports = webpackMerge(commonConfig({ env: ENV }), {
    devtool: 'eval-source-map',
    devServer: {
        contentBase: './target/www',
        proxy: [{
            context: [
                /* jhipster-needle-add-entity-to-webpack - JHipster will add entity api paths here */
                '/api',
                '/management', ...
                '/auth'
            ],
            target: 'https://127.0.0.1:8443',
            /* set secure to false here, otherwise self-signed certification cause DEPTH_ZERO_SELF_SIGNED_CERT proxy errors */
            secure: false
        }]
    },
    

    【讨论】:

    • 产品怎么样? webpackprod.prod.js ?由于 Driss Amri 的回答对我不起作用,我可以在那里改变什么?
    • Webpack 使用环境变量,如下所述:atendesigngroup.com/blog/… 因此在这种情况下名称将是 webpack.prod.js
    【解决方案2】:

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

    从头开始时需要完成这些步骤:

    1. 生成自签名证书
    2. 将 SSL 属性添加到您的 application.properties 或 application.yml,如 Spring Boot documentation 中所述
    3. (可选)将 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.yml,如 Spring 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:run 运行应用程序。您现在可以通过 https://localhost:8080

    访问您的应用程序

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


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

    您只能通过application.properties 启用一种协议,因此当您像上面那样执行此操作时,只有 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/

    【讨论】:

    • 我需要将 keystore.p12 文件保存在项目的类路径中吗?
    • 我将 keystore.p12 留在了项目的根目录中,并使用 mvn spring-boot:run 从根目录运行。如果你把它放在你的类路径中,你可以将 key-store 属性更改为: key-store: classpath:keystore.p12
    • 如果这个答案对你有帮助@Raj,请接受答案:-)
    • 嗨@Driss。现在我无法访问示例应用程序.. 如果我在浏览器中输入 127.0.0.1:8080/#,即如果我输入 http:// 它应该将我重定向到 https://
    • 此时只能通过应用属性激活一种协议。如果你想要 HTTP 和 HTTPS,你需要以编程方式激活一个。我在drissamri.be/blog/java/enable-https-in-spring-boot 上写了关于这个用例的博客,我也会在这里编辑回复
    【解决方案3】:

    扩展Driss Amri brilliant answer如何重新启用BrowserSync

    如果您选择不支持 http,或者如果 http 被重定向到 https,BrowserSync 将不起作用。要使其再次工作,需要在以下方面进行一些更改:

    1. gulp/config.jsapiPorturi 到:

      apiPort: 8443, 
      uri: 'https://localhost:',
      
    2. gulp/serve.js:将options.rejectUnauthorized = false; 添加到proxyRoutes 中,这样节点就不会抱怨自签名证书:

      proxyRoutes.map(function (r) {
          var options = url.parse(baseUri + r);
          options.route = r;
          options.preserveHost = true;
          options.rejectUnauthorized = false;
          return proxy(options);
      }));
      
    3. 也可选择让BrowserSync 通过 https 提供内容。我建议使用Spring Social 来节省一些麻烦。只需将 https: true 添加到 gulp/serve.js 中的 browserSync 调用中:

      browserSync({
          open: true,
          port: config.port,
          server: {
              baseDir: config.app,
              middleware: proxies
          },
          https: true
      });
      

      现在 BrowserSync 将使用随附的自签名证书提供内容。可以重用为Spring Boot 创建的那个,更多关于BrowserSync homepage

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-04-18
      • 1970-01-01
      • 2019-03-27
      • 1970-01-01
      • 2023-04-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多