我试图使用带有application/x form-urlencoded值的主体向keycloak REST端点发出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'令我失望的是,我还没有找到装模作样的办法。
下面是我尝试过的一个例子:
@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,但没有成功。
发布于 2020-05-20 20:43:53
我在这里找到了解决方案,How to POST form-url-encoded data with Spring Cloud 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));
}
}
}请求对象:
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class AuthTokenRequest {
private String username;
private String password;
private String client_id;
private String grant_type;
}响应对象:
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
public class KeycloakAccessToken {
@JsonProperty("access_token")
private String accessToken;
}发布于 2022-04-09 11:34:29
有时,这些变通方法/黑客或实际实现(如库本身所定义的)比所需的复杂和繁琐。
在这种情况下,我认为只需自己编码身体,就会更容易、更快。
@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
}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,但重点是。
https://stackoverflow.com/questions/61901362
复制相似问题