许多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