简体   繁体   English

使用Java创建“ HTTP重定向绑定” SAML请求

[英]Create 'HTTP Redirect Binding' SAML Request using Java

I am trying to implement the same algorithm in Java for SAML HTTP Redirect Binding which is described here: How do I correctly prepare an 'HTTP Redirect Binding' SAML Request using C# 我试图在Java中为SAML HTTP重定向绑定实现相同的算法,如下所述: 如何使用C#正确准备“ HTTP重定向绑定” SAML请求

The algorithm is rather simple: 该算法非常简单:

  1. Build a SAML string 建立SAML字串
  2. Compress this string 压缩这个字符串
  3. Base64 encode the string Base64编码字符串
  4. UrlEncode the string. 用Url编码字符串。

This should be the equivalent Java algorithm: 这应该是等效的Java算法:

    public String encodeRedirectFormat( String samlXML ) throws IOException{
            ByteArrayOutputStream os = new ByteArrayOutputStream();
            DeflaterOutputStream deflaterOutputStream = new DeflaterOutputStream(os);
            deflaterOutputStream.write( samlXML.getBytes( "UTF-8" ) );
            deflaterOutputStream.close();
            os.close();
            String base64 = Base64.encodeBase64String( os.toByteArray() );
            return URLEncoder.encode( base64, "UTF-8" );
    }

I try to encode the simplest assertion: 我尝试编码最简单的断言:

<saml2:Assertion xmlns:saml2="urn:oasis:names:tc:SAML:2.0:assertion"/>

This is the output: 这是输出:

eJyzKU7MzTGyciwuTi0qyczPU6jIzckrtgKL2iqVFuVZ5ScWZxZb5SXmphZblSRbBTv6%2BlgZ6RlYJcK0KOnbAQCHfRi3

And then try to decode with an online tool like 然后尝试使用在线工具进行解码

https://rnd.feide.no/simplesaml/module.php/saml2debug/debug.php https://rnd.feide.no/simplesaml/module.php/saml2debug/debug.php

the output is invalid. 输出无效。 Can someone spot the error? 有人可以发现错误吗? Maybe the Java Deflater works differently? 也许Java Deflater的工作方式有所不同?

You need to instruct specifically the Deflater for noWrap option. 您需要特别指示“ Deflater for noWrap选项。 This is the working code: 这是工作代码:

public String encodeRedirectFormat( String samlXML ) throws IOException{
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        Deflater deflater = new Deflater( Deflater.DEFAULT_COMPRESSION, true );
        DeflaterOutputStream deflaterOutputStream = new DeflaterOutputStream(os, deflater);
        deflaterOutputStream.write( samlXML.getBytes( "UTF-8" ) );
        deflaterOutputStream.close();
        os.close();
        String base64 = Base64.encodeBase64String( os.toByteArray() );
        return URLEncoder.encode( base64, "UTF-8" );
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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