简体   繁体   English

jasypt 解密密码在测试中有效,但在 spring 启动应用程序中无效

[英]jasypt decryption password working in test but not in spring boot application

I am trying to learn jasypt 3.0.2.我正在尝试学习 jasypt 3.0.2。 I have created a simple spring boot application我创建了一个简单的 spring 启动应用程序

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

its working fine and connecting DB.它工作正常并连接数据库。

now I am trying to encrypt the password field with jasypt.现在我正在尝试用 jasypt 加密密码字段。 I have created a sample main method to generate encrypted text and decrypt it again to validate.我创建了一个示例主要方法来生成加密文本并再次对其进行解密以进行验证。

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"));
}`

for instance, right now it generated maAm5JPu/Mw/e+/uSGzlkKUX7Vk1vv1py+Nr4ihrNUjKfdXwBO1SJKcuxjx5/nZQ so my updated applicaton.properties file is例如,现在它生成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

main class is主class是

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

build.gradle 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()
}

but it's giving me an exception但它给了我一个例外

 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]

what am I doing wrong?我究竟做错了什么?

please add following config in your application.properties:请在您的 application.properties 中添加以下配置:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM