当前位置:首页 > 说编程 > 正文内容

springboot中jasypt自定义加密解密使用

2023-03-06说编程1730

加密命令:

java -cp ./jasypt-1.9.3.jar org.jasypt.intf.cli.JasyptPBEStringEncryptionCLI algorithm=PBEWithMD5AndDES password=suyu.net input=test

解密命令:

java -cp ./jasypt-1.9.3.jar org.jasypt.intf.cli.JasyptPBEStringDecryptionCLI algorithm=PBEWithMD5AndDES password=suyu.net input=JKTA6O9YoXtcgZFaGb1JSw==

maven依赖:

<dependency>
        <groupId>com.github.ulisesbocchio</groupId>
        <artifactId>jasypt-spring-boot-starter</artifactId>
        <version>3.0.4</version>
</dependency>

配置使用:

ENC(JKTA6O9YoXtcgZFaGb1JSw==)

配置类:MyEncryptorConfig

package net.suyu.demo.configure;

import com.ulisesbocchio.jasyptspringboot.properties.JasyptEncryptorConfigurationProperties;
import org.jasypt.encryption.StringEncryptor;
import org.jasypt.encryption.pbe.PooledPBEStringEncryptor;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableConfigurationProperties(JasyptEncryptorConfigurationProperties.class)
public class MyEncryptorConfig {

    private final String password="suyu.net";
    
    /**
     * 默认找jasyptStringEncryptor这个bean,这里直接取这个名字进行注册,省去额外配置
     * @param properties
     * @return
     */
    @Bean
    StringEncryptor jasyptStringEncryptor(JasyptEncryptorConfigurationProperties properties){
        return new StringEncryptor() {
            // 加密
            @Override
            public String encrypt(String message) {
                // todo 加密实现
                return message;
            }
            // 解密
            @Override
            public String decrypt(String encryptedMessage) {
                // todo 解密实现
                PooledPBEStringEncryptor encryptor = new PooledPBEStringEncryptor();
                encryptor.setPassword(password);
                encryptor.setAlgorithm("PBEWithMD5AndDES");
                encryptor.setPoolSize(1);
                return encryptor.decrypt(encryptedMessage);
            }
        };
    }
}


扫描二维码推送至手机访问。

本站内容仅供学习研究,转载请以链接形式注明,如有侵犯请联系处理。

本文链接:https://suyu.net/post/117.html

分享给朋友:

发表评论

看不清,换一张

◎欢迎参与讨论,请在这里发表您的看法和观点。