首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >FeignClient用application/x-www-form-urlencoded创建POST

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

Stack Overflow用户
提问于 2020-05-19 21:51:50
回答 2查看 7.6K关注 0票数 3

我试图使用带有application/x form-urlencoded值的主体向keycloak REST端点发出POST请求。

下面是请求的cURL:

代码语言:javascript
复制
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'

令我失望的是,我还没有找到装模作样的办法。

下面是我尝试过的一个例子:

代码语言:javascript
复制
@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);

}

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

弹簧-云-启动器-启示牌2.1.1

我也尝试过添加假装形式的依赖项和@Param,而不是@RequestParam,但没有成功。

EN

回答 2

Stack Overflow用户

发布于 2020-05-20 20:43:53

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

冒充客户:

代码语言:javascript
复制
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));
        }
    }
}

请求对象:

代码语言:javascript
复制
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class AuthTokenRequest {
    private String username;

    private String password;

    private String client_id;

    private String grant_type;

}

响应对象:

代码语言:javascript
复制
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
public class KeycloakAccessToken {
    @JsonProperty("access_token")
    private String accessToken;
}
票数 6
EN

Stack Overflow用户

发布于 2022-04-09 11:34:29

有时,这些变通方法/黑客或实际实现(如库本身所定义的)比所需的复杂和繁琐。

在这种情况下,我认为只需自己编码身体,就会更容易、更快。

代码语言:javascript
复制
@FeignClient(name = "exampleClient", url = "https://example.com")
interface ExampleClient {

    @PostMapping(
        path = ["/oauth2/token"],
        headers = ["Content-Type: application/x-www-form-urlencoded"],
        consumes = [MediaType.APPLICATION_FORM_URLENCODED_VALUE],
    )
    fun loginWithCode(@RequestBody request: String): LoginResponse
}
代码语言:javascript
复制
val urlEncodedRequest = { code: String ->
    mapOf(
        "grant_type" to "authorization_code",
        "client_secret" to "S3CR3T",
        "client_id" to "0DFD0SF75F67DSA",
        "code" to code,
    ).map { "${it.key}=${it.value}" }.joinToString("&")
}

这个例子是在Kotlin,但重点是。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61901362

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档