如何在没有 Issuer-uri 的情况下创建 OAuth2 Spring 配置?

问题描述 投票:0回答:1

我正在尝试使用 Spring Java 应用程序中的 OAuth2 向 Jobber 进行身份验证。 我根据他们的文档在 application.yml 中有这些设置https://developer.getjobber.com/docs/building_your_app/app_authorization

security:
    oauth2:
      client:
        provider:
          oidc:
            authorization-uri:  https://api.getjobber.com/api/oauth/authorize
            token-uri:  https://api.getjobber.com/api/oauth/token
        registration:
          oidc:
            client-id: xxx
            client-secret: xxx
            authorization-grant-type: authorization_code
            scope: clients, invoices, users, offline_access # last one for refresh tokens
            redirect-uri: http://localhost:9000/login/oauth2/code/oidc

我正在修改与“issuer-uri”一起使用的代码,该代码在 .well-known/openid-configuration 响应中提供这些参数,但我的尝试是基于 spring 文档:https://docs.spring。 io/spring-security/reference/servlet/oauth2/login/core.html,编辑安全链:

.and()
            .oauth2Login() //modify the oauth2 parameters to handle the jobber config
            .oauth2ResourceServer()
                .jwt()
                .jwtAuthenticationConverter(authenticationConverter())
                .and()
            .and()
                .oauth2Client();

.and()
            .oauth2Login(  oauth2LoginCustomizer()) //modify the oauth2 parameters to handle the jobber config
            .oauth2ResourceServer()
                .jwt()
                .jwtAuthenticationConverter(authenticationConverter())
                .and()
            .and()
                .oauth2Client();

...

 Customizer<OAuth2LoginConfigurer<HttpSecurity>> oauth2LoginCustomizer(){
        return (oauth2Login) -> oauth2Login
            .userInfoEndpoint(userInfo -> userInfo
                .userAuthoritiesMapper(userAuthoritiesMapper())
            );
        }

并删除使用颁发者-uri 的方法:

@Bean
    JwtDecoder jwtDecoder(ClientRegistrationRepository clientRegistrationRepository, RestTemplateBuilder restTemplateBuilder) {
        NimbusJwtDecoder jwtDecoder = JwtDecoders.fromOidcIssuerLocation(issuerUri);

        OAuth2TokenValidator<Jwt> audienceValidator = new AudienceValidator(jHipsterProperties.getSecurity().getOauth2().getAudience());
        OAuth2TokenValidator<Jwt> withIssuer = JwtValidators.createDefaultWithIssuer(issuerUri);
        OAuth2TokenValidator<Jwt> withAudience = new DelegatingOAuth2TokenValidator<>(withIssuer, audienceValidator);

        jwtDecoder.setJwtValidator(withAudience);
        jwtDecoder.setClaimSetConverter(
            new CustomClaimConverter(clientRegistrationRepository.findByRegistrationId("oidc"), restTemplateBuilder.build())
        );

        return jwtDecoder;
    }

例外:

Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.security.web.SecurityFilterChain]: Factory method 'filterChain' threw exception; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.security.oauth2.jwt.JwtDecoder' available

如何修改配置以仅使用 Jobber 提供的参数?

spring security spring-oauth2
1个回答
0
投票

如何在没有 Issuer-uri 的情况下创建 OAuth2 Spring 配置?

答案:

  • OAuth2 客户端:你不需要它
  • 资源服务器:您提供 JWK-set URI

但是,我没有找到

getjobber
的发行者或 JWK-set URI 的引用。他们很可能为自己的资源服务器保留授权服务器。请理解,您将无法有效地配置自己的资源服务器来使用
getjobber
颁发的不记名令牌来授权请求:您需要一个自己的授权服务器

您可以选择本地解决方案(Keycloak 是一个著名的解决方案)、云产品(如 Auth0、Amazon Cognito 等),或者选择使用 spring-authorization-server“自己动手”。

使用授权服务器配置资源服务器后有两个选项:

  • 对 getjobber 的所有 API 调用都使用相同的服务帐户发送(好消息,似乎是这样的):将 OAuth2 客户端配置更改为
    authorization-grant-type: client_credentials
    (不是
    authorization_code
    )。如果您的应用程序只是一个 REST API,还请从资源服务器安全过滤器链中删除与登录相关的所有内容(登录不是资源服务器的责任)。
  • 必须代表每个用户发送对 getjobber 的 API 调用。然后是两个子选项:
    • 您的 OAuth2 客户端是使用能够处理客户端多租户的框架编写的:然后让您的用户针对您的授权服务器和 getjobber 进行身份验证,并使用正确的访问令牌向每个资源服务器发送请求
    • 您的 OAuth2 客户端是用 Spring 编写的(Thymeleaf 页面或
      spring-cloud-gateway
      ),那么您会因为 这个问题而遇到麻烦(请在那里投票并详细说明您的用例,以增加 Spring Security 团队考虑的机会有一天)。在这种情况下,我建议您查看 “my” starter,它配置了备用
      OAuth2AuthorizedClientRepository
      ,支持同一用户会话中的多个身份(由 getjobber 颁发的身份和您自己的授权服务器颁发的身份)。
© www.soinside.com 2019 - 2024. All rights reserved.