使用 WSSDecryption API 对 SOAP 消息进行解密
可通过使用 Web Service 安全性 API (WSS API) 保护 SOAP 消息,而不必对配置使用策略集。要配置客户机以便在响应(客户机)使用者端进行解密,请使用 WSSDecryption API 对 SOAP 消息进行解密。WSSDecryption API 指定配置客户机时要进行解密的请求 SOAP 消息部件。
开始之前
可以使用 WSS API 或者在管理控制台上使用策略集来启用解密并在 SOAP 消息中添加使用者安全性令牌。要保护 SOAP 消息,必须完成了下列解密任务:
- 对 SOAP 消息进行加密。
- 选择解密方法。
关于此任务
使用者端的解密信息用于对响应使用者(客户端)绑定的入局 SOAP 消息进行解密。客户机使用者配置必须与提供程序生成器的配置匹配。
机密性设置要求将机密性约束应用于生成的消息。
可以配置下列解密部件:
解密部件 | 描述 |
---|---|
part | 将 WSSDecryptPart 对象添加为解密部件的目标。 |
keyword | 使用关键字添加解密部件。WebSphere® Application Server 支持以下关键字:
|
xpath | 使用 XPath 表达式添加解密部件。 |
verification | 将 WSSVerification 实例添加为解密部件的目标。 |
header | 将由 QName 指定的 SOAP 头添加为解密部件的目标。 |
为了获取解密信息,将执行特定的缺省行为。使用 WSS API 进行解密的最简单方法是使用缺省行为(请参阅示例代码)。WSSDecryption 对密钥加密算法、数据加密算法以及诸如 SOAP 主体内容和签名之类的解密部件提供了缺省值。缺省解密行为包括:
解密决定 | 缺省行为 |
---|---|
要进行解密的部件 | 缺省解密部件是 BODY_CONTENT 和 SIGNATURE。WebSphere Application Server 支持使用下列关键字:
指定要进行解密的消息部件后,必须指定对使用者请求消息进行解密时使用的方法。例如,如果对签名和主体内容进行了加密,那么要进行解密的 SOAP 消息部件包括相同的部件。 |
是否对密钥进行加密 (isEncrypt) | 缺省值是对密钥进行加密 (true)。 |
要选择的数据解密算法(方法) | 缺省数据解密算法方法是 AES128。WebSphere Application Server 支持以下数据加密方法:
|
要选择的密钥解密方法(算法) | 缺省密钥解密算法方法是密钥合并 RSA OAEP。WebSphere Application Server 支持以下密钥加密方法:
|
要指定的安全性令牌 | 缺省安全性令牌类型是 X509 令牌。WebSphere Application Server 提供以下预配置的使用者令牌类型:
|
过程
结果
示例
以下示例提供了样本代码,用于对 SOAP 消息主体内容进行解密:
// Get the message context
Object msgcontext = getMessageContext();
// Generate the WSSFactory instance (step: a)
WSSFactory factory = WSSFactory.getInstance();
// Generate the WSSConsumingContext instance (step: b)
WSSConsumingContext gencont = factory.newWSSConsumingContext();
// Generate the callback handler (step: c)
X509ConsumeCallbackHandler callbackHandler = new
X509ConsumeCallbackHandler(
"",
"enc-sender.jceks",
"jceks",
"storepass".toCharArray(),
"alice",
"keypass".toCharArray(),
"CN=Alice, O=IBM, C=US");
// Generate the WSSDecryption instance (step: d)
WSSDecryption dec = factory.newWSSDecryption(X509Token.class,
callbackHandler);
// Set the part to be encrypted (step: e)
// DEFAULT: WSSEncryption.BODY_CONTENT and WSSEncryption.SIGNATURE
// Set the part to be encrypted (step: e)
// DEFAULT: WSSEncryption.BODY_CONTENT and WSSEncryption.SIGNATURE
// Set the part specified by the keyword (step: e)
dec.addRequiredDecryptPart(WSSDecryption.BODY_CONTENT);
// Set the part in the SOAP Header specified by QName (step: e)
dec.addRequiredDecryptHeader(new
QName("http://www.w3.org/2005/08/addressing",
"MessageID"));
// Set the part specified by WSSVerification (step: e)
X509ConsumeCallbackHandler verifyCallbackHandler =
getCallbackHandler();
WSSVerification ver = factory.newWSSVerification(X509Token.class,
verifyCallbackHandler);
dec.addRequiredDecryptPart(ver);
// Set the part specified by XPath expression (step: e)
StringBuffer sb = new StringBuffer();
sb.append("/*[namespace-uri()='http://schemas.xmlsoap.org/soap/envelope/'
and local-name()='Envelope']");
sb.append("/*[namespace-uri()='http://schemas.xmlsoap.org/soap/envelope/'
and local-name()='Body']");
sb.append("/*[namespace-uri()='http://xmlsoap.org/Ping'
and local-name()='Ping']");
sb.append("/*[namespace-uri()='http://xmlsoap.org/Ping'
and local-name()='Text']");
dec.addRequiredDecryptPartByXPath(sb.toString());
// Set the part in the SOAP header to be decrypted specified by QName (step: e)
dec.addRequiredDecryptHeader(new
QName("http://www.w3.org/2005/08/addressing",
"MessageID"));
// Set the candidates for the data encryption method (step: f)
// DEFAULT : WSSDecryption.AES128
dec.addAllowedEncryptionMethod(WSSDecryption.AES128);
dec.addAllowedEncryptionMethod(WSSDecryption.AES192);
// Set the candidates for the key encryption method (step: g)
// DEFAULT : WSSDecryption.KW_RSA_OAEP
dec.addAllowedKeyEncryptionMethod(WSSDecryption.KW_TRIPLE_DES);
// Set the candidate security token to used for the decryption (step: h)
X509ConsumeCallbackHandler callbackHandler2 = getCallbackHandler2();
dec.addToken(X509Token.class, callbackHandler2);
// Set whether or not the key should be encrypted in the incoming SOAP message (step: i)
// DEFAULT: true
dec.encryptKey(true);
// Add the WSSDecryption to the WSSConsumingContext (step: j)
concont.add(dec);
// Validate the WS-Security header (step: k)
concont.process(msgcontext);
下一步做什么
接下来,使用 WSSDecryptPart API,或者通过管理控制台配置策略集对使用者消息添加解密部件。