Spring OAuth2 的安全性和 Github 仅允许组织中的开发人员使用某些 API

Spring security with OAuth2 and Github to only allow developers in an organization to use certain APIs

我有一组 api,我只希望 github 的组织中的开发人员使用。为了对用户进行身份验证,我使用了 github 的 OAuth。但是将 Spring security 与 oauth 一起使用似乎并不能做到这一点,因为它允许不在组织中的开发人员使用它。我该怎么做?

看起来这个 spring.io 教程有一个针对这个确切用例的示例:

@Bean
public OAuth2UserService<OAuth2UserRequest, OAuth2User> oauth2UserService(WebClient rest) {
    DefaultOAuth2UserService delegate = new DefaultOAuth2UserService();
    return request -> {
        OAuth2User user = delegate.loadUser(request);
        if (!"github".equals(request.getClientRegistration().getRegistrationId())) {
            return user;
        }

        OAuth2AuthorizedClient client = new OAuth2AuthorizedClient
                (request.getClientRegistration(), user.getName(), request.getAccessToken());
        String url = user.getAttribute("organizations_url");
        List<Map<String, Object>> orgs = rest
                .get().uri(url)
                .attributes(oauth2AuthorizedClient(client))
                .retrieve()
                .bodyToMono(List.class)
                .block();

        if (orgs.stream().anyMatch(org -> "spring-projects".equals(org.get("login")))) {
            return user;
        }

        throw new OAuth2AuthenticationException(new OAuth2Error("invalid_token", "Not in Spring Team", ""));
    };
}