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

Raj*_*Raj 31 spring-boot jhipster

我已经创建了示例jHipster app.现在我想在本地添加自签名SSL证书和测试以访问https.怎么做到这一点?

Dri*_*mri 70

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

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

  1. 生成自签名证书
  2. Spring Boot文档中所述,将SSL属性添加到application.properties或application.yml
  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
Run Code Online (Sandbox Code Playgroud)

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

将SSL属性添加到您application.propertiesSpring Boot文档中application.yml提到的

现在,您需要为Tomcat添加HTTPS连接器属性.您可以在中找到属性(yml)文件,src/main/resources/并且需要更新application.yml(或者如果它仅用于application-dev.yml具有以下属性的开发:

server:
  ssl:
    key-store: keystore.p12
    key-store-password: mypassword
    keyStoreType: PKCS12
    keyAlias: tomcat
Run Code Online (Sandbox Code Playgroud)

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

为了简单起见,我没有更改端口,但理想情况下你应该在属性文件中更改它,但我把它留了出来,因为它们已经被定义了application-dev.yml,application-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;
  }
Run Code Online (Sandbox Code Playgroud)

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


Mic*_*ksa 5

扩展Driss Amri关于如何重新启用的精彩答案BrowserSync.

如果您选择不支持http,或者http被重定向到https,BrowserSync则无效.为了使其再次起作用,在以下方面进行的改动很少:

  1. gulp/config.js,apiPort以及uri:

    apiPort: 8443, 
    uri: 'https://localhost:',
    
    Run Code Online (Sandbox Code Playgroud)
  2. 吞掉/ 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);
    }));
    
    Run Code Online (Sandbox Code Playgroud)
  3. 也可以选择BrowserSync通过https提供内容.我推荐它Spring Social以省去一些麻烦.只需添加https: truebrowserSync在一饮而尽/ serve.js电话:

    browserSync({
        open: true,
        port: config.port,
        server: {
            baseDir: config.app,
            middleware: proxies
        },
        https: true
    });
    
    Run Code Online (Sandbox Code Playgroud)

    现在,BrowserSync将使用随附的自签名证书提供内容.可以Spring BootBrowserSync主页上重复使用创建的更多内容.