单例懒加载的实现方式

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package io.github.lyr2000.dissertation.util;

import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.symmetric.AES;

/**
 * @author lyr
 * @description 加密算法工具类
 * @create 2021-11-09 23:04
 */
public class PwdUtil {
    // private static volatile String privateKey;

    // private static  volatile AES aes;
    private static class InnerPrivateKey {
        
         private static final AES AES = SecureUtil.aes(SecureUtil.aes().encrypt("lyr-2000.github.io"));
    }
    /**
     * https://lyr-2000.github.io/
     * 个人博客地址
     */
    private static AES loadKey() {
        return InnerPrivateKey.AES;
        // if(aes == null) {
        //     synchronized (PwdUtil.class) {
        //         if (aes == null) {
        //             String privateKey = new String( SecureUtil.aes().encrypt("lyr-2000.github.io"));
        //             aes = SecureUtil.aes(privateKey.getBytes());
        //         }
        //     }
        // }
        // return aes;
    }

    public static String encodePassword(String pwd) {
        if (pwd == null) {
            pwd = "";
        }
        return new String(loadKey().encrypt(pwd));

    }
    public static String decodePassword(String pwd) {
        return new String(loadKey().decrypt(pwd));
    }


    public static String encodeToken(String userId) {
        return new String(loadKey().encrypt(userId));
    }
}