|
| 1 | +# springboot炖jasypt |
| 2 | +### 1. 先睹为快 |
| 3 | +### 2. 实现原理 |
| 4 | +#### 2.1 新建项目 |
| 5 | +#### 2.2 创建maven目录结构,以及pom.xml文件 |
| 6 | +#### 2.3 pom.xml文件中加入依赖 |
| 7 | +```xml |
| 8 | +<parent> |
| 9 | + <groupId>org.springframework.boot</groupId> |
| 10 | + <artifactId>spring-boot-starter-parent</artifactId> |
| 11 | + <version>2.0.5.RELEASE</version> |
| 12 | + <relativePath/> |
| 13 | +</parent> |
| 14 | +``` |
| 15 | +#### 2.4 pom.xml文件中加入springboot-starter依赖 |
| 16 | +```xml |
| 17 | +<dependencies> |
| 18 | + <!-- web启动器 --> |
| 19 | + <dependency> |
| 20 | + <groupId>org.springframework.boot</groupId> |
| 21 | + <artifactId>spring-boot-starter-web</artifactId> |
| 22 | + </dependency> |
| 23 | + |
| 24 | + <!-- lombok支持 --> |
| 25 | + <dependency> |
| 26 | + <groupId>org.projectlombok</groupId> |
| 27 | + <artifactId>lombok</artifactId> |
| 28 | + </dependency> |
| 29 | + |
| 30 | + <!-- 配置文件加密 --> |
| 31 | + <dependency> |
| 32 | + <groupId>com.github.ulisesbocchio</groupId> |
| 33 | + <artifactId>jasypt-spring-boot-starter</artifactId> |
| 34 | + <version>2.1.1</version> |
| 35 | + </dependency> |
| 36 | + |
| 37 | + <dependency> |
| 38 | + <groupId>org.springframework.boot</groupId> |
| 39 | + <artifactId>spring-boot-starter-test</artifactId> |
| 40 | + <scope>test</scope> |
| 41 | + </dependency> |
| 42 | +</dependencies> |
| 43 | +``` |
| 44 | +#### 2.5 pom.xml文件中加入maven-springboot打包插件 |
| 45 | +```xml |
| 46 | +<build> |
| 47 | + <plugins> |
| 48 | + <plugin> |
| 49 | + <groupId>org.springframework.boot</groupId> |
| 50 | + <artifactId>spring-boot-maven-plugin</artifactId> |
| 51 | + </plugin> |
| 52 | + </plugins> |
| 53 | +</build> |
| 54 | +``` |
| 55 | +#### 2.6 开发启动类 |
| 56 | +```java |
| 57 | +import org.springframework.boot.SpringApplication; |
| 58 | +import org.springframework.boot.autoconfigure.SpringBootApplication; |
| 59 | + |
| 60 | +@SpringBootApplication |
| 61 | +public class Application { |
| 62 | + |
| 63 | + public static void main(String[] args) { |
| 64 | + SpringApplication.run(Application.class, args); |
| 65 | + } |
| 66 | + |
| 67 | +} |
| 68 | +``` |
| 69 | +#### 2.7 开发测试接口 |
| 70 | +```java |
| 71 | +import org.springframework.beans.factory.annotation.Value; |
| 72 | +import org.springframework.web.bind.annotation.RequestMapping; |
| 73 | +import org.springframework.web.bind.annotation.RestController; |
| 74 | + |
| 75 | +@RestController |
| 76 | +public class TestController { |
| 77 | + |
| 78 | + @Value("${com.name}") |
| 79 | + private String name; |
| 80 | + |
| 81 | + @RequestMapping("/get") |
| 82 | + public String get() { |
| 83 | + return name; |
| 84 | + } |
| 85 | + |
| 86 | +} |
| 87 | +``` |
| 88 | +#### 2.8 编写配置文件 |
| 89 | +```properties |
| 90 | +# 用来加密的salt值 |
| 91 | +jasypt.encryptor.password=5217 |
| 92 | +# 加密的算法 |
| 93 | +jasypt.encryptor.iv-generator-classname=org.jasypt.salt.RandomIVGenerator |
| 94 | +com.name=ENC(kwYxvK1ssjcDMCMqMdRhvQ==) |
| 95 | +``` |
| 96 | +#### 2.9 编译打包运行 |
| 97 | +### 3. 应用场景 |
0 commit comments