使用Java创建"HTTP重定向绑定" SAML请求 [英] Create 'HTTP Redirect Binding' SAML Request using Java

查看:456
本文介绍了使用Java创建"HTTP重定向绑定" SAML请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Java中为SAML HTTP重定向绑定实现相同的算法,该算法在此处进行描述:

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#

算法非常简单:

  1. 构建SAML字符串
  2. 压缩此字符串
  3. Base64编码字符串
  4. 使用网址对字符串进行编码.

这应该是等效的Java算法:

This should be the equivalent Java algorithm:

    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"/>

这是输出:

eJyzKU7MzTGyciwuTi0qyczPU6jIzckrtgKL2iqVFuVZ5ScWZxZb5SXmphZblSRbBTv6%2BlgZ6RlYJcK0KOnbAQCHfRi3

然后尝试使用

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

输出无效.有人可以发现错误吗?也许Java Deflater的工作方式有所不同?

the output is invalid. Can someone spot the error? Maybe the Java Deflater works differently?

推荐答案

您需要特别说明Deflater for noWrap选项.这是工作代码:

You need to instruct specifically the Deflater for noWrap option. 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" );
}

这篇关于使用Java创建"HTTP重定向绑定" SAML请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆