向 IP Keycloak 发送 SamlAuthn 请求时出现无效请求错误

问题描述 投票:0回答:1

作为 IT 行业的新人,我尝试使用以下方法向我的身份提供商 (Keycloak) 发送 SAML 请求。但是,我在重定向到授权端点 URL 时遇到“InvalidRequest”错误,我将其设置为“http://localhost:8080/realms/realmsname/protocol/saml”。

String samlRequest = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<samlp:AuthnRequest 
xmlns:samlp=\"urn:oasis:names:tc:SAML:2.0:protocol\" "
+ "xmlns:saml=\"urn:oasis:names:tc:SAML:2.0:assertion\" " 
+ "ID=\""+ Instant.now().toEpochMilli() + "\" "
+ "Version=\"2.0\" " 
+"IssueInstant=\"" + Instant.now() + "\" "
+ "ProtocolBinding=\"urn:oasis:names:tc:SAML:2.0:bindings:HTTP-POST\" "
+ "AssertionConsumerServiceURL=\"http:localhost:8001/saml/login\">"
+ "<saml:Issuer>http://localhost:8080/realms/<></saml:Issuer>"
+ "<samlp:NameIDPolicy Format=\"urn:oasis:names:tc:SAML:2.0:nameid-format:transient\" AllowCreate=\"true\"/>"
+ "</samlp:AuthnRequest>";

// Encode SAML request into Base64 (no need for compression)
String base64Encoded = Base64.toBase64String(samlRequest.getBytes());
URL url = new URL(deploymentUtil.getIdpAuthorizationEndpoint());
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("GET");
con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            
// Redirect user to the Keycloak authorization endpoint
String redirectUrl = url+ "?SAMLRequest=" + base64Encoded;
resp.sendRedirect(redirectUrl);

如果有人知道请帮助我。

我已经尝试过此操作,并尝试针对一些在线工具进行验证以验证 saml 请求,我得到的错误为 SAML AuthN 请求无效。与 saml-schema-protocol-2.0.xsd 不匹配

<samlp:AuthnRequest xmlns:samlp="urn:oasis:names:tc:SAML:2.0:protocol" xmlns:saml="urn:oasis:names:tc:SAML:2.0:assertion" ID="1712556379904" Version="2.0" IssueInstant="2024-04-08T06:06:19.904Z" ProtocolBinding="urn:oasis:names:tc:SAML:2.0:bindings:HTTP-POST" AssertionConsumerServiceURL="http://localhost:8005/sso/restapp/saml/login"><saml:Issuer>"http://localhost:8080/realms/<realmsName>" </saml:Issuer>
 <samlp:NameIDPolicy Format="urn:oasis:names:tc:SAML:1.1:nameid-format:emailAddress" AllowCreate="true"/>
  <samlp:RequestedAuthnContext Comparison="exact">
    <saml:AuthnContextClassRef>urn:oasis:names:tc:SAML:2.0:ac:classes:PasswordProtectedTransport</saml:AuthnContextClassRef>
  </samlp:RequestedAuthnContext>
</samlp:AuthnRequest>

使用 Online Now 的 Online 工具执行更多验证后,我得到 THE SAML AUTHN REQUEST IS VALID。但是,我仍然在 keycloak 处面临无效请求。

<?xml version="1.0" encoding="UTF-8"?><samlp:AuthnRequest xmlns:samlp="urn:oasis:names:tc:SAML:2.0:protocol" xmlns:saml="urn:oasis:names:tc:SAML:2.0:assertion" ID="_882154b2ee4fa83b774e16a149713064" Version="2.0" IssueInstant="2024-04-09T11:48:28.540Z" ProtocolBinding="urn:oasis:names:tc:SAML:2.0:bindings:HTTP-POST" AssertionConsumerServiceURL="http://localhost:8001/rbac/login/cas"><saml:Issuer>http://localhost:8080/realms/OBSuiteRealm</saml:Issuer><samlp:NameIDPolicy Format="urn:oasis:names:tc:SAML:1.1:nameid-format:unspecified" AllowCreate="true"/><samlp:RequestedAuthnContext xmlns:samlp="urn:oasis:names:tc:SAML:2.0:protocol" Comparison="exact"><saml:AuthnContextClassRef xmlns:saml="urn:oasis:names:tc:SAML:2.0:assertion">urn:oasis:names:tc:SAML:2.0:ac:classes:PasswordProtectedTransport</saml:AuthnContextClassRef></samlp:RequestedAuthnContext></samlp:AuthnRequest>

Redirect Url 就是这样的

http://127.0.0.1:8080/realms/releamname/protocol/saml?SAMLRequest=nVNNb+MgFPw............

spring-mvc spring-security keycloak saml-2.0
1个回答
0
投票

我的身份提供商(HTTP 重定向绑定除外),我正在尝试使用 HTTP-Post 绑定发送 SamlAuthn 请求。

当您使用 HTTP 重定向绑定时,您必须发送 saml 请求 已被压缩并且(使用 UTF-8 的 Deflate+Base64+UrlEncoder)

ClientId 应与服务提供商 EntityId 匹配

© www.soinside.com 2019 - 2024. All rights reserved.