de.idealo.kafka.deckard.encryption.EncryptingSerializer Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of deckard Show documentation
Show all versions of deckard Show documentation
A declarative approach to defining Spring Kafka producers.
package de.idealo.kafka.deckard.encryption;
import org.apache.kafka.common.serialization.Serializer;
import org.springframework.security.crypto.encrypt.BytesEncryptor;
import org.springframework.security.crypto.encrypt.Encryptors;
import java.util.Map;
import java.util.Objects;
public class EncryptingSerializer implements Serializer {
private final Serializer embeddedSerializer;
private final BytesEncryptor bytesEncryptor;
public EncryptingSerializer(String pass, String salt, Serializer embeddedSerializer) {
this.embeddedSerializer = embeddedSerializer;
this.bytesEncryptor = Encryptors.stronger(pass, salt);
}
@Override
public void configure(Map map, boolean b) {
embeddedSerializer.configure(map, b);
}
@Override
public byte[] serialize(String topic, T data) {
return Objects.nonNull(data) ? this.bytesEncryptor.encrypt(embeddedSerializer.serialize(topic, data)) : null;
}
@Override
public void close() {
embeddedSerializer.close();
}
}