2017-06-02 4 views
0

Hallo Leute, ich hoffe, Sie sind alle in Ordnung Ich habe eine saml Assertion mit opensaml-j erstellt und ich möchte seinen Inhalt dispaly aber wenn ich das mache, wirft es NullPointerException in der Hauptmethode kann jemand mir den Grund sagen Warum?? hier ist mein CodeWie Saml Assertion Nodes anzeigen?

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()); 
} 
} 

und hier ist die Hauptklasse:

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); 
} 
} 

und dies ist der Stack-Trace

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)

Antwort

0

Diese Ringe eine Glocke über bootstraping. http://blog.samlsecurity.com/2014/05/nullpointer-exception-in-opensaml.html

Haben Sie den Bootstrap irgendwo ausgeführt?

DefaultBootstrap.bootstrap(); 
+0

Dank sir für Ihre Antwort, aber jetzt habe ich ein anderes Problem es wirft Classcast hier ist der Stack-Trace: 'Exception in thread "main" java.lang.ClassCastException: org.opensaml.core.xml.schema .impl.XSAnyBuilder kann nicht in org.opensaml.xml.XMLObjectBuilder umgewandelt werden – Karim

+0

Ich schlage vor, dass als neue Frage –

+0

ich kann nicht für den Moment fragen, ich muss warten 2 Tage, um erneut zu fragen – Karim

Verwandte Themen