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