ive been tryint to get my application to authenticate and start a session with another spring boot appilcation, ive been doing it like so.
WebClient webClient= WebClient.builder().build();
String reet=webClient.get().uri("http://localhost:8081/authenticate")
.header("x-api-key","123456789")
.header("Connection","keep-alive")
.accept(MediaType.APPLICATION_JSON)
.retrieve().bodyToMono(String.class).block();
query.put("collection","student");
String ret =webClient.post().uri("http://localhost:8081/query")
.contentType(MediaType.APPLICATION_JSON)
.body(BodyInserters.fromValue(query))
.retrieve().bodyToMono(String.class).block();
The first request should use the API key and authenticate, which starts the session. the second one should do a query, which will work IF the user is authenticated.
how do i get the session cookie from the response so i can send it back in the second request?
This has troubled me a lot and I was not able to retrieve exact session cookie from webClient due to ClientResponse not able to support text/html content-type in my case. So I just parsed the cookie value and created cookie again and add it in RestController.
/** * This method call the grafana API to retrieve session cookie value and return it as String * * @param userEmail * @param grafanaHostname * @return String */ private String retreiveGrafanaSessionCookieVal(String userEmail, String grafanaHostname) {
String grafana_url = grafanaHostname + ArgoCDURLConstant.GRAFANA_LOGIN_URL;
WebClient client1 = WebClient.create(grafana_url);
String cookieVal = client1.get().header(HttpHeaders.CONTENT_TYPE, MediaType.TEXT_HTML_VALUE)
.header(ArgoCDURLConstant.GRAFANA_WEBAUTH_USER_HEADER, userEmail).accept(MediaType.ALL)
.exchangeToMono(response -> {
MultiValueMap<String, ResponseCookie> cookies = response.cookies();
String grCookieVal = "";
for (var cookie : cookies.entrySet()) {
//System.out.println(cookie.getKey() + " : " + cookie.getValue());
List<ResponseCookie> resCookie = cookie.getValue();
for (ResponseCookie responseCookie : resCookie) {
//System.out.println(responseCookie.getName());
//System.out.println(grCookieVal = responseCookie.getValue());
grCookieVal = responseCookie.getValue();
}
}
return Mono.just(grCookieVal);
}).block();
//System.out.println("cookieVal::" + cookieVal);
return cookieVal;
}
/** * This method call the grafana API to retrieve session cookie value and set it in * cookie * * @param userEmail * @param domain * @param grafanaHostname * @return ResponseCookie */ public ResponseCookie getGrafanaSessionCookie(String userEmail, String domain, String grafanaHostname) {
ResponseCookie grafana_cookie;
String hostname = CommonConstants.DOT + domain.split(CommonConstants.COLON)[0];
String cookieValue = retreiveGrafanaSessionCookieVal(userEmail, grafanaHostname);
grafana_cookie = ResponseCookie.from(ArgoCDURLConstant.GRAFANA_SESSION_COOKIE_NAME, cookieValue.toString())
.httpOnly(true).secure(true).domain(hostname)
// .path("/") // path
.maxAge(Duration.ofDays(30)).sameSite(CommonConstants.COOKIE_SAMESITE_NONE).build();
return grafana_cookie;
}
/* Rest Controller method returning cookie in response. */ @GetMapping public ResponseEntity getGrafanaSession(@RequestHeader(name = "Authorization") String token, @RequestParam(value = "hostname", required = false) String grafanaHost, HttpServletResponse response, @RequestHeader("Host") String hostname) {
ResponseCookie grafana_cookie = grafanaService.getGrafanaSessionCookie(grafanaHost, hostname, token,
grafanaHost);
// Set cookie in the response
response.setHeader(HttpHeaders.SET_COOKIE, grafana_cookie.toString());
return new ResponseEntity<String>(HttpStatus.OK);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.