WSS4J和SAML
各位大侠们请问有用 WSS4J生成SAML断言的吗?
给个伪代码都可以,类有点多,实在看不明白
或者不用WSS4J,其他生成SAML断言的流程呢?
多了,还有断言的验证
请了解的朋友们提示一下吧
谢谢
[解决办法]
/* * To change this template, choose Tools | Templates * and open the template in the editor. */package org.apache.ws.security.util;import java.io.*;import java.util.*;import org.opensaml.Configuration;import org.opensaml.DefaultBootstrap;import org.opensaml.common.*;import org.opensaml.common.xml.*;import org.opensaml.common.impl.*;import org.opensaml.xml.*;import org.opensaml.xml.io.*;import org.opensaml.common.*;import org.opensaml.saml2.core.*;import org.w3c.dom.*;import org.joda.time.*;import org.apache.log4j.*;import org.xml.sax.*;import javax.xml.parsers.*;import javax.xml.namespace.*;import javax.xml.transform.*;/** * * @author zombo */public class SAML { static Logger logger = Logger.getLogger(SAML.class); SAMLVersion samlVersion; XMLObjectBuilderFactory builderFactory; DocumentBuilder docBuilder; SecureRandomIdentifierGenerator generator; public SAML(SAMLVersion _samlVersion) { try { DefaultBootstrap.bootstrap(); builderFactory = Configuration.getBuilderFactory(); } catch (Exception ex) { logger.error("error in " + ex.getMessage()); } this.samlVersion = _samlVersion; } public SAML() { this(SAMLVersion.VERSION_20); } public SAMLVersion getSamlVersion() { return samlVersion; } public void setSamlVersion(SAMLVersion _samlVersion) { this.samlVersion = _samlVersion; } public void setSamlVersion(int version) { switch (version) { case 1: this.samlVersion = SAMLVersion.VERSION_10; break; case 11: this.samlVersion = SAMLVersion.VERSION_11; break; case 2: this.samlVersion = SAMLVersion.VERSION_20; break; default: logger.debug("Error version number of SAML"); break; } } @SuppressWarnings("unchecked") public <T> T create(Class<T> cls, QName qname) { return (T) ((XMLObjectBuilder) Configuration.getBuilderFactory().getBuilder(qname)).buildObject(qname); } public Issuer createIssuer(String name) { return createIssuer(name, Issuer.EMAIL); } /** * create Issuer ELement from the specific issuer name and the specific format * @param name * @param format * @return issuer */ public Issuer createIssuer(String name, String format) { Issuer issuer = create(Issuer.class, Issuer.DEFAULT_ELEMENT_NAME); if (0 > Arrays.binarySearch(issuer_format_arr, format)) { format = Issuer.EMAIL; } issuer.setFormat(format); issuer.setValue(name); logger.info("create Issuer element with issuer " + name + "and format is " + format); return issuer; } public NameID createNameID(String identifier) { NameID nameID = create(NameID.class, NameID.DEFAULT_ELEMENT_NAME); nameID.setFormat(NameID.EMAIL); return nameID; } /** * create NameID element from the specific identitfier and format * @param identifier * @param format * @return nameID */ public NameID createNameID(String identifier, String format) { NameID nameID = create(NameID.class, NameID.DEFAULT_ELEMENT_NAME); if (0 > Arrays.binarySearch(nameID_format_arr, format)) { format = NameID.EMAIL; } nameID.setFormat(format); nameID.setValue(identifier); logger.info("create NameID element with NameID " + identifier + "and format is " + format); return nameID; } public SubjectLocality createSubjectLocality(String addr, String dns) { SubjectLocality subjectLocality = create(SubjectLocality.class, SubjectLocality.DEFAULT_ELEMENT_NAME); subjectLocality.setAddress(addr); subjectLocality.setDNSName(dns); return subjectLocality; } public SubjectLocality createSubjectLocality() { SubjectLocality subjectLocality = create(SubjectLocality.class, SubjectLocality.DEFAULT_ELEMENT_NAME); return subjectLocality; } /** * create subject Subject * @param nameID * @param methods * @return */ public Subject createSubject(NameID nameID, String[] methods) { Subject subject = create(Subject.class, Subject.DEFAULT_ELEMENT_NAME); ArrayList<SubjectConfirmation> subjectConfirmationList = new ArrayList<SubjectConfirmation>(); for (String method : methods) { SubjectConfirmation subjectConfirmation = create(SubjectConfirmation.class, SubjectConfirmation.DEFAULT_ELEMENT_NAME); subjectConfirmation.setMethod(method); subjectConfirmationList.add(subjectConfirmation); } subject.setNameID(nameID); subject.getSubjectConfirmations().addAll(subjectConfirmationList); logger.info("create Subject with name identifier " + nameID.getValue() + "subjectConfirmation Method is " + methods); return subject; } public Subject createSubject(NameID nameID) { Subject subject = create(Subject.class, Subject.DEFAULT_ELEMENT_NAME); SubjectConfirmation subjectConfirmation = create(SubjectConfirmation.class, SubjectConfirmation.DEFAULT_ELEMENT_NAME); subjectConfirmation.setMethod(SubjectConfirmation.METHOD_SENDER_VOUCHES); subject.setNameID(nameID); subject.getSubjectConfirmations().add(subjectConfirmation); return subject; } public Subject createSubject(String user, String[] methods) { NameID nameID = createNameID(user); return createSubject(nameID, methods); } public Subject createSubject(String user ) { NameID nameID = createNameID(user); return createSubject(nameID); }