Grails 3 默认强制使用 https

Grails 3 force https by default

我有一个 Grails 3 应用程序正在开发中。我想强制 运行-app 默认使用 https。有几篇帖子可以追溯到大约一年前,Most recent post,展示了默认情况下强制使用 https 的可能方法。即使用别名,或使用 运行-app -https 命令。

我将此问题重新提交给 table 以查看是否有人在不使用之前帖子中的显式方法的情况下默认设置了 https 运行。当然,对于那些总是希望 运行 默认启用 https 的应用程序的人来说,无论是通过 cli 还是 IDE,这是一个理想的功能。我们能否在 运行-app 命令之前的某处将它作为参数传递,这样我们就不必在不同的使用环境中明确声明它?

例如:

我曾尝试编辑构建文件以传递 jvm 参数,但没有成功。

bootRun {
    jvmArgs = "-D-https"
}

为了完成这项工作,我换了一种不同的方法。这个问题可以按照post中的说明解决:

与-https 选项不同,使用这种方法,我们需要创建自己的密钥库文件并将其存储在某个地方。我们还可以使用 grails 生成的密钥库,默认情况下它位于构建目录中。但是,我们必须将其位置移动到更永久的位置,因为可以清理构建目录。注意:您可以通过检查bootRun中传递的systemProperties来获取keystore的密码和数据。

bootRun {
    print systemProperties
}

我的输出如下:

endpoints.shutdown.enabled:true, env:development, full.stacktrace:false, grails.env:development, grails.full.stacktrace:false, info.app.grailsVersion:3.1.2, info.app.name:coolio, info.app.version:0.1, interactive.mode.enabled:true, run.active:true, server.port:8443, server.ssl.key-password:123456, server.ssl.key-store:./build/ssl/keystore, server.ssl.key-store-password:123456, verbose:false

我的配置没有作者所显示的确切属性,而是只需要部分的混合:

private static Connector getSslConnector() {
    def dir = System.getProperty("user.dir")
    Connector connector = new Connector();
    connector.setPort(8443);
    connector.setSecure(true);
    connector.setScheme("https");
    connector.setAttribute("keystorePass", "coolio");
    connector.setAttribute("keystoreFile", dir + "/.keystore");
    connector.setAttribute("clientAuth", "false");
    connector.setAttribute("protocol", "HTTP/1.1");
    connector.setAttribute("sslProtocol", "TLS");
    connector.setAttribute("maxThreads", "200");
    connector.setAttribute("protocol", "org.apache.coyote.http11.Http11NioProtocol");
    connector.setAttribute("SSLEnabled", true);
    return connector;
}

注意,原来post设置协议属性为:

connector.setAttribute("protocol", "org.apache.coyote.http11.Http11AprProtocol");

我将属性设置为:

connector.setAttribute("protocol", "org.apache.coyote.http11.Http11NioProtocol");

除此之外,将连接器附加到 tomcat 服务,如上一篇文章所示,一切都应按预期工作。您现在可以执行 运行-app,同时存在 http 和 https。