FeignClient 使用 application/x-www-form-urlencoded body 创建 POST

MBo*_*zic 2 java spring-boot spring-cloud-feign feign

我正在尝试向带有application/x-www-form-urlencoded值的主体的 keycloak REST API 端点发出 POST 请求。

这是一个请求的 cURL:

curl -X POST \
  https://auth.beyondtime-stage.io/auth/realms/master/protocol/openid-connect/token \
  -H 'cache-control: no-cache' \
  -H 'content-type: application/x-www-form-urlencoded' \
  -d 'username=admin&password=pass123&client_id=admin-cli&grant_type=password'
Run Code Online (Sandbox Code Playgroud)

令我失望的是,我还没有找到用假装做到这一点的方法。

这是我尝试过的一个例子:

@FeignClient(name = "beyondtime-io-kc-client", url = "${btio.keycloakUrl}", path = "/")
public interface KeycloakAdminClient {

    @PostMapping(name="realms/master/protocol/openid-connect/token", consumes = "application/x-www-form-urlencoded")
    String getAuthToken(@RequestParam("username") String username,
                        @RequestParam("password") String password,
                        @RequestParam("client_id") String clientId,
                        @RequestParam("grant_type") String grantType);

}
Run Code Online (Sandbox Code Playgroud)

在我的 pom.xml 中,我使用:

spring-cloud-starter-openfeign 2.1.1.RELEASE

我还尝试添加feign -form依赖项和@Param而不是@RequestParam,但没有成功。

MBo*_*zic 5

我在这里找到了解决方案How to POST form-url-encoded data with Spring Cloud Feign

Feign客户端:

import com.beyondtime.recruitmentservice.seafarer.entity.AuthTokenRequest;
import com.beyondtime.recruitmentservice.seafarer.entity.KeycloakAccessToken;
import feign.codec.Encoder;
import feign.form.spring.SpringFormEncoder;
import org.springframework.beans.factory.ObjectFactory;
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.cloud.openfeign.support.SpringEncoder;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@FeignClient(name = "beyondtime-io-kc-client", url = "${btio.keycloakUrl}", path = "/", configuration = KeycloakAdminClient.Configuration.class)
public interface KeycloakAdminClient {

    @PostMapping(value="realms/master/protocol/openid-connect/token", consumes = "application/x-www-form-urlencoded")
    KeycloakAccessToken getAuthToken(@RequestBody AuthTokenRequest authTokenRequest);

    class Configuration {
        @Bean
        Encoder feignFormEncoder(ObjectFactory<HttpMessageConverters> converters) {
            return new SpringFormEncoder(new SpringEncoder(converters));
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

请求对象:

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class AuthTokenRequest {
    private String username;

    private String password;

    private String client_id;

    private String grant_type;

}
Run Code Online (Sandbox Code Playgroud)

响应对象:

@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
public class KeycloakAccessToken {
    @JsonProperty("access_token")
    private String accessToken;
}
Run Code Online (Sandbox Code Playgroud)