在 webflux 中以反应方式在请求中添加 JWT 令牌
adding JWT token in request in reactive way in webflux
我正在使用 WebClient
调用由 JWT 令牌保护的 Rest API。
//获取Token
JwtToken token = client.post()
.uri("")
.body(BodyInserters.fromFormData("username", "XXX")
.with("password", "XXXXX"))
.retrieve()
.bodyToFlux(JwtToken.class)
.onErrorMap(e -> new Exception("Error While getting Token", e))
.blockLast();
//安全调用API
WebClient client = consorsWebClientBuilder
.defaultHeaders(token.bearer())
.build();
client
.get()
.uri(someURI)
.retrieve()
.bodyToMono(String.class)
我在反应链中调用 Block
,所以网络流量不高兴并说
block()/blockFirst()/blockLast()是阻塞的,thread reactor-http-client-epoll-12
不支持
我如何以反应方式添加令牌?
请注意,我可以创建过滤器但仍在过滤器中我还需要调用 Block
您可以使用 doOnSuccess
client.post()
.uri("")
.body(BodyInserters.fromFormData("username", "XXX")
.with("password", "XXXXX"))
.retrieve()
.bodyToFlux(JwtToken.class)
.onErrorMap(e -> new Exception("Error While getting Token", e))
.doOnSuccess(jwtToken -> {
client.header(HttpHeaders.AUTHORIZATION, "bearer " + jwtToken)
.get()
...
})
I used map for this, not the best solution but does the work
Mono<JwtToken> token = client.post()
.uri("")
.body(BodyInserters.fromFormData("username", "XXX")
.with("password", "XXXXX"))
.retrieve()
.bodyToMon(JwtToken.class)
.onErrorMap(e -> new Exception("Error While getting Token", e))
return token
.flatMap(token -> callApi(request, token));
还有一种添加它的方法,即使用 filter
在 WebClient
中实现 ExchangeFilterFunction
,如下所示:
WebClient.builder().filter(setJWT());
private ExchangeFilterFunction setJWT() {
return ExchangeFilterFunction.ofRequestProcessor((clientRequest) -> {
ClientRequest authorizedRequest = ClientRequest.from(clientRequest).header("AUTHORIZATION","{LOGIC TO GET THE TOKEN}").build();
return Mono.just(authorizedRequest);
});
}
此实现将在实际调用 API 并添加 JWT 令牌之前接收请求。
我正在使用 WebClient
调用由 JWT 令牌保护的 Rest API。
//获取Token
JwtToken token = client.post()
.uri("")
.body(BodyInserters.fromFormData("username", "XXX")
.with("password", "XXXXX"))
.retrieve()
.bodyToFlux(JwtToken.class)
.onErrorMap(e -> new Exception("Error While getting Token", e))
.blockLast();
//安全调用API
WebClient client = consorsWebClientBuilder
.defaultHeaders(token.bearer())
.build();
client
.get()
.uri(someURI)
.retrieve()
.bodyToMono(String.class)
我在反应链中调用 Block
,所以网络流量不高兴并说
block()/blockFirst()/blockLast()是阻塞的,thread reactor-http-client-epoll-12
不支持我如何以反应方式添加令牌?
请注意,我可以创建过滤器但仍在过滤器中我还需要调用 Block
您可以使用 doOnSuccess
client.post()
.uri("")
.body(BodyInserters.fromFormData("username", "XXX")
.with("password", "XXXXX"))
.retrieve()
.bodyToFlux(JwtToken.class)
.onErrorMap(e -> new Exception("Error While getting Token", e))
.doOnSuccess(jwtToken -> {
client.header(HttpHeaders.AUTHORIZATION, "bearer " + jwtToken)
.get()
...
})
I used map for this, not the best solution but does the work
Mono<JwtToken> token = client.post()
.uri("")
.body(BodyInserters.fromFormData("username", "XXX")
.with("password", "XXXXX"))
.retrieve()
.bodyToMon(JwtToken.class)
.onErrorMap(e -> new Exception("Error While getting Token", e))
return token
.flatMap(token -> callApi(request, token));
还有一种添加它的方法,即使用 filter
在 WebClient
中实现 ExchangeFilterFunction
,如下所示:
WebClient.builder().filter(setJWT());
private ExchangeFilterFunction setJWT() {
return ExchangeFilterFunction.ofRequestProcessor((clientRequest) -> {
ClientRequest authorizedRequest = ClientRequest.from(clientRequest).header("AUTHORIZATION","{LOGIC TO GET THE TOKEN}").build();
return Mono.just(authorizedRequest);
});
}
此实现将在实际调用 API 并添加 JWT 令牌之前接收请求。