我喜欢使用 spring-security-saml2-service-provider 的想法 - 来自文档:https://docs.spring.io/spring-security/reference/5.6.0-RC1/servlet/saml2/index。 html 它看起来不像 spring-security-saml2-core 那样简单,但当我从 Okta 管理应用程序发送应用程序嵌入链接时,我收到 400 响应。通过调试看来
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
AbstractSaml2AuthenticationRequest authenticationRequest = this.authenticationRequestResolver.resolve(request);
if (authenticationRequest == null) {
filterChain.doFilter(request, response);..}
无法解析传入的请求,但我不确定它是否相关。 我的 yml 配置:
security:
saml2:
relyingparty:
registration:
okta:
identityprovider:
entity-id: http://www.okta.com/exk1juy5xrR5BsW44697
verification.credentials:
- certificate-location: "classpath:saml/okta.cert"
singlesignon.url: https://trial-8410773.okta.com/app/trial-8410773_templatemanager_2/exk1juy5xrR5BsW44697/sso/saml
singlesignon.sign-request: false
assertingparty.metadata-uri: https://trial-8410773.okta.com/app/trial-8410773_templatemanager_2/exk1juy5xrR5BsW44697/sso/saml/metadata
我的 Okta 配置:
GENERAL
Single Sign On URLhttp://localhost:8080/api/v1/saml2/SSO
Requestable SSO URLsURLIndex
http://localhost:8080/api/v1/saml2/SSO0Recipient URLhttp://localhost:8080/api/v1/saml2/SSODestination URLhttp://localhost:8080/api/v1/saml2/SSOAudience Restrictionhttp://localhost:8080/saml/metadata
我还提供 saml 身份验证端点:
@RequestMapping(SsoAuthenticationController.BASE_NAME)
public interface SsoAuthenticationController {
final String BASE_NAME = "/v1/saml2/SSO";
@GetMapping("/")
public ResponseEntity<HttpStatus> index( Saml2AuthenticatedPrincipal principal) ;
}
实际安全配置:
http.cors()
.and()
.csrf()
.disable()
.authorizeRequests()
.antMatchers(SECURITY_WHITELIST)
.permitAll()
.anyRequest()
.authenticated()
/*.and()
.httpBasic()
.and()
.exceptionHandling()
.authenticationEntryPoint(authenticationEntryPoint).and().sessionManagement()
.sessionCreationPolicy(SessionCreationPolicy.STATELESS)*/
.and()
.saml2Login(Customizer.withDefaults());
这是 Google Chrome 的 Saml 拦截器日志: https://pastebin.com/Be3NZe5B
有什么想法吗?
我最近使用 Okta 创建了一个 Spring Boot 3 + SAML 示例。希望这些说明有所帮助。
使用 start.spring.io 创建 Spring Boot 应用程序。选择以下选项:
添加
src/main/java/com/example/demo/HomeController.java
以填充经过身份验证的用户的信息。
package com.example.demo;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.saml2.provider.service.authentication.Saml2AuthenticatedPrincipal;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class HomeController {
@RequestMapping("/")
public String home(@AuthenticationPrincipal Saml2AuthenticatedPrincipal principal, Model model) {
model.addAttribute("name", principal.getName());
model.addAttribute("emailAddress", principal.getFirstAttribute("email"));
model.addAttribute("userAttributes", principal.getAttributes());
return "home";
}
}
创建一个
src/main/resources/templates/home.html
文件来呈现用户的信息。
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="https://www.thymeleaf.org"
xmlns:sec="https://www.thymeleaf.org/thymeleaf-extras-springsecurity6">
<head>
<title>Spring Boot and SAML</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
</head>
<body>
<h1>Welcome</h1>
<p>You are successfully logged in as <span sec:authentication="name"></span></p>
<p>Your email address is <span th:text="${emailAddress}"></span>.</p>
<p>Your authorities are <span sec:authentication="authorities"></span>.</p>
<h2>All Your Attributes</h2>
<dl th:each="userAttribute : ${userAttributes}">
<dt th:text="${userAttribute.key}"></dt>
<dd th:text="${userAttribute.value}"></dd>
</dl>
<form th:action="@{/logout}" method="post">
<button id="logout" type="submit">Logout</button>
</form>
</body>
</html>
创建一个
src/main/resources/application.yml
文件来包含您的元数据 URI。
spring:
security:
saml2:
relyingparty:
registration:
assertingparty:
metadata-uri: <your-metadata-uri>
然后,将
build.gradle
更改为使用 thymeleaf-extras-springsecurity6
而不是 thymeleaf-extras-springsecurity5
并添加 Spring Security SAML 的依赖项:
implementation 'org.thymeleaf.extras:thymeleaf-extras-springsecurity6'
implementation 'org.springframework.security:spring-security-saml2-service-provider'
要从 Okta 获取元数据 URI,请登录您的帐户并转到 Applications > Create App Integration。选择SAML 2.0,然后单击下一步。将您的应用程序命名为
Spring Boot SAML
,然后单击 Next。
使用以下设置:
http://localhost:8080/login/saml2/sso/okta
http://localhost:8080/saml2/service-provider-metadata/okta
然后单击下一步。选择以下选项:
选择完成。
Okta 将创建您的应用程序,您将被重定向到其登录选项卡。向下滚动到 SAML 签名证书,然后转至 SHA-2 > 操作 > 查看 IdP 元数据。您可以右键单击并复制此菜单项的链接或打开其 URL。将生成的链接复制到剪贴板。它应该类似于以下内容:
https://dev-13337.okta.com/app/<random-characters>/sso/saml/metadata
转到应用程序的 Assignment 选项卡,并将访问权限分配给 Everyone 组。
将您的元数据 URI 粘贴到您的
application.yml
文件中。使用 ./gradlew bootRun
启动应用程序,然后在您喜欢的浏览器中打开 http://localhost:8080
。您应该被重定向到登录。
与奥克塔。问题发生。当打开 2 个选项卡时。 -第一步:第一个选项卡 -> 访问网络应用程序 -> 显示登录屏幕 -第二步:打开第二个选项卡 => 访问网络应用程序 -> 显示登录屏幕
请帮助如何修复