Java tutorial
/** * Copyright(c)2016 IntelCorporation * * LicensedundertheApacheLicense,Version2.0(the"License"); * youmaynotusethisfileexceptincompliancewiththeLicense. * YoumayobtainacopyoftheLicenseat * * http://www.apache.org/licenses/LICENSE-2.0 * * Unlessrequiredbyapplicablelaworagreedtoinwriting,software * distributedundertheLicenseisdistributedonan"ASIS"BASIS, * WITHOUTWARRANTIESORCONDITIONSOFANYKIND,eitherexpressorimplied. * SeetheLicenseforthespecificlanguagegoverningpermissionsand * limitationsundertheLicense. */ package org.trustedanalytics.user.secure.serializer; import org.codehaus.jackson.map.ObjectMapper; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.serializer.JacksonJsonRedisSerializer; import org.springframework.data.redis.serializer.SerializationException; import org.trustedanalytics.user.secure.EncryptionException; import org.trustedanalytics.user.secure.EncryptionService; import org.trustedanalytics.user.secure.SecureJson; import java.io.IOException; public class SecureJacksonJsonRedisSerializer<T> extends JacksonJsonRedisSerializer<T> { private EncryptionService encryptionService; private ObjectMapper objectMapper = new ObjectMapper(); @Autowired public SecureJacksonJsonRedisSerializer(Class<T> type, EncryptionService encryptionService) { super(type); this.encryptionService = encryptionService; } @Override public T deserialize(byte[] bytes) throws SerializationException { if (bytes == null || bytes.length == 0) { return null; } try { SecureJson sj = this.objectMapper.readValue(bytes, 0, bytes.length, SecureJson.class); byte[] plainJson = encryptionService.decrypt(sj); return super.deserialize(plainJson); } catch (IOException e) { throw new SerializationException("Could not read Secure-JSON", e); } catch (EncryptionException e) { throw new SerializationException("Could not decrypt Secure-JSON", e); } } @Override public byte[] serialize(Object t) throws SerializationException { if (t == null) { return new byte[0]; } try { byte[] plainJson = super.serialize(t); SecureJson sj = encryptionService.encrypt(plainJson); return this.objectMapper.writeValueAsBytes(sj); } catch (IOException e) { throw new SerializationException("Could not write Secure-JSON", e); } catch (EncryptionException e) { throw new SerializationException("Could not encrypt Secure-JSON", e); } } }