使用 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 消息进行加密。客户机生成器配置必须与提供程序使用者的配置匹配。

使用 WSSEncryption API 对出局 SOAP 消息进行加密

机密性设置要求将机密性约束应用于生成的消息。这些约束包括指定生成的消息中必须进行加密的消息部件,以及加密现时标志和时间戳记元素要连接至的消息部件。

可以配置下列加密部件:

表 1. 加密部件. 使用加密部件来启用对消息的加密。
加密部件 描述
part 将 WSSEncryptPart 对象添加为加密部件的目标。
keyword 使用关键字添加加密部件。WebSphere® Application Server 支持以下关键字:
  • BODY_CONTENT
  • SIGNATURE
xpath 使用 XPath 表达式添加加密部件。
signature 将 WSSignature 组件添加为加密部件的目标。
header 将由 QName 指定的 SOAP 头添加为加密部件的目标。
securityToken 将 SecurityToken 对象添加为加密部件的目标。

为了获取加密信息,将执行特定的缺省行为。使用 WSSEncryption API 的最简单方法是使用缺省行为(请参阅示例代码)。

WSSEncryption 对密钥加密算法、数据加密算法、安全性令牌引用方法以及诸如 SOAP 主体内容和签名之类的加密部件提供了缺省值。缺省加密行为包括:

表 2. 加密决定. 使用加密缺省行为来保护消息主体内容和签名。
加密决定 缺省行为
要使用关键字进行加密的 SOAP 消息部件

设置可使用关键字来添加的加密部件。缺省加密部件是 BODY_CONTENT 和 SIGNATURE。WebSphere Application Server 支持使用下列关键字:

  • WSSEncryption.BODY_CONTENT
  • WSSEncryption.SIGNATURE
要选择的数据加密方法(算法)

设置数据加密方法。可以指定数据和密钥加密方法。缺省数据加密算法方法是 AES 128。WebSphere Application Server 支持以下数据加密方法:

  • WSSEncryption.AES128:http://www.w3.org/2001/04/xmlenc#aes128-cbc
  • WSSEncryption.AES192:http://www.w3.org/2001/04/xmlenc#aes192-cbc
  • WSSEncryption.AES256:http://www.w3.org/2001/04/xmlenc#aes256-cbc
  • WSSEncryption.TRIPLE_DES:http://www.w3.org.2001/04/xmlenc#tripledes-cbc
是否对密钥进行加密 (isEncrypt)

指定是否对密钥进行加密。值为 truefalse。缺省值是对密钥进行加密 (true)。

要选择的密钥加密方法(算法)

设置密钥加密方法。可以指定数据和密钥加密方法。缺省密钥加密算法方法是密钥合并 RSA OAEP。WebSphere Application Server 支持以下密钥加密方法:

  • WSSEncryption.KW_AES128:http://www.w3.org/2001/04/xmlenc#kw-aes128
  • WSSEncryption.KW_AES192:http://www.w3.org/2001/04/xmlenc#kw-aes192
  • WSSEncryption.KW_AES256:http://www.w3.org/2001/04/xmlenc#kw-aes256
  • WSSEncryption.KW_RSA_OAEP:http://www.w3.org/2001/04/xmlenc#rsa-oaep-mgf1p
  • WSSEncryption.KW_RSA15:http://www.w3.org/2001/04/xmlenc#rsa-1_5
  • WSSEncryption.KW_TRIPLE_DES:http://www.w3.org/2001/04/xmlenc#kw-tripledes
要指定的安全性令牌 (securityToken)

设置 SecurityToken。缺省安全性令牌类型是 X509Token。WebSphere Application Server 提供以下预配置的使用者令牌类型:

  • 派生密钥令牌
  • X.509 令牌
要使用的令牌引用 (refType) 设置安全性令牌引用的类型。缺省令牌引用是 SecurityToken.REF_KEYID。WebSphere Application Server 支持以下令牌引用类型:
  • SecurityToken.REF_KEYID
  • SecurityToken.REF_STR
  • SecurityToken.REF_EMBEDDED
  • SecurityToken.REF_THUMBPRINT
是否使用 MTOM (mtomOptimize) 对加密部件设置消息传输优化机制 (MTOM) 优化。

过程

  1. 要使用 WSSEncryption API 对 SOAP 消息进行加密,请首先确保安装了应用程序服务器。
  2. 用于加密的 WSS API 过程执行以下过程步骤:
    1. 使用 WSSFactory.getInstance() 获取 WSS API 实现实例。
    2. 根据 WSSFactory 实例创建 WSSGenerationContext 实例。
    3. 根据 WSSFactory 创建用于加密的 SecurityToken。
    4. 根据 WSSFactory 实例使用 SecurityToken 创建 WSSEncryption。 WSSEncryption 的缺省行为是对主体内容和签名进行加密。
    5. 在 WSSEncryption 中添加要进行加密的新部件(如果现有部件不合适)。 调用 addEncryptPart()、addEncryptHeader() 或 addEncryptPartByXPath() 后,将清除缺省部件。
    6. 调用 encryptKey(false)(如果不对密钥进行加密)。
    7. 设置数据加密方法(如果缺省方法不合适)。
    8. 设置密钥加密方法(如果缺省方法不合适)。
    9. 设置令牌引用(如果缺省令牌引用不合适)。
    10. 将 WSSEncryption 添加至 WSSConsumingContext。
    11. 使用 SOAPMessageContext 调用 WSSGenerationContext.process()。

结果

如果在加密过程中存在错误情况,那么将提供 WSSException。如果成功,此 API 将调用 WSSGenerationContext.process(),生成 WS-Security 头,然后使用 Web Service 安全性来保护 SOAP 消息。

示例

以下示例提供了样本代码,这段代码使用了在 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);
注: 由于使用公用密钥进行加密,因此 X509GenerationCallbackHandler 不需要密钥密码。从 Java™ 密钥库获取公用密钥不需要密码。

下一步做什么

如果先前未指定要选择的加密方法,请使用 WSS API,或者通过管理控制台配置策略集来选择数据和密钥加密算法方法。


指示主题类型的图标 任务主题



时间戳记图标 最近一次更新时间: last_date
http://www14.software.ibm.com/webapp/wsbroker/redirect?version=cord&product=was-nd-mp&topic=twbs_confwssencryption
文件名:twbs_confwssencryption.html