Возможно ли создать объект, настроенный таким образом, чтобы он шифровал все поля, кроме полей, аннотированных с определJAVA

Программисты JAVA общаются здесь
Anonymous
Возможно ли создать объект, настроенный таким образом, чтобы он шифровал все поля, кроме полей, аннотированных с определ

Сообщение Anonymous »

Допустим, у вас есть аннотация, настроенная на основе поля: < /p>

Код: Выделить всё

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface DoNotEncrypt {
}
< /code>
ОК, так что теперь у вас есть аннотация. Таким образом, вы можете аннотировать свои поля, когда вы не хотите их шифровать: < /p>
static class CoolStuff {
private String shouldBeEncrypted;
@DoNotEncrypt
private String notEncrypted = "should not be encrypted";

public String getShouldBeEncrypted() {
return shouldBeEncrypted;
}

public void setShouldBeEncrypted(String shouldBeEncrypted) {
this.shouldBeEncrypted = shouldBeEncrypted;
}

public String getNotEncrypted() {
return notEncrypted;
}

public void setNotEncrypted(String notEncrypted) {
this.notEncrypted = notEncrypted;
}
}
< /code>
Я хочу реализовать этот сериализатор: < /p>
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.module.SimpleModule;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* Encrypt every string except fields that have annotation {@link DoNotEncrypt}
*/
public class ConfidentialSerializer extends JsonSerializer {
private static final Logger LOG = LoggerFactory.getLogger(ConfidentialSerializer.class);
@Override
public void serialize(Object value, JsonGenerator gen, SerializerProvider serializers) {
// I want to implement this here
}
public static ObjectMapper configurationObjectMapper() {
ObjectMapper objectMapper = new ObjectMapper();
SimpleModule module = new SimpleModule();
// is this right?
module.addSerializer(Object.class, new ConfidentialSerializer());
objectMapper.registerModule(module);
return objectMapper;
}
}
Чтобы проверить изменение, которое я хочу видеть, что строки зашифрованы, если не аннотация @donotencrypt .
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.Test;

public class ConfidentialSerializerTest {
static class CoolStuff {
private String shouldBeEncrypted;
@DoNotEncrypt
private String notEncrypted;

public String getShouldBeEncrypted() {
return shouldBeEncrypted;
}

public void setShouldBeEncrypted(String shouldBeEncrypted) {
this.shouldBeEncrypted = shouldBeEncrypted;
}

public String getNotEncrypted() {
return notEncrypted;
}

public void setNotEncrypted(String notEncrypted) {
this.notEncrypted = notEncrypted;
}
}
@Test
public void testConfigurationObjectMapper() throws JsonProcessingException {
ObjectMapper objectMapper = ConfidentialSerializer.configurationObjectMapper();
CoolStuff coolStuff = new CoolStuff();
coolStuff = new CoolStuff();
coolStuff.setShouldBeEncrypted("is");
coolStuff.setNotEncrypted("is not");
String out = objectMapper.writeValueAsString(coolStuff);
Map mapOut = objectMapper.readValue(out, Map.class);
Assert.assertNotEquals("is", mapOut.get("notEncrypted"));
Assert.assertEquals("is not", mapOut.get("shouldBeEncrypted")); }
}
< /code>
Это то, что возможно с Mapper Object? Если так, как вы это делаете?

Подробнее здесь: https://stackoverflow.com/questions/795 ... ypts-all-f

Вернуться в «JAVA»