如何修复Saml断言异常(NoClassDefFoundError)

时间:2017-05-29 18:05:06

标签: java opensaml

大家好我已经用opensaml-j创建了一个saml断言,当我运行该程序时发生异常并抛出NoClassDefFoundError 我已经加载了所有必要的jar文件,但仍然无法正常工作 这是我的代码:

package memory;

import java.security.SecureRandom;

import org.joda.time.DateTime;
import org.opensaml.saml.common.SAMLVersion;
import org.opensaml.saml.saml1.core.AttributeStatement;
import org.opensaml.saml.saml1.core.AttributeValue;
import org.opensaml.saml.saml1.core.NameIdentifier;
import org.opensaml.saml.saml1.core.Subject;
import org.opensaml.saml.saml1.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.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("bob");
    assertion.setIssuer(issuer);
    Subject subject=(Subject)new SubjectBuilder().buildObject();
    NameIdentifier nameId=(NameIdentifier)new NameIdentifierBuilder().buildObject();
    nameId.setFormat(NameIdentifier.EMAIL);
    nameId.setNameIdentifier("alice");
    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");
    AttributeValue aValue1=(AttributeValue) AttributeValue.DEFAULT_ELEMENT_NAME;
    attribute1.getAttributeValues().add(aValue1);

    Attribute attribute2=(Attribute)new AttributeBuilder().buildObject();
    attribute2.setName("IssuerCapabilityID");
    AttributeValue aValue2=(AttributeValue) AttributeValue.DEFAULT_ELEMENT_NAME;
    attribute1.getAttributeValues().add(aValue2);

    aStatement.getAttributes().add((org.opensaml.saml.saml1.core.Attribute) attribute1);
    aStatement.getAttributes().add((org.opensaml.saml.saml1.core.Attribute) 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());
}
}

以及这里的主要课程:

package memory;

import org.opensaml.saml.saml2.core.Assertion;

public class SamlTest {

public static void main(String[] args) {
    // TODO Auto-generated method stub
    Assertion assertion=CreateSamlAssertion.createAssertion();
    System.out.println(assertion);
}

}

堆栈跟踪



Exception in thread "main" java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory
	at org.opensaml.core.xml.AbstractXMLObject.<init>(AbstractXMLObject.java:47)
	at org.opensaml.xmlsec.signature.AbstractSignableXMLObject.<init>(AbstractSignableXMLObject.java:47)
	at org.opensaml.saml.common.AbstractSignableSAMLObject.<init>(AbstractSignableSAMLObject.java:44)
	at org.opensaml.saml.saml2.core.impl.AssertionImpl.<init>(AssertionImpl.java:83)
	at org.opensaml.saml.saml2.core.impl.AssertionBuilder.buildObject(AssertionBuilder.java:45)
	at org.opensaml.saml.saml2.core.impl.AssertionBuilder.buildObject(AssertionBuilder.java:40)
	at memory.CreateSamlAssertion.createAssertion(CreateSamlAssertion.java:34)
	at memory.SamlTest.main(SamlTest.java:9)
Caused by: java.lang.ClassNotFoundException: org.slf4j.LoggerFactory
	at java.net.URLClassLoader.findClass(Unknown Source)
	at java.lang.ClassLoader.loadClass(Unknown Source)
	at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
	at java.lang.ClassLoader.loadClass(Unknown Source)
	... 8 more
&#13;
&#13;
&#13;

并提前致谢

0 个答案:

没有答案