使用 WSSEncryption API 对 SOAP 消息进行加密
可通过使用 Web Service 安全性 API (WSS API) 保护 SOAP 消息,而不必对配置使用策略集。要配置客户机以便对生成器端进行请求加密,请使用 WSSEncryption API 对 SOAP 消息进行加密。WSSEncryption API 指定配置客户机时要进行加密的请求 SOAP 消息部件。
开始之前
可以使用 WSS API 或者在管理控制台上使用策略集来启用加密并在 SOAP 消息中添加生成器安全性令牌。要保护 SOAP 消息,请根据需要使用 WSS API 来完成下列加密任务:
- 使用 WSSEncryption API 配置加密并选择加密方法。
- 根据需要使用 WSSEncryptPart API 来配置加密部件。
关于此任务
生成器端的加密信息用于对请求生成者(客户端)绑定的出局 SOAP 消息进行加密。客户机生成器配置必须与提供程序使用者的配置匹配。

机密性设置要求将机密性约束应用于生成的消息。这些约束包括指定生成的消息中必须进行加密的消息部件,以及加密现时标志和时间戳记元素要连接至的消息部件。
可以配置下列加密部件:
加密部件 | 描述 |
---|---|
part | 将 WSSEncryptPart 对象添加为加密部件的目标。 |
keyword | 使用关键字添加加密部件。WebSphere® Application Server 支持以下关键字:
|
xpath | 使用 XPath 表达式添加加密部件。 |
signature | 将 WSSignature 组件添加为加密部件的目标。 |
header | 将由 QName 指定的 SOAP 头添加为加密部件的目标。 |
securityToken | 将 SecurityToken 对象添加为加密部件的目标。 |
为了获取加密信息,将执行特定的缺省行为。使用 WSSEncryption API 的最简单方法是使用缺省行为(请参阅示例代码)。
WSSEncryption 对密钥加密算法、数据加密算法、安全性令牌引用方法以及诸如 SOAP 主体内容和签名之类的加密部件提供了缺省值。缺省加密行为包括:
加密决定 | 缺省行为 |
---|---|
要使用关键字进行加密的 SOAP 消息部件 | 设置可使用关键字来添加的加密部件。缺省加密部件是 BODY_CONTENT 和 SIGNATURE。WebSphere Application Server 支持使用下列关键字:
|
要选择的数据加密方法(算法) | 设置数据加密方法。可以指定数据和密钥加密方法。缺省数据加密算法方法是 AES 128。WebSphere Application Server 支持以下数据加密方法:
|
是否对密钥进行加密 (isEncrypt) | 指定是否对密钥进行加密。值为 true 或 false。缺省值是对密钥进行加密 (true)。 |
要选择的密钥加密方法(算法) | 设置密钥加密方法。可以指定数据和密钥加密方法。缺省密钥加密算法方法是密钥合并 RSA OAEP。WebSphere Application Server 支持以下密钥加密方法:
|
要指定的安全性令牌 (securityToken) | 设置 SecurityToken。缺省安全性令牌类型是 X509Token。WebSphere Application Server 提供以下预配置的使用者令牌类型:
|
要使用的令牌引用 (refType) | 设置安全性令牌引用的类型。缺省令牌引用是 SecurityToken.REF_KEYID。WebSphere Application
Server 支持以下令牌引用类型:
|
是否使用 MTOM (mtomOptimize) | 对加密部件设置消息传输优化机制 (MTOM) 优化。 |
过程
结果
示例
以下示例提供了样本代码,这段代码使用了在 WSSEncryption 中定义的方法:
// Get the message context
Object msgcontext = getMessageContext();
// Generate the WSSFactory instance (step: a)
WSSFactory factory = WSSFactory.getInstance();
// Generate the WSSGenerationContext instance (step: b)
WSSGenerationContext gencont = factory.newWSSGenerationContext();
// Generate the callback handler
X509GenerateCallbackHandler callbackHandler = new
X509GenerateCallbackHandler(
"",
"enc-sender.jceks",
"jceks",
"storepass".toCharArray(),
"bob",
null,
"CN=Bob, O=IBM, C=US",
null);
// Generate the security token used for encryption (step: c)
SecurityToken token = factory.newSecurityToken(X509Token.class , callbackHandler);
// Generate WSSEncryption instance (step: d)
WSSEncryption enc = factory.newWSSEncryption(token);
// Set the part to be encrypted (step: e)
// DEFAULT: WSSEncryption.BODY_CONTENT and WSSEncryption.SIGNATURE
// Set the part specified by the keyword (step: e)
enc.addEncryptPart(WSSEncryption.BODY_CONTENT);
// Set the part in the SOAP Header specified by QName (step: e)
enc.addEncryptHeader(new QName("http://www.w3.org/2005/08/addressing",
"MessageID"));
// Set the part specified by WSSSignature (step: e)
SecurityToken sigToken = getSecurityToken();
WSSSignature sig = factory.newWSSSignature(sigToken);
enc.addEncryptPart(sig);
// Set the part specified by SecurityToken (step: e)
UNTGenerateCallbackHandler untCallbackHandler =
new UNTGenerateCallbackHandler("Chris", "sirhC");
SecurityToken unt = factory.newSecurityToken(UsernameToken.class,
untCallbackHandler);
enc.addEncryptPart(unt, false);
// sSt 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']");
enc.addEncryptPartByXPath(sb.toString());
// Set whether the key is encrypted (step: f)
// DEFAULT: true
enc.encryptKey(true);
// Set the data encryption method (step: g)
// DEFAULT: WSSEncryption.AES128
enc.setEncryptionMethod(WSSEncryption.TRIPLE_DES);
// Set the key encryption method (step: h)
// DEFAULT: WSSEncryption.KW_RSA_OAEP
enc.setEncryptionMethod(WSSEncryption.KW_RSA15);
// Set the token reference (step: i)
// DEFAULT: SecurityToken.REF_KEYID
enc.setTokenReference(SecurityToken.REF_STR);
// Add the WSSEncryption to the WSSGenerationContext (step: j)
gencont.add(enc);
// Process the WS-Security header (step: k)
gencont.process(msgcontext);
下一步做什么
如果先前未指定要选择的加密方法,请使用 WSS API,或者通过管理控制台配置策略集来选择数据和密钥加密算法方法。