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" />