【发布时间】:2015-06-13 20:26:29
【问题描述】:
我已经创建了示例 jHipster 应用程序。现在我想添加自签名 SSL 证书并在本地测试以访问 https。如何做到这一点?
【问题讨论】:
标签: spring-boot jhipster
我已经创建了示例 jHipster 应用程序。现在我想添加自签名 SSL 证书并在本地测试以访问 https。如何做到这一点?
【问题讨论】:
标签: spring-boot jhipster
对于那些使用 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
}]
},
【讨论】:
这些说明适用于 JHipster 所基于的所有 Spring Boot 应用程序。我已经在一个新生成的JHipster 2.7 项目上对此进行了测试。
从头开始时需要完成这些步骤:
首先你需要在你的项目目录中生成你的自签名证书,这可以通过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。
application.properties 或 application.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.yml 和 application-prod.yml 中定义,因此您必须更改它在那里或将其删除并放在通用application.yml
您只能通过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/
【讨论】:
扩展Driss Amri brilliant answer如何重新启用BrowserSync。
如果您选择不支持 http,或者如果 http 被重定向到 https,BrowserSync 将不起作用。要使其再次工作,需要在以下方面进行一些更改:
gulp/config.js、apiPort 和 uri 到:
apiPort: 8443,
uri: 'https://localhost:',
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);
}));
也可选择让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。
【讨论】: