当前位置:   article > 正文

【Java】springboot整合jasypt_springboot jasypt maven

springboot jasypt maven

jasypt

保证项目中的账号密码不以明文的形式展示

springboot集成jasypt

1.引入maven依赖

<dependency>
	<groupId>com.github.ulisesbocchio</groupId>
    <artifactId>jasypt-spring-boot-starter</artifactId>
    <version>3.0.4</version>
</dependency>
  • 1
  • 2
  • 3
  • 4
  • 5

2.启动类添加注解

import com.ulisesbocchio.jasyptspringboot.annotation.EnableEncryptableProperties;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@EnableEncryptableProperties
public class IpSourceApplication {
    public static void main(String[] args) {
       SpringApplication.run(IpSourceApplication.class, args);
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

3.yaml配置

jasypt:
  encryptor:
    password: 02700083-9fd9-4b82-a4b4-9177e0560e92
    algorithm: PBEWithMD5AndDES
    iv-generator-classname: org.jasypt.iv.NoIvGenerator
my:
  username: ENC(atRC+VNwB17CQVilGftfQg==)
  password: ENC(Or0FKbtskiXsJlFtI23FxA==)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

4.加解密测试类

import org.jasypt.util.text.BasicTextEncryptor;

public class Test01 {

    public static void main(String[] args) {
        //该类的选择根据algorithm:PBEWithMD5AndDE选择的算法选择
        BasicTextEncryptor encryptor = new BasicTextEncryptor();
        encryptor.setPassword("02700083-9fd9-4b82-a4b4-9177e0560e92");
        String encrypt = encryptor.encrypt("root");
        System.out.println(encrypt);
        String decrypt = encryptor.decrypt(encrypt);
        System.out.println(decrypt);
        encrypt = encryptor.encrypt("mysql");
        System.out.println(encrypt);
        decrypt = encryptor.decrypt(encrypt);
        System.out.println(decrypt);
    }

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

读取配置效果

@RestController
public class IpController implements InitializingBean {

    @Value("${my.username}")
    private String username;

    @Value("${my.password}")
    private String password;

    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("username:"+username+",password:"+password);
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小惠珠哦/article/detail/945665
推荐阅读
相关标签
  

闽ICP备14008679号