【问题标题】:Disable redirect to oauth2/authorization/{registrationId} in oauth2 client flow in Spring Cloud Gateway在 Spring Cloud Gateway 的 oauth2 客户端流中禁用重定向到 oauth2/authorization/{registrationId}
【发布时间】:2021-03-16 02:42:52
【问题描述】:

是否可以在 oauth2 客户端流程中禁用重定向到 oauth2/authorization/{registrationId}? 我在 Spring Cloud Gateway 中有以下 oauth2 流的属性,但我没有指定 url oauth2/authorization/{registrationId}:

  security:
    oauth2:
      client:
        registration:
          smart_hub_client:
            provider: wso2is
            client-id: someid
            client-secret: somesecret
            authorization-grant-type: authorization_code
            redirect-uri: "{baseUrl}/redirect_uri"
            scope: sso,openid
        provider:
          wso2is:
            authorization-uri: https://authserver/oauth2/authorize?loginPage=login.jsp
            token-uri: https://authserver.com/oauth2/token
            user-info-uri: https://authserver/oauth2/userinfo
            user-name-attribute: sub
            jwk-set-uri: https://authserver.com/oauth2/jwks

屏幕截图中的请求 URL 在这里:https://myscgapp/oauth2/authorization/smart_hub_client

更新: 我已将上面的 conf 更新为我的示例。 主要问题 - 我有重定向循环。也许禁用 https://myscgapp/oauth2/authorization/smart_hub_client 可以提供帮助?还是根本原因是另一个?

我有这样的重定向循环:

【问题讨论】:

    标签: spring-security oauth-2.0 spring-security-oauth2 spring-cloud-gateway spring-boot-starter-oauth2-client


    【解决方案1】:

    OAuth2AuthorizationRequestRedirectFilter 使用 OAuth2AuthorizationRequestResolver 通过将最终用户的用户代理重定向到授权服务器的授权端点来启动授权代码授权流程。
    默认实现 DefaultOAuth2AuthorizationRequestResolver 匹配(默认)路径 /oauth2/authorization/{registrationId}。

    您可以通过提供自定义 ServerOAuth2AuthorizationRequestResolver 来自定义此设置。

    在下面的示例中,解析器将匹配路径 /auth/custom/sso/{registrationId} 而不是 /oauth2/authorization/{registrationId}。

    @EnableWebFluxSecurity
    public class SecurityConfig {
    
        @Autowired
        private ReactiveClientRegistrationRepository clientRegistrationRepository;
    
        @Bean
        SecurityWebFilterChain configure(ServerHttpSecurity http) {
            http
                .authorizeExchange(exchanges ->
                    exchanges
                        .anyExchange().authenticated()
                )
                .oauth2Login(oauth2Login ->
                    oauth2Login
                        .authorizationRequestResolver(getAuthorizationRequestResolver()));
            return http.build();
        }
    
        private ServerOAuth2AuthorizationRequestResolver getAuthorizationRequestResolver() {
            return new DefaultServerOAuth2AuthorizationRequestResolver(
                    this.clientRegistrationRepository,
                    new PathPatternParserServerWebExchangeMatcher(
                            "/auth/custom/sso/{registrationId}"));
    
        }
    }
    

    【讨论】:

    • 感谢您的回答!根据我的屏幕,我有 3 个身份验证请求:第一个:smart_hub_client - 指向我的 SCG 应用程序本身 myscgapp/oauth2/authorization/smart_hub_client,第二个:请求身份验证代码 authserver/oauth2/…...... 第三个请求将身份验证代码交换为令牌:@ 987654323@..... 问题:如果我们已经在第二个请求中请求了验证码,为什么我们需要第一个请求 myscgapp/oauth2/authorization/smart_hub_client
    • 我有重定向循环的主要问题。而且我认为这个 myscgapp/oauth2/authorization/smart_hub_client 的问题,也许我错了。
    • 对“/oauth2/authorization/smart_hub_client”的请求是发起授权请求并最终启动授权码授权流程的原因。
    • 对“/oauth2/authorization/smart_hub_client”的请求可能不是导致重定向循环的原因。我会看看你更新的问题并分享一些建议。
    • Eleftheria Stein-Kousathana 感谢您保持联系并帮助解决我的问题。这对我来说非常重要。我试过你的变种 - 它不起作用。因为我的身份验证服务器等待“{baseUrl}/redirect_uri”。我得到了不同的错误-无效的回调。你有什么想法吗?如何解决?感谢您保持联系并帮助解决我的问题。这对我来说非常重要。我试过你的变种 - 它不起作用。因为我的身份验证服务器等待“{baseUrl}/redirect_uri”。我得到了不同的错误-无效的回调。你有什么想法吗?如何解决?
    猜你喜欢
    • 2020-07-04
    • 2022-11-08
    • 1970-01-01
    • 2020-10-04
    • 2021-10-20
    • 1970-01-01
    • 2019-01-09
    • 2023-04-09
    • 2012-10-22
    相关资源
    最近更新 更多