如何在承载请求令牌之后使用SpringSecurityOAuth2客户机获取用户?

mcvgt66p  于 2021-06-30  发布在  Java
关注(0)|答案(0)|浏览(188)

我将转向新的Spring Security ,我的主要授权提供者将是awscognito,但是我可能会收到一些不同的令牌,我需要在另一个提供者中验证它们。
所以我的流程是:
最终用户->使用承载令牌的请求->受保护的资源
受保护的资源->委托承载令牌->“假身份验证服务器”
“假身份验证服务器”->发现令牌是否来自cognito或其他东西->验证用户
我想在“假身份验证服务器”添加一些自定义信息到我的身份验证用户,例如,该用户有权访问的公司和我的“受保护的资源”在继续返回请求的资源之前,我会收到这些信息。
我创建了一个customoauthuserservice:

private OAuth2UserService<OAuth2UserRequest, OAuth2User> customOAuth2UserService() {
        return new CustomOAuth2UserService();
    }

    private class CustomOAuth2UserService implements OAuth2UserService<OAuth2UserRequest, OAuth2User> {

        @Override
        public OAuth2User loadUser(OAuth2UserRequest userRequest) throws OAuth2AuthenticationException {
            System.out.println("-->  In loadUser");
            return new DefaultOAuth2User(Collections.emptyList(), Collections.emptyMap(), "");
        }
    }

并注册为我的用户服务:

http
            .authorizeRequests()
                .anyRequest().authenticated()
                .and()
                .oauth2Login()
                .userInfoEndpoint()
                .userService(this.customOAuth2UserService());

保安总是给我打电话 authorization-uri ,我不确定,但我认为正确的方法是调用我的用户信息uri。
我的WebClient配置是这样设置的:

@Configuration
public class WebClientConfig {

    @Bean
    WebClient webClient(OAuth2AuthorizedClientManager authorizedClientManager) {
        ServletOAuth2AuthorizedClientExchangeFilterFunction oauth2Client =
                new ServletOAuth2AuthorizedClientExchangeFilterFunction(authorizedClientManager);
        return WebClient.builder()
                .apply(oauth2Client.oauth2Configuration())
                .build();
    }

    @Bean
    OAuth2AuthorizedClientManager authorizedClientManager(ClientRegistrationRepository clientRegistrationRepository,
                                                          OAuth2AuthorizedClientRepository authorizedClientRepository) {
        OAuth2AuthorizedClientProvider authorizedClientProvider =
                OAuth2AuthorizedClientProviderBuilder.builder()
                        .authorizationCode()
                        .refreshToken()
                        .clientCredentials()
                        .password()
                        .build();
        DefaultOAuth2AuthorizedClientManager authorizedClientManager = new DefaultOAuth2AuthorizedClientManager(
                clientRegistrationRepository, authorizedClientRepository);
        authorizedClientManager.setAuthorizedClientProvider(authorizedClientProvider);

        // For the `password` grant, the `username` and `password` are supplied via request parameters,
        // so map it to `OAuth2AuthorizationContext.getAttributes()`.
        authorizedClientManager.setContextAttributesMapper(contextAttributesMapper());

        return authorizedClientManager;
    }

    private Function<OAuth2AuthorizeRequest, Map<String, Object>> contextAttributesMapper() {
        return authorizeRequest -> {
            Map<String, Object> contextAttributes = Collections.emptyMap();
            HttpServletRequest servletRequest = authorizeRequest.getAttribute(HttpServletRequest.class.getName());
            String username = servletRequest.getParameter(OAuth2ParameterNames.USERNAME);
            String password = servletRequest.getParameter(OAuth2ParameterNames.PASSWORD);
            if (StringUtils.hasText(username) && StringUtils.hasText(password)) {
                contextAttributes = new HashMap<>();

                // `PasswordOAuth2AuthorizedClientProvider` requires both attributes
                contextAttributes.put(OAuth2AuthorizationContext.USERNAME_ATTRIBUTE_NAME, username);
                contextAttributes.put(OAuth2AuthorizationContext.PASSWORD_ATTRIBUTE_NAME, password);
            }
            return contextAttributes;
        };
    }

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题