개인정보 암호화
우리는 때론 내 사이트의 고객정보를 보호하기위해 노력해야 된다.
이름, 주민번호, 전화번호, 비밀번호, 여권번호 등등 물론 요즘 웬만한 사이트는 비밀번호에 대한 암호화(hash)는 기본적으로 적용되지만 의외로 나머지 항목들에 대한 정보는 관대한 편이다. 그래서 오늘은 이런 민감한 개인정보를 어떻게 암호화 할것인지 대해 처리방법을 논의해 보겠다.
Jasypt
pom.xml 추가
<dependency>
<groupId>com.github.ulisesbocchio</groupId>
<artifactId>jasypt-spring-boot-starter</artifactId>
<version>2.1.0</version>
</dependency>
application.yml
spring:
datasource:
username: root
url: jdbc:mysql://127.0.0.1:3306/testdb?useSSL=false&useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&zeoDateTimeBehavior=convertToNull&serverTimezone=Asia/Seoul
password: ENC(mVTvp4IddqdaYGqPl9lCQbzM3H/b0B6l)
jasypt:
encryptor:
property:
prefix: abc[
suffix: ']'
password: mypassword
java -jar -Djasypt.encryptor.password=1123 springboot-jasypt-2.3.3.RELEASE.jar
아래코 드로도 수행가능하다.
@Autowired
private StringEncryptor stringEncryptor;
public void encrypt(String content) {
String encryptStr = stringEncryptor.encrypt(content);
System.out.println("암호화된 결과:" + encryptStr);
}
@Documented
@Target({ElementType.FIELD,ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
public @interface EncryptField {
String[] value() default "";
}
@Documented
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface EncryptMethod {
String type() default ENCRYPT;
}
@Slf4j
@Aspect
@Component
public class EncryptHandler {
@Autowired
private StringEncryptor stringEncryptor;
@Pointcut("@annotation(com.xiaofu.annotation.EncryptMethod)")
public void pointCut() {
}
@Around("pointCut()")
public Object around(ProceedingJoinPoint joinPoint) {
/**
* 암호화
*/
encrypt(joinPoint);
/**
* 복호화
*/
Object decrypt = decrypt(joinPoint);
return decrypt;
}
public void encrypt(ProceedingJoinPoint joinPoint) {
try {
Object[] objects = joinPoint.getArgs();
if (objects.length != 0) {
for (Object o : objects) {
if (o instanceof String) {
encryptValue(o);
} else {
handler(o, ENCRYPT);
}
//TODO 其余类型自己看实际情况加
}
}
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
public Object decrypt(ProceedingJoinPoint joinPoint) {
Object result = null;
try {
Object obj = joinPoint.proceed();
if (obj != null) {
if (obj instanceof String) {
decryptValue(obj);
} else {
result = handler(obj, DECRYPT);
}
//TODO
}
} catch (Throwable e) {
e.printStackTrace();
}
return result;
}
。。。
}
@EncryptMethod
@PostMapping(value = "test")
@ResponseBody
public Object testEncrypt(@RequestBody UserVo user, @EncryptField String name) {
return insertUser(user, name);
}
private UserVo insertUser(UserVo user, String name) {
System.out.println("암호화 후 데이터 : user" + JSON.toJSONString(user));
return user;
}
@Data
public class UserVo implements Serializable {
private Long userId;
@EncryptField
private String mobile;
@EncryptField
private String address;
private String age;
}
Last updated