【问题标题】:jasypt decryption password working in test but not in spring boot applicationjasypt 解密密码在测试中有效,但在 Spring Boot 应用程序中无效
【发布时间】:2020-05-23 20:01:08
【问题描述】:

我正在尝试学习 jasypt 3.0.2。 我创建了一个简单的 Spring Boot 应用程序

spring.datasource.username=root
spring.datasource.password=root`
spring.datasource.url=jdbc:mysql://localhost:3306/student_db?jdbcCompliantTruncation=false&sessionVariables=sql_mode='NO_ENGINE_SUBSTITUTION'&useSSL=false&useServerPrepStmts=false&rewriteBatchedStatements=true&useUnicode=true&characterEncoding=utf8
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL8Dialect

它工作正常并连接数据库。

现在我正在尝试使用 jasypt 加密密码字段。我创建了一个示例 main 方法来生成加密文本并再次对其进行解密以进行验证。

private static StringEncryptor stringEncryptor() {
    PooledPBEStringEncryptor encryptor = new PooledPBEStringEncryptor();
    SimpleStringPBEConfig config = new SimpleStringPBEConfig();
    config.setPassword("password");
    config.setAlgorithm("PBEWITHHMACSHA512ANDAES_256");
    config.setKeyObtentionIterations("1000");
    config.setPoolSize("1");
    config.setProviderName("SunJCE");
    config.setSaltGeneratorClassName("org.jasypt.salt.RandomSaltGenerator");
    config.setIvGeneratorClassName("org.jasypt.iv.RandomIvGenerator");
    config.setStringOutputType("base64");
    encryptor.setConfig(config);
    return encryptor;
}
private static String encrypt(String text) {
    StringEncryptor textEncryptor = stringEncryptor();
    String encryptedText = textEncryptor.encrypt(text);
    return encryptedText;
}
private static String decrypt(String text) {
    StringEncryptor textEncryptor = stringEncryptor();
    String decryptedText = textEncryptor.decrypt(text);
    return decryptedText;
}
public static void main(String[] args) {
    System.out.println(encrypt("root"));
    System.out.println(decrypt("L5fd23Kr2q0tFRpxe+FdjTSrcF1jMD1iiriNvNQEfYHR6tODsx8E5ec/uX+evaMI"));
}`

例如,现在它生成了maAm5JPu/Mw/e+/uSGzlkKUX7Vk1vv1py+Nr4ihrNUjKfdXwBO1SJKcuxjx5/nZQ,所以我更新的 applicaton.properties 文件是

    spring.datasource.url=jdbc:mysql://localhost:3306/student_db?jdbcCompliantTruncation=false&sessionVariables=sql_mode='NO_ENGINE_SUBSTITUTION'&useSSL=false&useServerPrepStmts=false&rewriteBatchedStatements=true&useUnicode=true&characterEncoding=utf8
    spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
    spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL8Dialect
    spring.datasource.username=root
    spring.datasource.password=ENC(maAm5JPu/Mw/e+/uSGzlkKUX7Vk1vv1py+Nr4ihrNUjKfdXwBO1SJKcuxjx5/nZQ)
    jasypt.encryptor.algorithm=PBEWITHHMACSHA512ANDAES_256
    jasypt.encryptor.password=password
    jasypt.encryptor.iv-generator-classname=org.jasypt.iv.NoIvGenerator

主类是

@SpringBootApplication
@EnableEncryptableProperties
public class DemoApp {
    public static void main(String[] args) {
        SpringApplication.run(DemoApp.class, args);
    }
}

build.gradle

plugins {
    id 'org.springframework.boot' version '2.2.6.RELEASE'
    id 'io.spring.dependency-management' version '1.0.9.RELEASE'
    id 'java'
}    
group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '14'    
configurations {
    compileOnly {
        extendsFrom annotationProcessor
    }
}    
repositories {
    mavenCentral()
}    
dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
    implementation 'org.springframework.boot:spring-boot-starter-web'
    compile 'mysql:mysql-connector-java'
    compile 'com.github.ulisesbocchio:jasypt-spring-boot-starter:3.0.2'
}    
test {
    useJUnitPlatform()
}

但它给了我一个例外

 org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSourceConfig': Injection of autowired dependencies failed; nested exception is com.ulisesbocchio.jasyptspringboot.exception.DecryptionException:
Unable to decrypt: ENC(maAm5JPu/Mw/e+/uSGzlkKUX7Vk1vv1py+Nr4ihrNUjKfdXwBO1SJKcuxjx5/nZQ). Decryption of Properties failed,  make sure encryption/decryption passwords match at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:405) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]

我做错了什么?

【问题讨论】:

    标签: spring-boot encryption jasypt


    【解决方案1】:

    请在您的 application.properties 中添加以下配置:

     jasypt.encryptor.iv-generator-classname=org.jasypt.iv.NoIvGenerator
    

    【讨论】:

      猜你喜欢
      • 2021-12-02
      • 1970-01-01
      • 2020-03-07
      • 2021-01-09
      • 2012-03-26
      • 1970-01-01
      • 2019-12-19
      • 2020-06-11
      • 1970-01-01
      相关资源
      最近更新 更多