【发布时间】:2018-06-12 08:42:35
【问题描述】:
我试图用 Spring Webflux 做一些事情,但我真的对一些反应性概念感到困惑。
我有一些受表单身份验证保护的 REST 服务,在调用我的业务 REST 服务之前,我必须调用通过 Spring Security 提供的“登录”URL,传递我的凭据并将返回的 Cookie 放置到进一步调用其他 REST 服务。
下面是调用 REST 业务服务的片段......
@RequestMapping(value = "/endpoint1", method = RequestMethod.POST)
public Mono<String> businessService(@RequestBody ApiManagementWrapper apiManagementWrapper, ServerWebExchange httpRequest) {
log.info("Handling /enpoint1");
Mono<String> t = fetch(apiManagementWrapper, httpRequest);
return t;
}
private Mono<String> fetch(ApiManagementWrapper apiManagementWrapper, ServerWebExchange httpRequest) {
return this.webClientProxy
.post()
.uri("http://localhost:8081/something")
.headers(httpHeaders -> httpRequest.getRequest().getHeaders())
.cookies(httpCookies -> httpRequest.getRequest().getCookies())
.body(BodyInserters.fromPublisher(Mono.just(apiManagementWrapper.getEnterpriseMessage()), Object.class))
.exchange()
.flatMap(response -> response.bodyToMono(String.class));
}
完美运行我的问题是如何将其与呼叫登录服务结合起来..
我想过下面的 login() 方法...
private void login(ApiManagementWrapper apiManagementWrapper) {
LinkedMultiValueMap<String, String> formData = new LinkedMultiValueMap<>();
formData.add("username", "user1");
formData.add("password", "user1");
Mono<ClientResponse> response = this.webClientRouteConfirmation
.post()
.uri("http://localhost:8383/login")
.body(BodyInserters.fromFormData(formData))
.exchange();
response.subscribe(clientResponse -> {
handleLogin(clientResponse.cookies().getFirst("JSESSION_ID"), apiManagementWrapper);
});
}
private void handleLogin(ResponseCookie loginCookie, ApiManagementWrapper apiManagementWrapper){
Mono<Route> route = loadRoute(apiManagementWrapper.getEnterptiseContext(), loginCookie);
if(route == null) {
return;
}
}
private Mono<Route> loadRoute(EnterpriseContext enterpriseContext, ResponseCookie loginCookie) {
Mono<Route> route = this.webClientRouteConfirmation
.get()
.uri("http://localhost:8383/routes/search/findByServiceIdAndClientId?serviceName=" + enterpriseContext.getTarget() + "&clientName" + enterpriseContext.getSource())
.cookie("JSESSION_ID", loginCookie.getValue())
.exchange()
.flatMap(clientResponse -> clientResponse.bodyToMono(Route.class));
route.subscribe(r -> {
handleRoute(enterpriseContext, loginCookie);
});
}
使用 'login()' 方法我可以获得 'JSESSION_ID' Cookie,但是当它返回 Mono 时我无法访问 Cookie 值(我可以使用 clientResponse.block() 但我知道它是邪恶的所以尽量不要这样做),所以我尝试使用回调订阅 clientResponse,但如果我尝试这样做,我将与 'businessService()' 方法分离,并且无法返回我喜欢的值.. .
所以我想要实现的伪代码如下......
@RequestMapping(value = "/endpoint1", method = RequestMethod.POST)
public Mono<String> businessService(@RequestBody ApiManagementWrapper apiManagementWrapper, ServerWebExchange httpRequest) {
log.info("Handling /netty1");
Route route = login(apiManagementWrapper);
Mono<String> t = null;
if(route != null) {
t = fetch(apiManagementWrapper, httpRequest);
}
return t;
}
但我不知道如何在不使用 Mono.block() 的情况下去那里..
从 'handleLogin()' 返回 'Route' 对象没有任何意义,因为如果我正确理解 Reactive 概念,'fetch()' 方法无论如何都不会等待它......
有什么建议吗?
【问题讨论】:
标签: project-reactor spring-webflux