2017-06-02 52 views
0

嗨,大家好,我希望你们都好 我用opensaml-j创建了一个saml断言,我想要显示它的内容 但是当我这样做时,它在主方法中抛出NullPointerException 有人可以告诉我原因为什么?? 这里是我的代码如何显示Saml断言节点?

import java.security.SecureRandom; 

import org.joda.time.DateTime; 
import org.opensaml.core.xml.schema.XSString; 
import org.opensaml.core.xml.schema.impl.XSStringBuilder; 
import org.opensaml.saml.common.SAMLVersion; 
import org.opensaml.saml.saml1.core.NameIdentifier; 
import org.opensaml.saml.saml1.core.Subject; 
import org.opensaml.saml.saml2.core.impl.EvidenceBuilder; 
import org.opensaml.saml.saml1.core.impl.NameIdentifierBuilder; 
import org.opensaml.saml.saml1.core.impl.SubjectBuilder; 
import org.opensaml.saml.saml2.core.Action; 
import org.opensaml.saml.saml2.core.Assertion; 
import org.opensaml.saml.saml2.core.Attribute; 
import org.opensaml.saml.saml2.core.AttributeStatement; 
import org.opensaml.saml.saml2.core.AttributeValue; 
import org.opensaml.saml.saml2.core.AuthzDecisionStatement; 
import org.opensaml.saml.saml2.core.Conditions; 
import org.opensaml.saml.saml2.core.DecisionTypeEnumeration; 
import org.opensaml.saml.saml2.core.Evidence; 
import org.opensaml.saml.saml2.core.Issuer; 
import org.opensaml.saml.saml2.core.impl.ActionBuilder; 
import org.opensaml.saml.saml2.core.impl.AssertionBuilder; 
import org.opensaml.saml.saml2.core.impl.AttributeBuilder; 
import org.opensaml.saml.saml2.core.impl.AttributeStatementBuilder; 
import org.opensaml.saml.saml2.core.impl.AuthzDecisionStatementBuilder; 
import org.opensaml.saml.saml2.core.impl.ConditionsBuilder; 
import org.opensaml.saml.saml2.core.impl.IssuerBuilder; 

public class CreateSamlAssertion { 

@SuppressWarnings("deprecation") 
public static Assertion createAssertion(){ 
    Assertion assertion=(Assertion)new AssertionBuilder().buildObject(); 
    assertion.setID(idGenerator()); 
    assertion.setVersion(SAMLVersion.VERSION_20); 
    assertion.setIssueInstant(new DateTime()); 
    Issuer issuer=(Issuer)new IssuerBuilder().buildObject(); 
    issuer.setNameQualifier("tfou");//StaticData.logName 
    assertion.setIssuer(issuer); 
    Subject subject=(Subject)new SubjectBuilder().buildObject(); 
    NameIdentifier nameId=(NameIdentifier)new NameIdentifierBuilder().buildObject(); 
    nameId.setFormat(NameIdentifier.EMAIL); 
    nameId.setNameIdentifier("tfou2");//StaticData.destName 
    subject.setNameIdentifier(nameId); 
    Conditions conditions=(Conditions)new ConditionsBuilder().buildObject(); 
    conditions.setNotBefore(new DateTime()); 
    conditions.setNotOnOrAfter(new DateTime().plusMinutes(20)); 
    assertion.setConditions(conditions); 
    AuthzDecisionStatement authDecisionStatement=getAuthzDecisionStatement(); 
    assertion.getAuthzDecisionStatements().add(authDecisionStatement); 
    return assertion; 
} 

private static AuthzDecisionStatement getAuthzDecisionStatement(){ 
    AuthzDecisionStatement aDStatement=(AuthzDecisionStatement)new AuthzDecisionStatementBuilder().buildObject(); 
    DecisionTypeEnumeration decision = DecisionTypeEnumeration.PERMIT; 
    aDStatement.setDecision(decision); 
    aDStatement.setResource("http://www.hb.com/Resources/Resource A1"); 
    Action actions=getAction(); 
    actions.setNamespace("http://www.hb.com/Resources/"); 
    aDStatement.getActions().add(actions); 
    Evidence evidence=getEvidence(); 
    aDStatement.setEvidence(evidence); 
    return aDStatement; 
} 


private static Evidence getEvidence(){ 

    Evidence evidence=(Evidence) new EvidenceBuilder().buildObject(); 
    Assertion assertion=(Assertion)new AssertionBuilder().buildObject(); 
    AttributeStatement aStatement=(AttributeStatement) new AttributeStatementBuilder().buildObject(); 
    Attribute attribute1=(Attribute)new AttributeBuilder().buildObject(); 
    attribute1.setName("IssuerCapabilityID"); 

    XSString attrValue1 = (XSString)new XSStringBuilder(). 
      buildObject(AttributeValue.DEFAULT_ELEMENT_NAME, XSString.TYPE_NAME); 
    attrValue1.setValue("Cap01"); 
    attribute1.getAttributeValues().add(attrValue1); 

    Attribute attribute2=(Attribute)new AttributeBuilder().buildObject(); 
    attribute2.setName("IssuerCapabilityID"); 
    XSString attrValue2 = (XSString)new XSStringBuilder(). 
      buildObject(AttributeValue.DEFAULT_ELEMENT_NAME, XSString.TYPE_NAME); 
    attrValue2.setValue("Cap02"); 
    attribute2.getAttributeValues().add(attrValue2); 


    aStatement.getAttributes().add(attribute1); 
    aStatement.getAttributes().add(attribute2); 
    assertion.getAttributeStatements().add((org.opensaml.saml.saml2.core.AttributeStatement) aStatement); 
    evidence.getAssertions().add(assertion); 
    return evidence; 
} 

private static Action getAction(){ 
    Action action=(Action)new ActionBuilder().buildObject(); 
    action.setAction("Read"); 
    action.setAction("Write"); 
    return action; 
} 

private static String idGenerator(){ 
    SecureRandom random=new SecureRandom(); 
    return String.valueOf(random.nextInt()); 
} 
} 

和这里的主类:

import org.opensaml.core.xml.io.MarshallingException; 
import org.opensaml.saml.saml2.core.Assertion; 
import org.opensaml.saml.saml2.core.impl.AssertionMarshaller; 
import org.opensaml.xml.util.XMLHelper; 
import org.w3c.dom.Element; 


public class SamlTest { 

public static void main(String[] args) throws MarshallingException { 
    // TODO Auto-generated method stub 
    Assertion assertion=CreateSamlAssertion.createAssertion(); 
    AssertionMarshaller marshaller = new AssertionMarshaller(); 
    Element plaintextElement = marshaller.marshall(assertion); 
    String originalAssertionString = XMLHelper.nodeToString(plaintextElement); 

    System.out.println("Assertion String: " + originalAssertionString); 
} 
} 

,这是堆栈跟踪

Exception in thread "main" java.lang.NullPointerException 
 
     at org.opensaml.core.xml.config.XMLObjectProviderRegistrySupport.getMarshallerFactory(XMLObjectProviderRegistrySupport.java:116) 
 
     at org.opensaml.core.xml.io.AbstractXMLObjectMarshaller.<init>(AbstractXMLObjectMarshaller.java:68) 
 
     at org.opensaml.saml.common.AbstractSAMLObjectMarshaller.<init>(AbstractSAMLObjectMarshaller.java:30) 
 
     at org.opensaml.saml.saml2.core.impl.AssertionMarshaller.<init>(AssertionMarshaller.java:34) 
 
     at SamlTest.main(SamlTest.java:14)

回答

0

这戒指有关的Bootstrap钟。 http://blog.samlsecurity.com/2014/05/nullpointer-exception-in-opensaml.html

你有没有在任何地方运行bootstrap?这里

DefaultBootstrap.bootstrap(); 
+0

先生感谢您的回答,但现在我还有一个问题 它抛出ClassCastException异常 的堆栈跟踪: '异常在线程“主要” java.lang.ClassCastException:org.opensaml.core.xml.schema .impl.XSAnyBuilder无法转换为org.opensaml.xml.XMLObjectBuilder – Karim

+0

我建议将该帖子发布为新问题 –

+0

我不能要求我现在不得不等待2天再提问 – Karim