Home > Software engineering >  Adding parameters to webclient
Adding parameters to webclient

Time:11-19

I need help!

here is my webclient

private WebClient client = WebClient.create("http://address:port");

public Mono<RespDto> translate(AuthPrincipal principal, ReqDto dto, String token) {
    return client.get()
            .uri(uriBuilder -> uriBuilder
                    .path("/api")
                    .queryParam("id", dto.getId())
                    .queryParam("content", dto.getContent())
                    .queryParam("profile", dto.getProfile())
                    .build())
            .headers(h -> h.setBearerAuth(token.split(" ")[1]))
            .retrieve()
            .bodyToMono(RespDto.class);

}

and here is the controller for the api I'm trying to retrieve

@GetMapping("")
public Mono<?> trans(@AuthenticationPrincipal @ApiIgnore AuthPrincipal principal,
                     @Valid ReqDto reqDto) {
    return service.function(principal, reqDto);
}

So my problem is that the service function checks principal id for authority but I know how how to pass AuthPrincipal object via webclient...

please help the noob!

CodePudding user response:

You are already providing the Bearer authenticaton token with the following line of code: .headers(h -> h.setBearerAuth(token.split(" ")[1])). If this is working properly and you are able to authenticate yourself against the target API, then Spring will inject AuthPrincipal in the endpoint method (assuming your Spring Security configuration / customization is working correctly).

  • Related