我在Grails 3中遇到了配置问题,并希望挂钩到配置设置过程,除非有更好的方法
我们有一个插件(jasypt(,它需要在application.yml
中设置配置,否则它会抛出错误(下面的示例(。我们需要在runtime.groovy
中设置配置值,但它们不会覆盖yml
中设置的任何值
application.yml
jasypt:
algorithm: "PBEWITHSHA256AND256BITAES-CBC-BC"
providerName: "BC"
password: "-"
keyObtentionIterations: 10
runtime.groovy
jasypt {
algorithm = ExternalSecureKeyConfig.getInstance().jasypt.algorithm
providerName = ExternalSecureKeyConfig.getInstance().jasypt.providerName
password = ExternalSecureKeyConfig.getInstance().jasypt.password
keyObtentionIterations = ExternalSecureKeyConfig.getInstance().jasypt.keyObtentionIterations
}
在启动和运行时进行调试显示,没有一个配置被覆盖(密码保持"-"(。如果我将配置从yml
移动到application.groovy
则应用运行时值,但在启动时出现以下错误
org.jasypt.exceptions.EncryptionInitializationException: If "encryptorRegisteredName" is not specified, then "password" (and optionally "algorithm" and "keyObtentionIterations") must be specified
目前,我希望我可以手动设置 jsypt 配置值或挂钩到时髦的配置设置中,以确保它们覆盖上一个设置的 yml 值。无法弄清楚如何做到这一点或是否可能
jasypt v 2.0.2 with Grails 3.3.10 (hibernate5, gorm 6.1)
求解
使用自定义配置文件中的值覆盖 application.yml 配置。仍然发现是 runtime.groovy 不会覆盖 yml 配置值。
class Application extends GrailsAutoConfiguration implements EnvironmentAware {
static void main(String[] args) {
GrailsApp.run(Application, args)
}
@Override
void setEnvironment(Environment environment) {
URL jasyptConfigUrl = getClass().classLoader.getResource('jasypt.groovy')
if (jasyptConfigUrl) {
def jasyptConfig = new ConfigSlurper().parse(jasyptConfigUrl)
environment.propertySources.addFirst(new MapPropertySource('jasypt.groovy', jasyptConfig))
}
}
}