1.使用环境
springboot:2.1.4.RELEASE
JDK:8
jasypt-spring-boot-starter:3.0.2
2.引入依赖
!-- 配置文件加密 -->
<dependency><groupId>com.github.ulisesbocchio</groupId><artifactId>jasypt-spring-boot-starter</artifactId><version>3.0.2</version>
</dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>2.0.1</version>
</dependency>
<dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>8.0.33</version>
</dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><optional>true</optional>
</dependency>
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope>
</dependency>
3.application.properties配置
server.port=8080### mybatis
mybatis.mapper-locations=classpath:mapper/*.xml
mybatis.type-aliases-package=com.xx.xx ### xxl-job, datasource
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/xxx?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&serverTimezone=Asia/Shanghai
spring.datasource.username=root
spring.datasource.password=ENC(jGfd15IFYKMjMW5bUYp06BQ8OCdHTXi3zbD4j9tw6U3PPvvA5GS8zn10kgVmgxXY)
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver#加密盐值,获取加密后的值后删除,在vmOptions配置(如下图)
jasypt.encryptor.password=123adf
加密后,可删除jasypt.encryptor.password配置;发版时可在命令行中配置
4.明文加密
@SpringBootTest
@RunWith(SpringRunner.class)
public class D2ApplicationTests {@Resourceprivate StringEncryptor jasyptStringEncryptor;@Testpublic void contextLoads() {String pwd = jasyptStringEncryptor.encrypt("root");System.out.println("pwd:" + pwd);}}
5.所遇问题
org.jasypt.exceptions.EncryptionOperationNotPossibleException:
Encryption raised an exception.
A possible cause is you are using strong encryption algorithms and you have not
installed the Java Cryptography Extension (JCE) Unlimited Strength
Jurisdiction Policy Files in this Java Virtual Machine
解决方案:java密码扩展无限制权限策略文件安装–jce_policy安装
文件下载地址:
下面为下载链接
JDK 1.7–jce安装地址:JDK1.7 -jce地址
JDK 1.8–jce安装地址:JDK 1.8–jce安装地址
相关文件来自于博客:https://www.cnblogs.com/zgngg/p/13859299.html) 感谢博主:疯狂的⑨酱 的无私奉献
jar配置路径:
JDK:将两个jar文件放到%JDK_HOME%\jre\lib\security下
JRE:将两个jar文件放到%JRE_HOME%\lib\security下