使用 WSS API 插入 SAML 属性
您可以使用 Java™ API for XML-Based Web Services (JAX-WS) 编程模型和 Web service 安全 API (WSS API) 将定制属性插入自签发 SAML 令牌。
开始之前
本任务假设您熟悉 JAX-WS 编程模型、WSS API 接口、SAML 概念以及策略集的使用以配置和管理 Web service 设置。
请在开始本任务前,完成下列操作:
- 阅读关于使用 WSS API 传播自签发 SAML 不记名令牌的信息。
- 阅读关于通过将 WSS API 用于消息级别保护来传播自签发 SAML sender-vouches 令牌的信息。
- 阅读关于通过将 WSS API 与 SSL 传输保护一起用于传播自签发 SAML sender-vouches 令牌的信息。
- 阅读关于使用 WSS API 以对称密钥传播自签发 SAML holder-of-key 令牌的信息。
- 阅读关于使用 WSS API 以非对称密钥传播自签发 SAML holder-of-key 令牌的信息。
关于此任务
过程
创建 SAML 安全性令牌时插入定制属性,例如:
import com.ibm.websphere.wssecurity.wssapi.token.SecurityToken;
import com.ibm.websphere.wssecurity.callbackhandler.SAMLGenerateCallbackHandler;
import com.ibm.websphere.wssecurity.wssapi.token.SAMLToken;
import com.ibm.wsspi.wssecurity.core.token.config.WSSConstants;
import com.ibm.wsspi.wssecurity.saml.config.SamlConstants;
import com.ibm.wsspi.wssecurity.saml.data.SAMLAttribute;
WSSFactory factory = WSSFactory.getInstance();
HashMap<Object, Object> map = new HashMap<Object, Object>();
map.put(SamlConstants.CONFIRMATION_METHOD, "Bearer");
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");
ArrayList<SAMLAttribute> al = new ArrayList<SAMLAttribute>();
String groups[] = {"IBMer", "Texan"};
SAMLAttribute sattribute = new SAMLAttribute("Membership", groups, null,null, null, null);
al.add(sattribute);
String gender[] = {"Female"};
sattribute = new SAMLAttribute("Gender", gender, null,null, null, null);
al.add(sattribute);
map.put(SamlConstants.SAML_ATTRIBUTES, al);
SAMLGenerateCallbackHandler callbackHandler = new SAMLGenerateCallbackHandler(map);
SecurityToken samlToken = factory.newSecurityToken(SAMLToken.class, callbackHandler,
"system.wss.generate.saml");
结果
您将定制属性插入 SAML 安全性令牌。
示例
<saml2:Assertion xmlns:saml2="urn:oasis:names:tc:SAML:2.0:assertion"
Version="2.0"
ID="_E62A1CA3C2F21D9A9B1287772824570"
IssueInstant="2010-10-22T18:40:24.531Z">
<saml2:Issuer>example.com</samls2:Issuer>
<ds:Signature xmlns:ds="http://www.w3.org/2000/09/xmldsig#">
...
</ds:Signature>
<saml2:Subject>
<saml2:NameID>Alice</saml2:NameID>
<saml2:SubjectConfirmation Method="urn:oasis:names:tc:SAML:2.0:cm:bearer"></saml2:SubjectConfirmation>
</saml2:Subject>
<saml2:Conditions NotBefore="2010-10-22T18:40:24.531Z"
NotOnOrAfter="2010-10-22T19:40:24.531Z">
</saml2:Conditions>
<saml2:AttributeStatement>
<saml2:Attribute Name="Membership">
<saml2:AttributeValue>IBMer</saml2:AttributeValue>
<saml2:AttributeValue>Texan</saml2:AttributeValue>
</saml2:Attribute>
<saml2:Attribute Name="Gender">
<saml2:AttributeValue>Female</saml2:AttributeValue>
</saml2:Attribute>
</saml2:AttributeStatement>
</saml2:Assertion>