我认为使用 cookie 是一个好主意,因为它们将被浏览器保留并随每个请求发送回代理。
您基本上需要添加过滤器并根据需要检查/插入 cookie:
return new HttpFiltersSourceAdapter() {
@Override
public HttpFilters filterRequest(HttpRequest originalRequest) {
return new HttpFiltersAdapter(originalRequest) {
@Override
public HttpResponse requestPre(HttpObject httpObject) {
if (httpObject instanceof HttpRequest) {
// check the cookie here
}
return super.requestPre(httpObject);
}
@Override
public HttpObject responsePre(HttpObject httpObject) {
if (httpObject instanceof HttpResponse) {
HttpResponse response = (HttpResponse) httpObject;
response.headers().set(HttpHeaders.Names.SET_COOKIE, ServerCookieEncoder.encode("Content", "Some"));
return response;
}
return httpObject;
}
};
}
};