Обработка файлов cookie для веб-клиентаJAVA

Программисты JAVA общаются здесь
Ответить
Anonymous
 Обработка файлов cookie для веб-клиента

Сообщение Anonymous »


Is there any default implementation for handling cookies with WebClient ?

For below code, I've been using CookieManager().getCookieStore() using WebClient#filters. But want to check, if there is any efficient way to handle this ?

Additionally, can cookieStore be mapped to @SessionScope ? As WebClient works in reactive context, whereas Session is specific to Current request ?
@Service public class WebClientProvider { private final CookieStore cookieStore; public WebClientProvider() { this.cookieStore = new CookieManager().getCookieStore(); } public WebClient getClient(final String serviceId) { return WebClient.builder().filter(cookieHandlerFilter(serviceId)).build(); } ExchangeFilterFunction cookieHandlerFilter(final String serviceId) { return (clientRequest, next) -> { final List cookiesList = cookieStore.get(clientRequest.url()); clientRequest = cookiesList.isEmpty() ? addProxyTicket(clientRequest) : addCookies(clientRequest, cookiesList); final ClientRequest updatedRequest = clientRequest; return next.exchange(clientRequest) .flatMap(clientResponse -> handleResponse(updatedRequest, clientResponse)); }; } private ClientRequest addCookies(final ClientRequest request, final List cookiesList) { return ClientRequest.from(request).cookies(cookies -> { cookies.addAll(cookiesList.stream().collect(LinkedMultiValueMap::new,(map, cookie) -> map.add(cookie.getName(), cookie.getValue()),LinkedMultiValueMap::addAll));}) .build(); } private Mono handleResponse(final ClientRequest request, final ClientResponse clientResponse) { if (clientResponse.statusCode().is2xxSuccessful()) { // If response succesful, update cookies in cookie Store if (!clientResponse.cookies().isEmpty()) { clientResponse.cookies().values().stream().flatMap(Collection::stream).toList() .forEach(responseCookie -> cookieStore.add(request.url(), createHttpCookie(responseCookie))); } } else { //Remove cookies in CookieStore for current request's URI cookieStore.get(request.url()).forEach(cookie -> cookieStore.remove(uri, cookie)); } return Mono.just(clientResponse); } }

Источник: https://stackoverflow.com/questions/780 ... -webclient
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «JAVA»