如何从 opensaml 2.6 迁移到 3.1.1

2022-09-03 00:37:37

我必须将一个类从 opensaml 2.6 迁移到 opensaml 3.1.1 编译我得到一些错误

1)

Element plaintextElement = getElementAssertion(inputBean);
String xml = XMLHelper.prettyPrintXML(plaintextElement);

我在新版本中找不到类 XMLHelper。

2)

DefaultBootstrap.bootstrap();
builderFactory = Configuration.getBuilderFactory();
Configuration.getMarshallerFactory().getMarshaller(assertion).marshall(assertion);

我找不到类DefaultBootstrap,我找不到一个类配置与方法getBuilderFactory(),getMarshallerFactory()

3)

BasicCredential credential = new BasicCredential();

现在构造函数 new BasicCredential() 不可见。

我没有找到带有弃用指示的文档。我必须执行哪些操作才能将此类移植到 opensaml 3.1.1 版本?


答案 1

不知道你是否已经设法升级到opensaml 3,但是由于我在尝试自己升级时遇到了这个问题,我想我会记录我的发现。

文档很少,因为显然目前这不是他们的优先事项(这里也提到了OpenSaml3文档),我发现的最有用的(即使到目前为止还不完整)页面是这个:https://wiki.shibboleth.net/confluence/display/OS30/Initialization+and+Configuration

1) 在 lib 中有一个带有方法的类SerializeSupportprettyPrintXMLnet.shibboleth.utilities:java-support

2) 初始化现在通过以下方式完成:InitializationService

InitializationService.initialize();

您可以通过以下方式检索构建器/编组器,例如:XMLObjectProviderRegistrySupport

XMLObjectProviderRegistrySupport.getMarshallerFactory()
XMLObjectProviderRegistrySupport.getBuilderFactory()
XMLObjectProviderRegistrySupport.getUnmarshallerFactory()

请注意,opensaml 使用的是 Java Service Provider API。在我的情况下(使用OSGi捆绑包),为了解析SAML断言,我添加了包含以下条目的SPI配置:org.apache.servicemix.bundles:org.apache.servicemix.bundles.opensamlMETA-INF/services/org.opensaml.core.config.Initializer

org.opensaml.core.xml.config.XMLObjectProviderInitializer
org.opensaml.core.xml.config.GlobalParserPoolInitializer
org.opensaml.saml.config.XMLObjectProviderInitializer
org.opensaml.saml.config.SAMLConfigurationInitializer
org.opensaml.xmlsec.config.XMLObjectProviderInitializer

编辑:上述方法在测试中起作用,但在 OSGi 容器中未运行。OSGi 的解决方法:在 OSGi 容器中找不到 OpenSAML3 资源“默认配置.xml”

如果使用标准库 (, , , , ...),则可能不需要添加任何 SPI 配置,因为 jar 已经包含具有用于初始化的标准配置的 SPI 配置。org.opensaml:opensaml-coreorg.opensaml:opensaml-saml-apiorg.opensaml:opensaml-saml-impl

3)lib中有一个类。我没有看到在初始化期间提供密钥的替代方法。BasicCredentialorg.opensaml:opensaml-security-api


答案 2

我正在学习如何使用OS3进行开发。这是在 V3 版本中将 base 64 saml 请求转换为 SAMLObject 的一个示例。希望它能帮助你。

项目查看 github 存储库

public class SAMLToolkit {

    public static SAMLObject convertBase64ToSaml(String base64Str) {
        byte[] decodedBytes = new byte[0];
        try {
            decodedBytes = Base64.decode(base64Str);
        } catch (Base64DecodingException e) {
            e.printStackTrace();
            return null;
        }

        InputStream is = new ByteArrayInputStream(decodedBytes);
        //is = new InflaterInputStream(is, new Inflater(true));
        try {

            InitializationService.initialize();
            Document messageDoc;
            BasicParserPool basicParserPool = new BasicParserPool();
            basicParserPool.initialize();
            messageDoc = basicParserPool.parse(is);
            Element messageElem = messageDoc.getDocumentElement();
            Unmarshaller unmarshaller = XMLObjectProviderRegistrySupport.getUnmarshallerFactory().getUnmarshaller(messageElem);

            assert unmarshaller != null;
            return(SAMLObject) unmarshaller.unmarshall(messageElem);
        } catch (InitializationException e) {
            e.printStackTrace();
            return null;
        } catch (XMLParserException e) {
            e.printStackTrace();
            return null;
        } catch (UnmarshallingException e) {
            e.printStackTrace();
            return null;
        } catch (ComponentInitializationException e) {
            e.printStackTrace();
            return null;
        }
    }
}

推荐