Spring Boot OAuth2 / Cognito: получить пользовательскую информацию от AccessToken - PullRequest
0 голосов
/ 08 апреля 2020

У меня есть сервер ресурсов весенней загрузки, который аутентифицирует пользователя с помощью accessToken, извлеченного из Cook ie. AccessToken извлекается из Cognito UserPool в реагирующем FE и записывается повару ie. Кажется, Spring удалось аутентифицировать пользователя, и я вижу имя пользователя в SecurityContextHolder.getContext().authentication.name. Мне нужно получить остальные атрибуты пользователя, такие как электронная почта. Большинство решений, которые я посмотрел, говорят, что SecurityContextHolder.getContext().authentication.principal должен содержать все необходимые мне атрибуты. В моем случае это строка, и я не могу привести ее к какому-либо объекту User. Даже SecurityContextHolder.getContext().authentication.details равно нулю. Я определил user-info-uri в свойствах своего приложения security.oauth2.resource.user-info-uri. Я чувствую, что пропустил что-то, что приводит к отсутствию пользовательских атрибутов в контексте аутентификации.

Это моя конфигурация безопасности сервера ресурсов:

@Configuration
@EnableResourceServer
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SpringSecurityConfig extends ResourceServerConfigurerAdapter {

    private final ResourceServerProperties resource;

    public SpringSecurityConfig(ResourceServerProperties resource) {
        this.resource = resource;
    }

    @Override
    public void configure(ResourceServerSecurityConfigurer resources) throws Exception {
        resources.tokenExtractor(new CustomExtractor());
    }

    @Override
    public void configure(HttpSecurity http) throws Exception {

        http
                .csrf().disable()
                .authorizeRequests();
    }

    // Enabling Cognito Converter
    @Bean
    public TokenStore jwkTokenStore() {
        return new JwkTokenStore(
                Collections.singletonList(resource.getJwk().getKeySetUri()),
                new CognitoAccessTokenConverter(),
                null);
    }

}
...