我喜欢将字符串的java映射编码为单个基本64位编码字符串.编码的字符串将被传输到远程端点,并且可能由不好的人操纵.因此,应该发生的最糟糕的事情是invaild key,value-tuples,但不应该带来任何其他安全风险.
例:
Map<String,String> map = ...
String encoded = Base64.encode(map);
// somewhere else
Map<String,String> map = Base64.decode(encoded);
Run Code Online (Sandbox Code Playgroud)
是的,必须是Base64.不喜欢那个 或那个 或任何其他这些.是否有现有的轻量级解决方案(Single Utils-Class首选)?或者我必须创建自己的?
有什么比这更好的?
// marshalling
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(map);
oos.close();
String encoded = new String(Base64.encodeBase64(baos.toByteArray()));
// unmarshalling
byte[] decoded = Base64.decodeBase64(encoded.getBytes());
ByteArrayInputStream bais = new ByteArrayInputStream(decoded);
ObjectInputStream ois = new ObjectInputStream(bais);
map = (Map<String,String>) ois.readObject();
ois.close();
Run Code Online (Sandbox Code Playgroud)
谢谢,