76.7. 动态提供密钥
当使用 Recipient 列表或类似的 EIP 时,交换的收件人可能会动态变化。在所有收件人中使用相同密钥可能并不可行。能够在每次交换时动态指定密钥会很有用。然后,在由数据格式处理前,交换可以被动态地利用其目标接收者的密钥进行动态增强。为便于此数据,DataFormat 允许通过下面的消息标题动态提供密钥
- CryptoDataFormat.KEY "CamelCryptoKey"
CryptoDataFormat cryptoFormat = new CryptoDataFormat("DES", null); /** * Note: the header containing the key should be cleared after * marshalling to stop it from leaking by accident and * potentially being compromised. The processor version below is * arguably better as the key is left in the header when you use * the DSL leaks the fact that camel encryption was used. */ from("direct:key-in-header-encrypt") .marshal(cryptoFormat) .removeHeader(CryptoDataFormat.KEY) .to("mock:encrypted"); from("direct:key-in-header-decrypt").unmarshal(cryptoFormat).process(new Processor() { public void process(Exchange exchange) throws Exception { exchange.getIn().getHeaders().remove(CryptoDataFormat.KEY); exchange.getOut().copyFrom(exchange.getIn()); } }).to("mock:unencrypted");
也可借助 spring。
<crypto id="nokey" algorithm="DES" />