【问题标题】:Micronaut decrypting property valueMicronaut 解密属性值
【发布时间】:2020-01-23 00:05:44
【问题描述】:

我正在尝试从 spring 移动到 micronaut。一些属性值为encrypted,我们目前正在使用spring-boot-jasypt,如下所述,以帮助decrypt 注入spring 时的属性值强>应用。

但是,我还没有找到在 micronaut 框架中添加属性加密器的方法来让我做同样的事情。有谁知道 micronaut 中的方法吗?

https://www.baeldung.com/spring-boot-jasypt

【问题讨论】:

    标签: micronaut


    【解决方案1】:

    以下方法对我有用。创建@Context 带注释的Singleton,它将在所有其他bean 之前创建。在创建时Environment 已经初始化并且所有配置属性都已加载。接下来找到所有以定义加密值的特殊前缀开头的属性。使用解密的值创建新的属性源并将其作为新的PropertySource 放入Environment

    代码:

    @Context
    @Singleton
    @Requires(property = "JASYPT_ENCRYPTOR_PASSWORD")
    @Slf4j
    public class JasyptConfigurationPropertiesDecryptor implements BeanInitializedEventListener<Environment>  {
        private static final String PREFIX = "ENC(";
    
        public JasyptConfigurationPropertiesDecryptor(DefaultEnvironment env, @Value("${JASYPT_ENCRYPTOR_PASSWORD}") String encPassword) {
            log.info("JasyptBootstrapDecryptor started");
            processConfigurationProperties(env, encPassword);
        }
    
        public void processConfigurationProperties(DefaultEnvironment env, String encPassword) {
            StandardPBEStringEncryptor encryptor = encryptor(encPassword);
            Map<String, Object> all = env.getAllProperties(StringConvention.RAW, MapFormat.MapTransformation.FLAT);
            Map<String, Object> decrypted = all.entrySet().stream()
                                               .filter(entry -> entry.getValue().toString().startsWith(PREFIX))
                                               .collect(collector(encryptor));
    
            env.addPropertySource(MapPropertySource.of("decrypted", decrypted));
        }
    
        private StandardPBEStringEncryptor encryptor(String encPassword) {
            StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor();
            encryptor.setPassword(encPassword);
            encryptor.setStringOutputType("base64");
            encryptor.setAlgorithm("PBEWITHHMACSHA512ANDAES_256");
            encryptor.setIvGenerator(new RandomIvGenerator());
            return encryptor;
        }
    
        @Nonnull
        private Collector<Map.Entry<String, Object>, ?, Map<String, Object>> collector(StandardPBEStringEncryptor encryptor) {
            return Collectors.toMap(Map.Entry::getKey, entry -> decrypt(entry, encryptor));
        }
    
        private String decrypt(Map.Entry<String, Object> entry, StandardPBEStringEncryptor encryptor) {
            log.info("Decrypt \"{}\" configuration property", entry.getKey());
            String encrypted = entry.getValue().toString().substring(PREFIX.length(), entry.getValue().toString().length() - 1);
            return encryptor.decrypt(encrypted);
        }
    
        @Override
        public Environment onInitialized(BeanInitializingEvent<Environment> event) {
            return event.getBean();
        }
    
    
    }
    

    【讨论】:

      【解决方案2】:

      我自己是 micronaut 的新手。但到目前为止我发现的最好的方法是将加密的属性放入一个单独的文件中,在启动时自己加载和解密它们,并将它们未加密的添加到 java 系统属性(或我知道 micronaut 可以找到它们的任何其他地方)。所有这些都应该在初始化或启动 Micronaut 上下文之前完成。然后我可以使用 ${...} 值占位符表示法从 micronaut 的配置文件中引用这些属性。

      更惯用的方法似乎是实现和注册您自己的 PropertySourceLoader,但它需要更多的努力并且结果几乎相同 - 它仍然需要将加密的属性放入单独的文件中。

      如果您想将加密的属性保留在原处,我能找到的最好方法是自己加载所有属性,然后将它们作为 PropertySources 提供给 micronaut。但是这种方式会丢弃 micronaut 的大部分配置支持,并且在我的书中违背了目的。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-03-11
        • 2019-08-18
        • 2019-09-23
        • 2011-04-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多