在项目开发中,对于一些敏感属性,可能需要进行加密处理。比如下面的对象中的name若需要加密。可在name上加上@JsonAdapter注解。

/**
 * JsonAdapter
 *   用来自定义序列化 和 反序列化
 *
 * 这种形式比较单一 ,
 *   序列化 : 放置在 该类的属性上
 *    反序列化时: 实现 JsonDeserializer<该类名>
 *
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class JsonAdapterGson {

    @JsonAdapter(EncryptSerializer.class)
    private String name;
    private String email;
    private String mobile;

}

序列化时需要实现JsonSerializer<T> 接口。操作的关键在于在序列化时。进行加密等需要的操作。

public class EncryptSerializer implements JsonSerializer<String> {
    /**
     *
     * @param src 所需要序列化的值
     * @param typeOfSrc 序列化的类型
     * @param context 序列化的操作
     * @return
     */
    @Override
    public JsonElement serialize(String src, Type typeOfSrc, JsonSerializationContext context) {
        JsonElement jsonElement = null;
        try {
           jsonElement =   context.serialize(DesUtil.encrypt(src,"wow!@#$%"));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return jsonElement;
    }
}

进行加解密DestUtil的代码如下

public class DesUtil {

    private final static String DES = "DES";

    public static void main(String[] args) throws Exception {
        String data = "123 456a";
        String key = "wow!@#$%";
        System.err.println(encrypt(data, key));
        System.err.println(decrypt(encrypt(data, key), key));

    }

    /**
     * Description 根据键值进行加密
     * @param data
     * @param key  加密键byte数组
     * @return
     * @throws Exception
     */
    public static String encrypt(String data, String key) throws Exception {
        byte[] bt = encrypt(data.getBytes(), key.getBytes());
        String strs = new BASE64Encoder().encode(bt);
        return strs;
    }

    /**
     * Description 根据键值进行解密
     * @param data
     * @param key  加密键byte数组
     * @return
     * @throws IOException
     * @throws Exception
     */
    public static String decrypt(String data, String key) throws IOException,
            Exception {
        if (data == null)
            return null;
        BASE64Decoder decoder = new BASE64Decoder();
        byte[] buf = decoder.decodeBuffer(data);
        byte[] bt = decrypt(buf,key.getBytes());
        return new String(bt);
    }

    /**
     * Description 根据键值进行加密
     * @param data
     * @param key  加密键byte数组
     * @return
     * @throws Exception
     */
    private static byte[] encrypt(byte[] data, byte[] key) throws Exception {
        // 生成一个可信任的随机数源
        SecureRandom sr = new SecureRandom();

        // 从原始密钥数据创建DESKeySpec对象
        DESKeySpec dks = new DESKeySpec(key);

        // 创建一个密钥工厂,然后用它把DESKeySpec转换成SecretKey对象
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);
        SecretKey securekey = keyFactory.generateSecret(dks);

        // Cipher对象实际完成加密操作
        Cipher cipher = Cipher.getInstance(DES);

        // 用密钥初始化Cipher对象
        cipher.init(Cipher.ENCRYPT_MODE, securekey, sr);

        return cipher.doFinal(data);
    }


    /**
     * Description 根据键值进行解密
     * @param data
     * @param key  加密键byte数组
     * @return
     * @throws Exception
     */
    private static byte[] decrypt(byte[] data, byte[] key) throws Exception {
        // 生成一个可信任的随机数源
        SecureRandom sr = new SecureRandom();

        // 从原始密钥数据创建DESKeySpec对象
        DESKeySpec dks = new DESKeySpec(key);

        // 创建一个密钥工厂,然后用它把DESKeySpec转换成SecretKey对象
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);
        SecretKey securekey = keyFactory.generateSecret(dks);

        // Cipher对象实际完成解密操作
        Cipher cipher = Cipher.getInstance(DES);

        // 用密钥初始化Cipher对象
        cipher.init(Cipher.DECRYPT_MODE, securekey, sr);

        return cipher.doFinal(data);
    }

测试方法如下

    @Test
    public void testJsonAdapterGsonSerializer(){
         Gson gson = new Gson();
        JsonAdapterGson g = new JsonAdapterGson("reed","reed@ustc.com","110");
        String json = gson.toJson(g);
        System.out.println(json);
}

测试结果如下。由结果可知。实现了加密