【问题标题】:Combine Saml2 and oAuth2 in the same authentication module将 Saml2 和 oAuth2 组合在同一个身份验证模块中
【发布时间】:2021-05-26 15:34:58
【问题描述】:

我在 spring-boot 中开发了一个基于 spring-security 的身份验证模块,它允许通过 oAuth2 对 AAD、ADFS 等外部系统进行用户身份验证 ...

一切正常,但新客户端请求使用 Saml2 作为集成协议。

目前该模块由以下部分组成

SecurityConfig.java

@Override
protected void configure(HttpSecurity http) throws Exception {
    http
            .cors()
            .and()
            .sessionManagement()
            .sessionCreationPolicy(SessionCreationPolicy.STATELESS)
            .and()
            .csrf()
            .disable()
            .formLogin()
            .disable()
            .httpBasic()
            .disable()
            .exceptionHandling()
            .authenticationEntryPoint(new RestAuthenticationEntryPoint())
            .and()
            // only allow access to specified URIs
            .authorizeRequests()
            .antMatchers("/auth/**", "/oauth2/**", "/public/**")
            .permitAll()
            // only allow access with fully authenticated requests
            .anyRequest()
            .fullyAuthenticated()
            .and()
            // configure OAuth2 login
            .oauth2Login()
            // configure token endpoint for hack
            .tokenEndpoint()
            .accessTokenResponseClient(getAccessTokenResponseClient())
            .and()
            // endpoint for authorization (the endpoint we expose and knows the third party to go to)
            .authorizationEndpoint()
            .baseUri(OAUTH2_AUTHORIZE_BASE_URI)
            .authorizationRequestResolver(oauth2AuthorizationRequestResolver)
            .authorizationRequestRepository(httpCookieOAuth2AuthorizationRequestRepository)
            .and()
            // endpoint for callback (where the third party service calls back after authenticating a user)
            .redirectionEndpoint()
            .baseUri("/oauth2/callback/*")
            .and()
            // the service to use
            .userInfoEndpoint()
            .userService(customOAuth2UserService)
            .and()
            .successHandler(oAuth2AuthenticationSuccessHandler)
            .failureHandler(oAuth2AuthenticationFailureHandler);

    // Add our custom Token based authentication filter
    http.addFilterBefore(tokenAuthenticationFilter, UsernamePasswordAuthenticationFilter.class);
}

Application.yaml

spring:
  security:
    oauth2:
      client:
        registration:
          example1:
            clientId: -----------------
            clientSecret: -----------------
            redirectUriTemplate: -----------------
            grant-type: authorization_code
            authorizationGrantType: authorization_code
            tokenName: code
            authenticationScheme: query
            clientAuthenticationScheme: form
          example2:
            clientId: -----------------
            clientSecret: -----------------
            tenant-id: -----------------
            active-directory-groups: -----------------
            redirectUriTemplate: -----------------
            grant-type: authorization_code
            authorizationGrantType: authorization_code
            tokenName: code
            authenticationScheme: query
            clientAuthenticationScheme: form

我对与 Saml2 集成的疑问如下:

  1. 可以在同一个应用程序中结合两种身份验证,您可以在 Application.yaml 中提供类似的内容吗?
spring:
  security:
    saml2:
      relyingparty:
        registration:
          aad: 
            identityprovider:
              entity-id: -----------------
              verification.credentials:
                - certificate-location: "classpath:certs/aad.cert"
              singlesignon.url: -----------------
              singlesignon.sign-request: false
          okta:
            identityprovider:
              entity-id: -----------------
              verification.credentials:
                - certificate-location: "classpath:certs/okta.cert"
              singlesignon.url: -----------------
              singlesignon.sign-request: false
    oauth2:
      client:
        registration:
          example1:
            clientId: -----------------
            clientSecret: -----------------
            redirectUriTemplate: -----------------
            grant-type: authorization_code
            authorizationGrantType: authorization_code
            tokenName: code
            authenticationScheme: query
            clientAuthenticationScheme: form
          example2:
            clientId: -----------------
            clientSecret: -----------------
            tenant-id: -----------------
            active-directory-groups: -----------------
            redirectUriTemplate: -----------------
            grant-type: authorization_code
            authorizationGrantType: authorization_code
            tokenName: code
            authenticationScheme: query
            clientAuthenticationScheme: form
  1. 如果前面的配置是可能的,在“SecurityConfig.java -> configure (HttpSecurity http)”中会如何表示呢?是否可以在当前配置中输入 saml2Login?

  2. 我见过一些不完整的例子,他们谈论使用“authenticationProvider”来实现这种类型的案例。有人知道这是否有效吗?

public void configure(AuthenticationManagerBuilder auth) throws Exception {
          auth.authenticationProvider(oauth2AuthenticationProvider());
          auth.authenticationProvider(saml2AuthenticationProvider());
          auth.authenticationProvider(DDBBAuthenticationProvider());
}

感谢您的帮助!

【问题讨论】:

    标签: spring-boot spring-security oauth-2.0 saml-2.0


    【解决方案1】:
    1. for 的关键在于,您的应用不需要了解有关 SAML 的任何信息,因为它是一项旧技术。您将希望您的 UI 和 API 是现代的并使用 OAuth 令牌。

    2. 无法回答这个问题。

    3. 听起来您应该使用授权服务器 (AS),而不是在自己的身份验证模块中做太多事情。第三方系统已经内置了对许多提供商的支持,并且已经花费了数年时间来开发。

    作为示例,请参阅 Curity 产品支持的 all of these options,该产品有一个免费的社区版本供您下载。

    就一般模式而言:

    • 您的应用使用 OAuth 和 OpenID Connect 并与 AS 交互
    • 如果业务合作伙伴想要针对他们自己的身份提供者使用 SAML 登录,您只需在 AS 中进行 SAML 配置更改 - 对您的应用程序进行零代码更改

    【讨论】:

      猜你喜欢
      • 2017-07-30
      • 2018-05-15
      • 1970-01-01
      • 1970-01-01
      • 2021-07-29
      • 2012-10-23
      • 2020-07-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多