使用 WSS API 发送自发出的 SAML holder-of-key 令牌和对称密钥

您可以创建具有 holder-of-key 主体集确认方法的自签发 SAML 令牌,随后使用 Java™ API for XML-Based Web Services (JAX-WS) 编程模型和 Web Service 安全 API (WSS API) 将这些令牌随 Web service 请求消息一起发送。

开始之前

本任务假设您熟悉 JAX-WS 编程模型、WSS API 接口、SAML 概念以及策略集的使用以配置和管理 Web service 设置。 请在开始本任务前,完成下列操作:
  • 阅读关于使用 WSS API 发送自签发 SAML 不记名令牌的信息。
  • 阅读关于通过将 WSS API 用于消息级别保护来发送自签发 SAML sender-vouches 令牌的信息。

关于此任务

此任务关注使用嵌入在 SAML 安全性令牌中的对称密钥来生成所选 SOAP 消息元素的数字签名,以满足 holder-of-key 主体集确认方法安全性需求。附加到 Web service 提供程序的 Web Service 安全策略是 WebSphere® Application Server 7.0.0.7 和稍后发行版中随附的 SAML20 HoK Symmetric WSSecurity default 策略集的策略。

过程

  1. 创建包含 holder-of-key 主体集确认方法的 SAML 安全性令牌;例如:
    WSSFactory factory = WSSFactory.getInstance();
    // Initialize WSSGenerationContext
    com.ibm.websphere.wssecurity.wssapi.WSSGenerationContext gencont = factory.newWSSGenerationContext();
    // Initialize SAML issuer configuration via custom properties
    HashMap<Object, Object> customProps = new HashMap<Object,Object>();
    
    customProps.put(SamlConstants.ISSUER_URI_PROP, "example.com");
    customProps.put(SamlConstants.TTL_PROP, "3600000");
    customProps.put(SamlConstants.KS_PATH_PROP, "keystores/saml-provider.jceks");
    customProps.put(SamlConstants.KS_TYPE_PROP, "JCEKS");
    customProps.put(SamlConstants.KS_PW_PROP, "{xor}LCswLTovPiws");
    customProps.put(SamlConstants.KEY_ALIAS_PROP, "samlissuer");
    customProps.put(SamlConstants.KEY_NAME_PROP, "CN=SAMLIssuer, O=EXAMPLE");
    customProps.put(SamlConstants.KEY_PW_PROP, "{xor}NDomLz4sLA==");
    customProps.put(SamlConstants.TS_PATH_PROP, "keystores/saml-provider.jceks");
    customProps.put(SamlConstants.TS_TYPE_PROP, "JCEKS");
    customProps.put(SamlConstants.TS_PW_PROP, "{xor}LCswLTovPiws"); 
    gencont.add(customProps); //Add custom properties
    HashMap<Object, Object> map = new HashMap<Object, Object>();
    map.put(SamlConstants.CONFIRMATION_METHOD, "holder-of-key");
    map.put(SamlConstants.Token_REQUEST, "issue");
    map.put(SamlConstants.TOKEN_TYPE, WSSConstants.SAML.SAML20_VALUE_TYPE);
    map.put(SamlConstants.SAML_NAME_IDENTIFIER, "Alice");
    map.put(SamlConstants.SIGNATURE_REQUIRED, "true");
    map.put(SamlConstants.SERVICE_ALIAS, "soaprecipient");
    map.put(SamlConstants.KEY_TYPE, 
            "http://docs.oasis-open.org/ws-sx/ws-trust/200512/SymmetricKey");
    map.put(SamlConstants.SAML_APPLIES_TO, "http://localhost:9080/your_Web_service");
    map.put(RequesterConfiguration.RSTT.ENCRYPTIONALGORITHM,
            "http://www.w3.org/2001/04/xmlenc#aes256-cbc");
    map.put(SamlConstants.KEY_SIZE, "256");
    SAMLGenerateCallbackHandler callbackHandler = new 
            SAMLGenerateCallbackHandler(map); 
    SAMLToken samlToken = (SAMLToken) factory.newSecurityToken(SAMLToken.class,
            callbackHandler, "system.wss.generate.saml");

    为目标 Web service 加密 SAML 安全性令牌中的嵌入式证明密钥。加密证明密钥的目标服务的公用密钥是由 SamlConstants.SERVICE_ALIAS 属性指定,该属性指定信任文件中的公用证书。信任文件位置是由 com.ibm.websphere.wssecurity.wssapi.WSSGenerationContext 定制属性指定的。在此示例中,您必须导入 Java 密码术扩展 (JCE) 策略文件,因为加密使用 256 位密钥大小。有关更多信息,请阅读“调节 Web Service 安全应用程序”主题中关于使用未受限 JCE 策略文件的信息。

    如果您首选将派生密钥用于数字签名和加密而不是直接使用对称密钥,请添加以下名称/值对:

    map.put(SamlConstants.REQUIRE_DKT, "true");
  2. 使用 WSSGenerationContext 对象来准备请求消息安全性头处理;例如:
    gencon.add(samlToken); //this line of code can be omitted
    
    WSSTimestamp timestamp = factory.newWSSTimestamp();
    gencon.add(timestamp);
    
    WSSSignature sig = factory.newWSSSignature(samlToken);
    
    sig.setSignatureMethod(WSSSignature.HMAC_SHA1);
    sig.setCanonicalizationMethod(WSSSignature.EXC_C14N);
    sig.addSignPart(WSSSignature.BODY);
    sig.addSignPart(WSSSignature.TIMESTAMP);
    sig.addSignPart(WSSSignature.ADDRESSING_HEADERS);
    sig.setTokenReference(SecurityToken.REF_KEYID);
    //If the gencon.add(samlToken); line of code is omitted, or DerivedKey is used
    //the above line of code must be replaced with
    //sig.setTokenReference(SecurityToken.REF_STR);
    
    gencon.add(sig);
    
    WSSEncryption enc = factory.newWSSEncryption(samlToken); 
    
    enc.setEncryptionMethod(WSSEncryption.AES256);
    enc.setTokenReference(SecurityToken.REF_KEYID);
    //If the gencon.add(samlToken); line of code is omitted, or DerivedKey is used
    //the above line of code must be replaced with
    //enc.setTokenReference(SecurityToken.REF_STR);
    enc.encryptKey(false);
    enc.addEncryptPart(WSSEncryption.BODY_CONTENT);
    enc.addEncryptPart(WSSEncryption.SIGNATURE);
    gencon.add(enc);
  3. 创建 WSSConsumingContext 对象来准备响应消息,安全性头处理;例如:
    WSSConsumingContext concont = factory.newWSSConsumingContext();
    
    HashMap<Object, Object> map = new HashMap<Object, Object>();
    
    SAMLConsumerCallbackHandler callbackHandler = new
            SAMLConsumerCallbackHandler(map); 
    
    WSSDecryption dec = factory.newWSSDecryption(SAMLToken.class, callbackHandler,
            "system.wss.consume.saml");
    dec.addAllowedEncryptionMethod(WSSDecryption.AES256);
    dec.encryptKey(false);
    dec.addRequiredDecryptPart(WSSDecryption.BODY_CONTENT); 
    
    concont.add(dec);
    
    callbackHandler = new SAMLConsumerCallbackHandler(map);
    WSSVerification ver = factory.newWSSVerification(SAMLToken.class, callbackHandler,
            "system.wss.consume.saml");
    ver.addAllowedSignatureMethod(WSSVerification.HMAC_SHA1);
    ver.addRequiredVerifyPart(WSSVerification.BODY);
    ver.addRequiredVerifyPart(WSSVerification.TIMESTAMP);
    
    concont.add(ver);
  4. 使用 JDK keytool 实用程序来生成 saml-provider.jceksrecipient.jceks 文件,这些文件用于测试示例代码;例如:
    keytool -genkey -alias samlissuer -keystore saml-provider.jceks -dname "CN=SAMLIssuer, O=ACME" -storepass issuerstorepass
     -keypass issuerkeypass -storetype jceks -validity 5000 -keyalg RSA -keysize 2048
    
    keytool -genkey -alias soaprecipient -keystore recipient.jceks -dname "CN=SOAPRecipient, O=ACME" -storepass reciptstorepass
     -keypass reciptkeypass -storetype jceks -validity 5000 -keyalg RSA -keysize 2048
    
    keytool -export -alias soaprecipient -file reciptpub.cer -keystore recipient.jceks -storepass reciptstorepass -storetype jceks
    
    keytool -import -alias soaprecipient -file reciptpub.cer -keystore saml-provider.jceks -storepass issuerstorepass -storetype jceks
     -keypass issuerkeypass -noprompt

结果

您已了解关键构建块来创建 Web service 客户机应用程序以在 SOAP 消息中发送 SAML 安全性令牌以及使用嵌入在消息级别保护中的 SAML 安全性内的对称密钥。


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



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