简体   繁体   中英

How to display Saml assertion nodes?

Hi guys i hope You are all ok I've created a saml assertion with opensaml-j and i want to dispaly its content but when i do that it throws NullPointerException in the main method can some one tell me the reason why?? here's my code

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

and here's the main class:

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

and this is the 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) 

This rings a bell about bootstraping. http://blog.samlsecurity.com/2014/05/nullpointer-exception-in-opensaml.html

Have you run the bootstrap anywhere?

DefaultBootstrap.bootstrap();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM