我正在尝试使用“Saml2RedirectAuthenticationRequest”类生成响应重定向,但是当我进行重定向时,出现以下错误:
java.lang.IllegalArgumentException: samlRequest cannot be null or empty
我知道,我必须创建一个
samlRequest
对象,但我找不到任何关于这种情况的文档。下面是我用来生成请求重定向的示例代码:
Saml2RedirectAuthenticationRequest authnRequest = Saml2RedirectAuthenticationRequest
.withRelyingPartyRegistration(relyingPartyRegsitration)
.relayState(relayState)
.build();
String encodedRequest = URI.create(authnRequest.getAuthenticationRequestUri().toString()).toASCIIString();
response.sendRedirect(encodedRequest);
你收到错误的原因是因为
Saml2RedirectAuthenticationRequest
要求你指定签名和序列化的 samlRequest
作为构建它的一部分。
Saml2RedirectAuthenticationRequest
是一个包含身份验证请求的域对象,而不是创建、签名和序列化请求的对象。
创建、签名和序列化的组件是
Saml2AuthenticationRequestResolver
。当给定一个HttpServletRequest
时,它返回一个Saml2RedirectAuthenticationRequest
。您可以在 Spring Security 的文档 中阅读更多相关信息。
我建议使用 Spring Security 的过滤器链为您创建和发送
AuthnRequest
,而不是直接制定它。当您在 saml2Login
设置中使用 HttpSecurity
DSL 方法或为 SAML 配置 Spring Boot 时,当您重定向到 /saml2/authenticate/{registrationId}
. 时,这一切都会为您完成
您可以看到 Spring Security 的 Boot 示例 完全配置了 SAML 2.0 登录和注销。