Skip to content

Commit bf38067

Browse files
author
zhifu.xin
committed
自定义配置文件脱敏工具
1 parent e5420fe commit bf38067

File tree

6 files changed

+115
-33
lines changed

6 files changed

+115
-33
lines changed
Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
11
package com.xiaofu;
22

3+
import com.xiaofu.process.MyPropertiesBeanFactoryPostProcessor;
4+
import com.xiaofu.process.conf;
35
import org.springframework.boot.SpringApplication;
46
import org.springframework.boot.autoconfigure.SpringBootApplication;
7+
import org.springframework.context.ApplicationContext;
8+
import org.springframework.context.ConfigurableApplicationContext;
59
import org.springframework.context.annotation.ComponentScan;
10+
import org.springframework.context.annotation.Import;
611

12+
@Import({MyPropertiesBeanFactoryPostProcessor.class, conf.class})
713
@ComponentScan("com.xiaofu")
814
@SpringBootApplication
915
public class JasyptApplication {
1016

11-
public static void main(String[] args) {
12-
SpringApplication.run(JasyptApplication.class, args);
13-
}
17+
public static void main(String[] args) {
18+
ConfigurableApplicationContext run = SpringApplication.run(JasyptApplication.class, args);
19+
}
1420
}

springboot-jasypt/src/main/java/com/xiaofu/controller/Encryptor.java

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,12 @@
33
import com.alibaba.fastjson.JSON;
44
import com.xiaofu.annotation.EncryptField;
55
import com.xiaofu.annotation.EncryptMethod;
6-
import lombok.Data;
6+
import com.xiaofu.model.UserVo;
77
import lombok.extern.slf4j.Slf4j;
88
import org.jasypt.encryption.StringEncryptor;
99
import org.springframework.beans.factory.annotation.Autowired;
1010
import org.springframework.web.bind.annotation.*;
1111

12-
import java.io.Serializable;
13-
1412
@Slf4j
1513
@RestController
1614
@RequestMapping("/encryptor/")
@@ -36,18 +34,4 @@ private UserVo insertUser(UserVo user, String name) {
3634
System.out.println("加密后的数据:user" + JSON.toJSONString(user));
3735
return user;
3836
}
39-
40-
@Data
41-
public class UserVo implements Serializable {
42-
43-
private Long userId;
44-
45-
@EncryptField
46-
private String mobile;
47-
48-
@EncryptField
49-
private String address;
50-
51-
private String age;
52-
}
5337
}

springboot-jasypt/src/main/java/com/xiaofu/model/UserVo.java

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,16 @@
1010
* @Date: 2021/7/26 15:10
1111
* @Description:
1212
*/
13-
//@Data
14-
//public class UserVo implements Serializable {
15-
//
16-
// private Long userId;
17-
//
18-
// @EncryptField
19-
// private String mobile;
20-
//
21-
// @EncryptField
22-
// private String address;
23-
//
24-
// private String age;
25-
//}
13+
@Data
14+
public class UserVo implements Serializable {
15+
16+
private Long userId;
17+
18+
@EncryptField
19+
private String mobile;
20+
21+
@EncryptField
22+
private String address;
23+
24+
private String age;
25+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.xiaofu.process;
2+
3+
import com.ulisesbocchio.jasyptspringboot.EncryptablePropertySource;
4+
import org.springframework.beans.factory.annotation.Autowired;
5+
import org.springframework.core.env.ConfigurableEnvironment;
6+
import org.springframework.core.env.PropertySource;
7+
import org.springframework.core.env.StandardEnvironment;
8+
import org.springframework.stereotype.Component;
9+
10+
import java.util.Optional;
11+
12+
/**
13+
* @Auther: [email protected]
14+
* @Date: 2021/7/30 18:35
15+
* @Description:
16+
*/
17+
public class EnvCopy1 {
18+
19+
StandardEnvironment copy1;
20+
21+
public EnvCopy1(final ConfigurableEnvironment environment) {
22+
copy1 = new StandardEnvironment();
23+
Optional.ofNullable(environment.getPropertySources()).ifPresent(sources -> sources.forEach(ps -> {
24+
final PropertySource<?> original = ps instanceof EncryptablePropertySource
25+
? ((EncryptablePropertySource) ps).getDelegate()
26+
: ps;
27+
copy1.getPropertySources().addLast(original);
28+
}));
29+
}
30+
31+
public ConfigurableEnvironment get() {
32+
return copy1;
33+
}
34+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.xiaofu.process;
2+
3+
import lombok.NoArgsConstructor;
4+
import org.springframework.beans.BeansException;
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
7+
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
8+
import org.springframework.context.annotation.Import;
9+
import org.springframework.core.Ordered;
10+
import org.springframework.core.env.*;
11+
12+
import java.util.Iterator;
13+
14+
@NoArgsConstructor
15+
public class MyPropertiesBeanFactoryPostProcessor implements BeanFactoryPostProcessor, Ordered {
16+
17+
EnvCopy1 envCopy1 ;
18+
19+
@Override
20+
public void postProcessBeanFactory(ConfigurableListableBeanFactory configurableListableBeanFactory) throws BeansException {
21+
MutablePropertySources propertySources = envCopy1.get().getPropertySources();
22+
Iterator<PropertySource<?>> iterator = propertySources.iterator();
23+
while (iterator.hasNext()) {
24+
PropertySource<?> propertySource = iterator.next();
25+
26+
if (propertySource instanceof PropertiesPropertySource) {
27+
System.out.println(propertySource.getProperty("propertyKey"));
28+
// 用propertySource.getSource() 可以获取全部配置
29+
}
30+
}
31+
}
32+
33+
@Override
34+
public int getOrder() {
35+
return 0;
36+
}
37+
38+
39+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.xiaofu.process;
2+
3+
import org.springframework.context.annotation.Bean;
4+
import org.springframework.context.annotation.Configuration;
5+
import org.springframework.core.env.ConfigurableEnvironment;
6+
7+
/**
8+
* @Auther: [email protected]
9+
* @Date: 2021/7/30 18:41
10+
* @Description:
11+
*/
12+
@Configuration
13+
public class conf {
14+
15+
@Bean
16+
public EnvCopy1 envCopy1(final ConfigurableEnvironment environment) {
17+
return new EnvCopy1(environment);
18+
}
19+
}

0 commit comments

Comments
 (0)