diff --git a/pom.xml b/pom.xml index db51795b..0de89494 100644 --- a/pom.xml +++ b/pom.xml @@ -207,6 +207,11 @@ 20.0.0 compile + + com.github.ulisesbocchio + jasypt-spring-boot + 3.0.3 + diff --git a/src/main/java/com/dispose/PhoenixBootApplication.java b/src/main/java/com/dispose/PhoenixBootApplication.java index 0a7620f0..e83217df 100644 --- a/src/main/java/com/dispose/PhoenixBootApplication.java +++ b/src/main/java/com/dispose/PhoenixBootApplication.java @@ -1,5 +1,6 @@ package com.dispose; +import com.ulisesbocchio.jasyptspringboot.annotation.EnableEncryptableProperties; import lombok.extern.slf4j.Slf4j; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; @@ -19,6 +20,7 @@ import org.springframework.transaction.annotation.EnableTransactionManagement; @EnableScheduling @EnableAspectJAutoProxy @EnableTransactionManagement +@EnableEncryptableProperties @MapperScan(basePackages = {"com.dispose.mapper"}) @Slf4j public class PhoenixBootApplication { diff --git a/src/main/java/com/dispose/interceptor/ConfigSecurityInterceptor.java b/src/main/java/com/dispose/interceptor/ConfigSecurityInterceptor.java new file mode 100644 index 00000000..b0d4bbeb --- /dev/null +++ b/src/main/java/com/dispose/interceptor/ConfigSecurityInterceptor.java @@ -0,0 +1,36 @@ +package com.dispose.interceptor; + +import lombok.extern.slf4j.Slf4j; +import org.jasypt.encryption.StringEncryptor; +import org.jasypt.encryption.pbe.PooledPBEStringEncryptor; +import org.jasypt.encryption.pbe.config.SimpleStringPBEConfig; +import org.springframework.context.annotation.Bean; + +/** + * The type Config security interceptor. + * + * @author + */ +@Slf4j +public class ConfigSecurityInterceptor { + /** + * String encryptor string encryptor. + * + * @return the string encryptor + */ + @Bean("jasyptStringEncryptor") + static public StringEncryptor stringEncryptor() { + PooledPBEStringEncryptor encryptor = new PooledPBEStringEncryptor(); + SimpleStringPBEConfig config = new SimpleStringPBEConfig(); + config.setPassword("xajhuang"); + config.setAlgorithm("PBEWITHHMACSHA512ANDAES_256"); + config.setKeyObtentionIterations("1000"); + config.setPoolSize("1"); + config.setProviderName("SunJCE"); + config.setSaltGeneratorClassName("org.jasypt.salt.RandomSaltGenerator"); + config.setIvGeneratorClassName("org.jasypt.iv.NoIvGenerator"); + config.setStringOutputType("base64"); + encryptor.setConfig(config); + return encryptor; + } +} diff --git a/src/test/java/com/dispose/test/dev/function/CryptoConfigureFile.java b/src/test/java/com/dispose/test/dev/function/CryptoConfigureFile.java new file mode 100644 index 00000000..7cd3e66b --- /dev/null +++ b/src/test/java/com/dispose/test/dev/function/CryptoConfigureFile.java @@ -0,0 +1,34 @@ +package com.dispose.test.dev.function; + +import com.ulisesbocchio.jasyptspringboot.annotation.EnableEncryptableProperties; +import lombok.extern.slf4j.Slf4j; +import org.jasypt.encryption.StringEncryptor; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.context.annotation.Configuration; +import org.springframework.test.context.junit4.SpringRunner; + +import javax.annotation.Resource; + +@RunWith(SpringRunner.class) +@SpringBootTest +@EnableEncryptableProperties +@Configuration +@Slf4j +public class CryptoConfigureFile { + + @Resource + private StringEncryptor encryptor; + + @Test + public void t1_jasyptEncrypt() { + String srcTest = "root"; + String enText = encryptor.encrypt(srcTest); + String deTest = encryptor.decrypt(enText); + + log.info("Src: {}", srcTest); + log.info("Encrypt: {}", enText); + log.info("Decrypt: {}", deTest); + } +}