【问题标题】:How to secure apis with ResourceServer implementation with multiple resource ids?如何使用具有多个资源 ID 的 ResourceServer 实现来保护 api?
【发布时间】:2022-03-01 16:27:05
【问题描述】:

我有一个要求,我需要使用由不同资源服务器获取的不同令牌来保护我的应用程序中的不同 API 集。

/api/user/** -> APIs 应该使用 token(T1) 进行身份验证,从提供者 RS1 获得 /api/admin/** -> API 应该使用从提供者 RS2 获得的令牌(T2)进行身份验证

T1 应该无法访问 /api/admin/** 和 T2 应该无法访问 /api/user/**

两者 - RS1 和 RS2 都发出 JWT。

这怎么可能,使用 Spring 的资源服务器实现?任何指针表示赞赏。

【问题讨论】:

    标签: spring-boot spring-security


    【解决方案1】:

    我假设您的意思是说不同的令牌是从不同的授权服务器获得的。

    在这种情况下,您可以查看多租户应用程序。

    区分租户的一种方法是通过发行人声明。为此,您可以使用JwtIssuerAuthenticationManagerResolverJwtIssuerReactiveAuthenticationManagerResolver

    @Bean
    SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) {
        JwtIssuerReactiveAuthenticationManagerResolver authenticationManagerResolver = 
                new JwtIssuerReactiveAuthenticationManagerResolver(
                        "https://idp.example.org/issuerOne",
                        "https://idp.example.org/issuerTwo");
    
        http
                .authorizeExchange(exchanges -> exchanges
                        .anyExchange().authenticated()
                )
                .oauth2ResourceServer(oauth2 -> oauth2
                        .authenticationManagerResolver(authenticationManagerResolver)
                );
        return http.build();
    }
    

    这里有更多资源可以帮助您入门:

    【讨论】:

      猜你喜欢
      • 2019-08-14
      • 1970-01-01
      • 2023-03-25
      • 2014-11-29
      • 2016-02-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多