Skip to content

Commit b0e44cb

Browse files
committed
springboot_jasypt
1 parent c7d28f6 commit b0e44cb

6 files changed

Lines changed: 203 additions & 0 deletions

File tree

springboot_jasypt/pom.xml

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>springboot_chowder</artifactId>
7+
<groupId>com.oven</groupId>
8+
<version>1.0-SNAPSHOT</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
12+
<artifactId>springboot_jasypt</artifactId>
13+
14+
<dependencies>
15+
<!-- web启动器 -->
16+
<dependency>
17+
<groupId>org.springframework.boot</groupId>
18+
<artifactId>spring-boot-starter-web</artifactId>
19+
</dependency>
20+
21+
<!-- lombok支持 -->
22+
<dependency>
23+
<groupId>org.projectlombok</groupId>
24+
<artifactId>lombok</artifactId>
25+
</dependency>
26+
27+
<!-- 配置文件加密 -->
28+
<dependency>
29+
<groupId>com.github.ulisesbocchio</groupId>
30+
<artifactId>jasypt-spring-boot-starter</artifactId>
31+
<version>2.1.1</version>
32+
</dependency>
33+
34+
<dependency>
35+
<groupId>org.springframework.boot</groupId>
36+
<artifactId>spring-boot-starter-test</artifactId>
37+
<scope>test</scope>
38+
</dependency>
39+
</dependencies>
40+
41+
</project>

springboot_jasypt/readme.md

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
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. 应用场景
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.oven;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
public class Application {
8+
9+
public static void main(String[] args) {
10+
SpringApplication.run(Application.class, args);
11+
}
12+
13+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.oven.controller;
2+
3+
import org.springframework.beans.factory.annotation.Value;
4+
import org.springframework.web.bind.annotation.RequestMapping;
5+
import org.springframework.web.bind.annotation.RestController;
6+
7+
@RestController
8+
public class TestController {
9+
10+
@Value("${com.name}")
11+
private String name;
12+
13+
@RequestMapping("/get")
14+
public String get() {
15+
return name;
16+
}
17+
18+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# 用来加密的salt值
2+
jasypt.encryptor.password=5217
3+
# 加密的算法
4+
jasypt.encryptor.iv-generator-classname=org.jasypt.salt.RandomIVGenerator
5+
com.name=ENC(kwYxvK1ssjcDMCMqMdRhvQ==)
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.oven;
2+
3+
import org.jasypt.encryption.StringEncryptor;
4+
import org.junit.Test;
5+
import org.junit.runner.RunWith;
6+
import org.springframework.beans.factory.annotation.Autowired;
7+
import org.springframework.boot.test.context.SpringBootTest;
8+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
9+
10+
@SpringBootTest
11+
@RunWith(SpringJUnit4ClassRunner.class)
12+
public class JasyptTest {
13+
14+
@Autowired
15+
StringEncryptor encryptor;
16+
17+
@Test
18+
public void getPass() {
19+
String name = encryptor.encrypt("5217");
20+
System.out.println("加密结果:" + name);
21+
}
22+
23+
@Test
24+
public void passDecrypt() {
25+
String username = encryptor.decrypt("lFuHUaMta6RjpJtBKRKBcg==");
26+
System.out.println("解密结果:" + username);
27+
}
28+
29+
}

0 commit comments

Comments
 (0)