线程异常 "main" org.xml.sax.SAXParseException;行号:1;列数:1;序言中不能有内容

Exception in thread "main" org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 1; Content is not allowed in prolog

我是 WSO2 API Manager 版本 1.9.1 的新手。我希望使用 OpenSAML 库 (http://mvnrepository.com/artifact/org.opensaml/opensaml/2.6.4). I wanted same x-jwt-assertion (shown in this link ) 将 x-jwt-assertion 解码为 decoded/parse,但是当执行下面的代码时,我看到以下错误。请指导

是否可以使用 OpenSAML 解码 WSO2 APIM(API 管理器)的 x-jwt-assertion

参考代码:

Exception in thread "main" org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 1; Content is not allowed in prolog.
    at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(DOMParser.java:257)
    at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:347)
    at javax.xml.parsers.DocumentBuilder.parse(DocumentBuilder.java:121)
    at com.mkyong.app.OpenSAMLDemo.main(OpenSAMLDemo.java:46)

参考代码:

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.Properties;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.apache.commons.codec.binary.Base64;
import org.opensaml.Configuration;
import org.opensaml.DefaultBootstrap;
import org.opensaml.saml2.core.Assertion;
import org.opensaml.saml2.core.Response;
import org.opensaml.xml.ConfigurationException;
import org.opensaml.xml.XMLObject;
import org.opensaml.xml.io.Unmarshaller;
import org.opensaml.xml.io.UnmarshallerFactory;
import org.opensaml.xml.io.UnmarshallingException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.xml.sax.SAXException;

public class OpenSAMLDemo {
    public static void main(String[] args) throws IOException, 
    ParserConfigurationException, SAXException, ConfigurationException, UnmarshallingException {

        Properties prop = new Properties();
        prop.load(OpenSAMLDemo.class.getClassLoader().getResourceAsStream("jwtAssertion.properties"));

        String responseMessage = prop.getProperty("jwt");
        System.out.println(responseMessage);

        Base64 base64 = new Base64();
        byte[] base64DecodedResponse = base64.decode(responseMessage);

        DefaultBootstrap.bootstrap();

        ByteArrayInputStream is = new ByteArrayInputStream(base64DecodedResponse);

        DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
        documentBuilderFactory.setNamespaceAware(true);
        DocumentBuilder docBuilder = documentBuilderFactory.newDocumentBuilder();

        Document document = docBuilder.parse(is);
        Element element = document.getDocumentElement();

        UnmarshallerFactory unmarshallerFactory = Configuration.getUnmarshallerFactory();
        Unmarshaller unmarshaller = unmarshallerFactory.getUnmarshaller(element);
        XMLObject responseXmlObj = unmarshaller.unmarshall(element);

        Response response = (Response) responseXmlObj;

        Assertion assertion = response.getAssertions().get(0);

        String subject = assertion.getSubject().getNameID().getValue();
        System.out.println("SUBJECT  : " + subject );

        String issuer = assertion.getIssuer().getValue();
        System.out.println("ISSUER  : " + issuer);

        String audience = assertion.getConditions().getAudienceRestrictions().get(0).getAudiences().get(0).getAudienceURI();
        System.out.println("AUDIENCE  : " + audience );
    }
}

您正在使用 WSO2 API Manager 工具。 API Manager 给出 X-JWT-Assertion,这与 WSO2 SAML Assertion 不同。所以 OpenSAML 库将无法使用。因为它不是为支持 X-JWT-Assertion 而开发的。为了解析 X-JWT-Assertion,您需要使用 axiom-api.

请参考此站点和提供的解决方案。