标签: spring-security-saml2

如何在 OpenSaml4AuthenticationProvider 中包含来自旧版 UserDetailsS​​ervice 的用户详细信息?

我希望有人能给我一个比我在文档中找到的更具体的例子。

使用 SpringBoot/Spring Security 5.6.0。我正在将基于 SpringSecurity/SAML 的身份验证过程迁移到SAML2

我需要将根据responseToken信息构建的UserDetails添加到身份验证中。就像我们在文档中可以读到的内容: https: //docs.spring.io/spring-security/reference/5.6.0-RC1/servlet/saml2/index.html#servlet-saml2login-opensamlauthenticationprovider-userdetailsservice

但我不明白第三点:返回包含用户详细信息的自定义身份验证:“return MySaml2Authentication(userDetails,authentication);”

在任何情况下,您都必须执行“return new MySaml2Authentication(userDetails,authentication);” 正确的?

无论如何,当该过程继续时,它会被执行:

身份验证authenticate =provider.authenticate(authentication);

我们可以看到,它用原始值替换了“详细信息”值。

authenticationResponse.setDetails(authentication.getDetails());

/**
 * @param authentication the authentication request object, must be of type
 * {@link Saml2AuthenticationToken}
 * @return {@link Saml2Authentication} if the assertion is valid
 * @throws AuthenticationException if a validation exception occurs
 */
@Override
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
    try {
        Saml2AuthenticationToken token = (Saml2AuthenticationToken) authentication;
        String serializedResponse = token.getSaml2Response();
        Response response …
Run Code Online (Sandbox Code Playgroud)

spring-security spring-boot spring-security-saml2

5
推荐指数
1
解决办法
877
查看次数

java.lang.ClassCastException:org.opensaml.core.xml.schema.impl.XSAnyBuilder 无法转换为 org.opensaml.xml.XMLObjectBuilder

在我们的应用程序中,我们尝试升级到 Spring boot 2,我们使用 spring-security-saml2-core:1.0.4.RELEASE,在运行应用程序时,我们遇到以下异常。看起来,classpaath 上有两个 jar xmltooling-1.4.6opensaml-core-3.3.0,它们具有相同的文件default-config.xml。在这种情况下,类加载器碰巧将opensaml-core-3.3.0放在xmltooling-1.4.6.jar 之前,并且default-config.xml加载错误

<ObjectProviders>
        <ObjectProvider qualifiedName="xt:DEFAULT">
        <BuilderClass className="org.opensaml.core.xml.schema.impl.XSAnyBuilder"/>
        <MarshallingClass className="org.opensaml.core.xml.schema.impl.XSAnyMarshaller"/>
        <UnmarshallingClass className="org.opensaml.core.xml.schema.impl.XSAnyUnmarshaller"/>
    </ObjectProvider>  </ObjectProviders> 

java.lang.ClassCastException: org.opensaml.core.xml.schema.impl.XSAnyBuilder cannot be cast to org.opensaml.xml.XMLObjectBuilder
        at org.opensaml.xml.XMLConfigurator.initializeObjectProviders(XMLConfigurator.java:236)
        at org.opensaml.xml.XMLConfigurator.load(XMLConfigurator.java:182)
        at org.opensaml.xml.XMLConfigurator.load(XMLConfigurator.java:166)
        at org.opensaml.xml.XMLConfigurator.load(XMLConfigurator.java:143)
        at org.opensaml.DefaultBootstrap.initializeXMLTooling(DefaultBootstrap.java:203)
        at org.opensaml.DefaultBootstrap.initializeXMLTooling(DefaultBootstrap.java:186)
        at org.opensaml.DefaultBootstrap.bootstrap(DefaultBootstrap.java:92)
        at org.opensaml.PaosBootstrap.bootstrap(PaosBootstrap.java:27)
        at org.springframework.security.saml.SAMLBootstrap.postProcessBeanFactory(SAMLBootstrap.java:42).
Run Code Online (Sandbox Code Playgroud)

有人可以帮忙吗?请..

opensaml spring-boot spring-security-saml2

4
推荐指数
1
解决办法
1万
查看次数

Spring Saml2 和 Spring Session - 未检索到 SavedRequest(身份验证/InResponseTo 异常后无法重定向到请求的页面)

我正在尝试使用 Spring Boot SAML2 + Spring Session 来保护我的 Web 应用程序(将部署在 K8S 上)。没有 spring-session-data-rest 或 spring-session-hazelcast 一切都很好。它可以通过 Okta 进行身份验证,并在身份验证后重定向回所请求的页面。另外,我可以使用 opensaml3 或 opensaml4。

但是,当我尝试使用 spring-session-data-rest 或 spring-session-hazelcast (只有 1 个实例,还没有集群)时,它不会重定向回请求的页面。此外,opensaml4 也会失败,并出现异常:“响应包含 InResponseTo 属性 [],但未找到已保存的身份验证请求”。有人提到 opensaml3 即将停产,所以我想让它与 opensaml4 一起使用。

这是一个示例应用程序来演示我的案例https://github.com/simonckw/redis-saml2/tree/redis。我错过了什么吗?有人有这个设置的工作样本吗?非常感谢您的帮助。

ps 我已经追踪到 HttpSessionRequestCache.java,从 SavedRequestAwareWarpper.java 调用。如果没有 spring-session-data-rest 或 spring-session-hazelcast,可以检索保存的请求,但在启用 spring-session-data-rest 或 spring-session-hazelcast 时则无法检索。在我看来,InResponseTo 异常也可能与之相关。我的 Redis 设置应该没问题。这是写入 Redis 的会话数据:

  1. “弹簧:会话:会话:过期:7c1858d1-0ea7-4a7a-8523-2abf89137771”
  2. “春天:会话:过期:1654439580000”
  3. “弹簧:会话:会话:过期:58a584d3-625e-4e0a-bef5-3aaff485ad93”
  4. “弹簧:会话:索引:org.springframework.session.FindByIndexNameSessionRepository.PRINCIPAL_NAME_INDEX_NAME:abc@xyz.com”
  5. “春天:会话:会话:7c1858d1-0ea7-4a7a-8523-2abf89137771”
  6. “春天:会话:会话:58a584d3-625e-4e0a-bef5-3aaff485ad93”

127.0.0.1:6379> hkeys 弹簧:会话:会话:7c1858d1-0ea7-4a7a-8523-2abf89137771

  1. “sessionAttr:SPRING_SECURITY_SAVED_REQUEST”
  2. “上次访问时间”
  3. “最大非活动间隔”
  4. “创作时间”
  5. “sessionAttr:org.springframework.security.saml2.provider.service.web.HttpSessionSaml2AuthenticationRequestRepository.SAML2_AUTHN_REQUEST”

127.0.0.1:6379> hkeys 弹簧:会话:会话:58a584d3-625e-4e0a-bef5-3aaff485ad93

  1. “最大非活动间隔”
  2. “创作时间”
  3. “上次访问时间”
  4. “sessionAttr:SPRING_SECURITY_CONTEXT”
  5. “sessionAttr:SPRING_SECURITY_LAST_EXCEPTION”

spring-security opensaml spring-saml spring-session spring-security-saml2

3
推荐指数
1
解决办法
4297
查看次数

使用 Spring Security 将数据库和 SAML 身份验证合并到一个应用程序中

我正在尝试使用 spring security(spring-security-starter) 在 spring boot(2.2.4) 应用程序中实现身份验证和授权。

用例:根据用户名,我想将用户重定向到特定的身份验证提供商

  • 如果用户名以“mit.com”结尾,则使用数据库对用户进行身份验证(我正在使用hibernate)-为此,我可以使用spring的UserDetailService
  • 如果用户名以“einfochips.com”结尾,则使用 SAML 2.0 协议对用户进行身份验证 - 使用 Okta、SSOCircle、OneLogin 等身份提供商。

    我不知道我该怎么做。我尝试使用自定义过滤器但无法做到。

我已经阅读了很多文章但无法实现这一目标。

我编写了以下仅使用 SAML 进行身份验证的代码。它运行良好。将用户带到 okta idp 进行登录。

package com.example.demo;

import static org.springframework.security.extensions.saml2.config.SAMLConfigurer.saml;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.saml.userdetails.SAMLUserDetailsService;

@EnableWebSecurity
@Configuration
@EnableGlobalMethodSecurity(securedEnabled = true)
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {

    @Autowired
    SAMLUserDetailsService userDetailsService;

    @Value("${security.saml2.metadata-url}")
    String metadataUrl;

    @Value("${server.ssl.key-alias}")
    String keyAlias;

    @Value("${server.ssl.key-store-password}")
    String password;

    @Value("${server.port}")
    String port;

    @Value("${server.ssl.key-store}")
    String …
Run Code Online (Sandbox Code Playgroud)

spring-security saml saml-2.0 spring-boot spring-security-saml2

2
推荐指数
1
解决办法
2281
查看次数

自定义“RelyingPartyRegistrationRepository”实现

看起来Spring总是用来InMemoryRelyingPartyRegistrationRepository返回RelyingPartyRegistrationRepository类型化的bean,请参阅https://github.com/spring-projects/spring-boot/blob/master/spring-boot-project/spring-boot-autoconfigure/src/main/ java/org/springframework/boot/autoconfigure/security/saml2/Saml2RelyingPartyRegistrationConfiguration.java

问题:我如何注入(自动装配)我自己的实现RelyingPartyRegistrationRepository?假设我希望在更新某个客户的 SAML 配置后,允许自动连接依赖方存储库从数据库自动重新加载。这可行吗?

java spring-security saml saml-2.0 spring-security-saml2

2
推荐指数
1
解决办法
1945
查看次数

Spring 启动和 Okta SAML2

我喜欢使用 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);..}
Run Code Online (Sandbox Code Playgroud)

无法解析传入的请求,但我不确定它是否相关。我的 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
Run Code Online (Sandbox Code Playgroud)

我的 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
Run Code Online (Sandbox Code Playgroud)

我还提供了 saml 身份验证的端点:

@RequestMapping(SsoAuthenticationController.BASE_NAME)
public interface SsoAuthenticationController { …
Run Code Online (Sandbox Code Playgroud)

okta spring-security-saml2

2
推荐指数
1
解决办法
1906
查看次数