怎样用模板做网站,图书馆门户网站建设,企业营销型网站建设公司,电信网络服务商许多REST服务会将cookie用作身份验证/授权方案的一部分。 这是一个问题#xff0c;因为默认情况下#xff0c;旧的Jersey客户端将使用单例CookieHandler.getDefault #xff0c;大多数情况下#xff0c;该示例将为null#xff0c;如果不为null#xff0c;则在多线程服务器… 许多REST服务会将cookie用作身份验证/授权方案的一部分。 这是一个问题因为默认情况下旧的Jersey客户端将使用单例CookieHandler.getDefault 大多数情况下该示例将为null如果不为null则在多线程服务器环境中不太可能工作。 这是因为默认情况下默认的Jersey客户端将使用URL.openConnection。 现在您可以使用针对Jersey的Apache HTTP客户端适配器来解决此问题 但这并不总是可用。 因此如果您想在服务器环境中将Jersey客户端与cookie一起使用则需要做一些反思以确保您使用自己的私有cookie jar。 final CookieHandler ch new CookieManager();Client client new Client(new URLConnectionClientHandler(new HttpURLConnectionFactory() {Overridepublic HttpURLConnection getHttpURLConnection(URL uRL) throws IOException {HttpURLConnection connect (HttpURLConnection) uRL.openConnection();try {Field cookieField connect.getClass().getDeclaredField(cookieHandler);cookieField.setAccessible(true);MethodHandle mh MethodHandles.lookup().unreflectSetter(cookieField);mh.bindTo(connect).invoke(ch);} catch (Throwable e) {e.printStackTrace();}return connect;}})); 仅当您的环境使用JDK随附的sun.net.www.protocol.http.HttpURLConnection的内部实现时此方法才有效。 对于WLS的现代版本情况似乎如此。 对于JAX-RS 2.0可以使用Jersey 2.x特定的ClientConfig类和HttpUrlConnectorProvider进行类似的更改。 final CookieHandler ch new CookieManager();Client client ClientBuilder.newClient(new ClientConfig().connectorProvider(new HttpUrlConnectorProvider().connectionFactory(new HttpUrlConnectorProvider.ConnectionFactory() {Overridepublic HttpURLConnection getConnection(URL uRL) throws IOException {HttpURLConnection connect (HttpURLConnection) uRL.openConnection();try {Field cookieField connect.getClass().getDeclaredField(cookieHandler);cookieField.setAccessible(true);MethodHandle mh MethodHandles.lookup().unreflectSetter(cookieField);mh.bindTo(connect).invoke(ch);} catch (Throwable e) {e.printStackTrace();}return connect;}}))); 2015年2月11日更新在某些情况下尤其是使用https我看到HttpURLConnection封装在另一个类中要解决此问题只需使用反射即可访问委托字段的值。 我已经更新了代码示例以反映此问题。 翻译自: https://www.javacodegeeks.com/2015/02/per-client-cookie-handling-jersey.html