后端三郎@ZYJ 2024-07-21 09:13 采纳率: 33.3%
浏览 7

springmvc集成jasypt

org.jasypt.exceptions.EncryptionInitializationException: Password not set for Password Based Encryptor

 @Bean
    public static EncryptablePropertyPlaceholderConfigurer  propertySourcesPlaceholderConfigurer(
            @Qualifier("jasyptStringEncryptor") StandardPBEStringEncryptor encryptor) {
        StandardPBEStringEncryptor standardPBEStringEncryptor = new StandardPBEStringEncryptor();
        standardPBEStringEncryptor.setPassword("123456");
        String envValue = System.getenv("123456");
        System.out.println("================="+envValue);
        if(envValue.equals("123456")){
            EncryptablePropertyPlaceholderConfigurer configurer = new EncryptablePropertyPlaceholderConfigurer(encryptor);
            configurer.setLocation(new ClassPathResource("jdbc.properties"));
            return configurer;
        }
        EncryptablePropertyPlaceholderConfigurer encryptablePropertyPlaceholderConfigurer = new EncryptablePropertyPlaceholderConfigurer(standardPBEStringEncryptor);
        encryptablePropertyPlaceholderConfigurer.setLocation(new ClassPathResource("newjdbc.properties"));
        return encryptablePropertyPlaceholderConfigurer;
    }

提示报错没有设置解密 密码

  • 写回答

4条回答 默认 最新

  • 阿里嘎多学长 2024-07-21 09:13
    关注

    以下内容由AIGC及阿里嘎多学长共同生成、有用望采纳:


    根据您提供的代码和问题描述,您遇到的问题主要是 Jasypt 加密器的密码没有正确设置导致的 EncryptionInitializationException 异常。以下是针对您的问题的一些建议和解决方案:

    1. 确保密码正确设置:在您的代码中,您已经创建了一个新的 StandardPBEStringEncryptor 实例并设置了密码。但是,您在方法签名中使用了 @Qualifier("jasyptStringEncryptor") 注解,这意味着 Spring 应该注入一个已经配置好的 StandardPBEStringEncryptor 实例。如果这个注入的实例没有设置密码,那么您需要确保在 Spring 容器中有一个配置了密码的 StandardPBEStringEncryptor 定义。

    2. 正确读取环境变量:您的代码中使用了 System.getenv("123456") 来读取环境变量,这实际上是错误的,因为环境变量名应该是一个字符串,而不是数字。您应该使用一个有意义的环境变量名,例如 JASYPT_PASSWORD,并确保它在操作系统中正确设置。

    3. 处理配置文件的加载:您的代码中已经根据环境变量的值来决定加载哪个配置文件,这是正确的。但是,您需要确保 EncryptablePropertyPlaceholderConfigurer 正确地与 Spring 容器中的 StandardPBEStringEncryptor 实例关联。

    以下是修改后的代码示例,它解决了上述问题:

    @Bean
    public EncryptablePropertyPlaceholderConfigurer propertySourcesPlaceholderConfigurer(
            @Qualifier("jasyptStringEncryptor") StandardPBEStringEncryptor encryptor) {
        // 确保注入的 encryptor 实例已经设置了密码
        if (encryptor == null || encryptor.getPassword() == null) {
            throw new IllegalStateException("Encryptor password must be set.");
        }
    
        String envValue = System.getenv("JASYPT_PASSWORD"); // 使用正确的环境变量名
        System.out.println("================="+envValue);
    
        // 根据环境变量的值来决定加载哪个配置文件
        EncryptablePropertyPlaceholderConfigurer configurer;
        if ("123456".equals(envValue)) { // 确保环境变量的值与预期匹配
            configurer = new EncryptablePropertyPlaceholderConfigurer(encryptor);
            configurer.setLocation(new ClassPathResource("jdbc.properties"));
        } else {
            configurer = new EncryptablePropertyPlaceholderConfigurer(encryptor);
            configurer.setLocation(new ClassPathResource("newjdbc.properties"));
        }
        return configurer;
    }
    

    请注意,您需要确保 Spring 容器中的 StandardPBEStringEncryptor 实例已经通过其他配置正确设置了密码。如果还没有设置,您需要添加相应的配置代码。同时,请确保环境变量 JASYPT_PASSWORD 在您的操作系统中正确设置,并且其值与您的预期密码匹配。

    评论 编辑记录

报告相同问题?

问题事件

  • 创建了问题 7月21日

悬赏问题

  • ¥50 我在一个购物网站的排队系统排队,这个排队到号后重新定向到目标网站进行购物,但是有技术牛通过技术方法直接跳过排队系统进入目标网址购物,有没有什么软件或者脚本可以用
  • ¥15 ios可以实现ymodem-1k协议 1024字节传输吗?
  • ¥300 寻抓云闪付tn组成网页付款链接
  • ¥15 请问Ubuntu要怎么安装chrome呀?
  • ¥15 视频编码 十六进制问题
  • ¥15 unity terrain打包后地形错位,跟建筑不在同一个位置,怎么办
  • ¥15 uniapp实现如下图的图表功能
  • ¥15 u-subsection如何修改相邻两个节点样式
  • ¥30 vs2010开发 WFP(windows filtering platform)
  • ¥15 服务端控制goose报文控制块的发布问题