Spring Security 5 и jwk-set-uri с аутентификацией - PullRequest
1 голос
/ 26 марта 2020

Ну, у меня есть URL-адрес для получения ключей * publi c, но для этого URL-адреса требуется токен на предъявителя, поэтому у меня есть следующее в моем приложении. :

@Override
protected void configure(HttpSecurity http) throws Exception {
    http.authorizeRequests(authorizeRequests ->
        authorizeRequests
                .antMatchers("/customers/**").authenticated()
                .anyRequest().anonymous()
    ).oauth2ResourceServer(OAuth2ResourceServerConfigurer::jwt);
}

Но когда я пытаюсь сделать запрос, я получаю следующую ошибку:

org.springframework.security.oauth2.core.OAuth2AuthenticationException: An error occurred while attempting to decode the Jwt: Signed JWT rejected: Another algorithm expected, or no matching key(s) found

И мой pom. xml:

  <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-security</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-oauth2-resource-server</artifactId>
    </dependency>
    <dependency>
        <groupId>com.nimbusds</groupId>
        <artifactId>oauth2-oidc-sdk</artifactId>
        <version>7.3</version>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-oauth2-jose</artifactId>
    </dependency>

Я действительно не знаю, происходит ли эта ошибка, потому что мой URI "keys.jwt" требует некоторой аутентификации или другой причины.

1 Ответ

1 голос
/ 26 марта 2020

Проблема была решена с помощью:

 @Bean
    public NimbusJwtDecoder nimbusJwtDecoder(){
        RestTemplate rest = new RestTemplate();
        rest.getInterceptors().add((request, body, execution) -> {
            request.getHeaders().setBearerAuth(myJwt);
            return execution.execute(request, body);
        });
        return NimbusJwtDecoder.withJwkSetUri(jwkUri)
                .restOperations(rest).build();
    }
...