I use spring oauth2-authorization-server 1.2.2 ,but t can`t get token

23 Views Asked by At

I use spring oauth2-authorization-server 1.2.2 I call http://localhost:9000/oauth2/token?grant_type=client_credentials&scope=read&client_id=client_a&client_secret=secret by POST

It`s tell me Access Denied.

this is my configuration


@Configuration
@EnableWebSecurity
@Import(OAuth2AuthorizationServerConfiguration.class)
public class AuthorizationServerConfig {


    @Bean
    public SecurityFilterChain defaultSecurityFilterChain(HttpSecurity http)
            throws Exception {
        http
                .csrf(Customizer.withDefaults())
                .authorizeHttpRequests((authorize) -> authorize
                        .requestMatchers("/oauth2/**").permitAll()
                );


        return http.build();
    }
    @Bean
    public OAuth2AuthorizationService authorizationService() {
        return new InMemoryOAuth2AuthorizationService();
    }

    @Bean
    public RegisteredClientRepository registeredClientRepository() {
        RegisteredClient registeredClient = RegisteredClient.withId(UUID.randomUUID().toString())
                .clientId("client-a")
                .clientSecret("secret")
                .clientAuthenticationMethod(ClientAuthenticationMethod.CLIENT_SECRET_JWT)
                .authorizationGrantType(AuthorizationGrantType.CLIENT_CREDENTIALS)
                .redirectUri("http://127.0.0.1:8080/index")
                .scope("read")
                .clientSettings(ClientSettings.builder().requireAuthorizationConsent(true).build())
                .build();

        return new InMemoryRegisteredClientRepository(registeredClient);
    }


    @Bean
    public JWKSource<SecurityContext> jwkSource() {
        RSAKey rsaKey = Jwks.generateRsa();
        JWKSet jwkSet = new JWKSet(rsaKey);
        return (jwkSelector, securityContext) -> jwkSelector.select(jwkSet);
    }


    @Bean
    public JwtDecoder jwtDecoder(JWKSource<SecurityContext> jwkSource) {
        return OAuth2AuthorizationServerConfiguration.jwtDecoder(jwkSource);
    }

    @Bean
    public AuthorizationServerSettings authorizationServerSettings() {
        return AuthorizationServerSettings.builder().build();
    }
}

Is there something wrong with my configuration? I configured access permission.

1

There are 1 best solutions below

0
Chin Huang On

Spring Authorization Server rejects query parameters in the token request. The request must send the parameters in the body of a POST request.

Also, the registered client must set

.clientAuthenticationMethod(ClientAuthenticationMethod.CLIENT_SECRET_POST)

for Spring Authorization Server to authenticate the client_id and client_secret parameters in the POST body.