我使用Spring的 WebServiceGatewaySupport 连接到供应商的SOAP Web服务。此服务的要求之一是客户端必须维护服务器发送的会话cookie。
我能够确定 WebServiceGatewaySupport 在内部使用 HttpURLConnection 类来发出请求。只需致电
CookieHandler.setDefault(new CookieManager());
在派对开始之前添加一个默认的cookie管理器,一切都在我本地的Tomcat实例上发挥得非常好看(我甚至注意到我的机器旁边出现了一个小彩虹)。
但是,当我部署到WebLogic 10.3.6.0时,所有内容都是Miley Cyrus。它不像过去那样扭曲,我的饼干被扔了。
我能够通过覆盖CookieManager的get和put方法来证明WebLogic是罪魁祸首。在Tomcat中对这些行动采取了很多行动。不是WebLogic的杂音。
CookieHandler.setDefault(new CookieManager() {
@Override
public Map<String, List<String>> get(URI uri, Map<String, List<String>> stringListMap) throws IOException {
Map<String, List<String>> map = super.get(uri, stringListMap);
LOGGER.info("Cop that: " + uri + " " + map);
return map;
}
@Override
public void put(URI uri, Map<String, List<String>> stringListMap) throws IOException {
LOGGER.info("Hello sailor: " + uri + " " + stringListMap);
super.put(uri, stringListMap);
}
});
((CookieManager)CookieHandler.getDefault()).setCookiePolicy(CookiePolicy.ACCEPT_ALL);
我只能假设存在某种用于传入servlet请求的“高级安全性shenanigans”,但也适用于传出连接。我找不到任何有用的weblogic部署描述符选项。
开溜。
我可能会让它与Axis一起工作,但我会用钢笔将自己刺入脸中。
我要回家了。
更新:好的,我还没有解决根本原因,但这是我如何运作的。我在想是否可以访问实际的HttpURLConnection对象,我可以对其进行手动cookie管理。我能够查看Spring WS源代码并设置一个新的MessageSender,它的工作方式基本相同。
public class MyClient extends WebServiceGatewaySupport {
public MyClient(WebServiceMessageFactory messageFactory) {
super(messageFactory);
super.getWebServiceTemplate().setMessageSender(new WebServiceMessageSender() {
@Override
public WebServiceConnection createConnection(URI uri) throws IOException {
URL url = uri.toURL();
URLConnection connection = url.openConnection();
if (!(connection instanceof HttpURLConnection)) {
throw new HttpTransportException("URI [" + uri + "] is not an HTTP URL");
}
HttpURLConnection httpURLConnection = (HttpURLConnection) connection;
prepareConnection(httpURLConnection);
HttpURLConnectionProxy httpURLConnectionProxy = new HttpURLConnectionProxy(url);
httpURLConnectionProxy.setHttpURLConnection(httpURLConnection);
httpURLConnectionProxy.setCookieManager(cookieManager);
return new MyHttpUrlConnection(httpURLConnectionProxy);
}
protected void prepareConnection(HttpURLConnection connection) throws IOException {
connection.setRequestMethod(HttpTransportConstants.METHOD_POST);
connection.setUseCaches(false);
connection.setDoInput(true);
connection.setDoOutput(true);
// ORRRRR YEAAHHHHHHH!
cookieManager.setCookies(connection);
}
@Override
public boolean supports(URI uri) {
return true;
}
});
}
另一个复杂因素是我需要在调用connect()之前和之后设置和获取cookie数据。所以我创建了一个HttpURLConnectionProxy类,它代理了url.openConnection()生成的所有方法调用,但在connect()之后执行了cookie;
public void connect() throws IOException {
httpURLConnection.connect();
// WOOPWOOPWOOPWOOP!
cookieManager.storeCookies(httpURLConnection);
}
但是TWERKS
答案 0 :(得分:1)
我认为你正在扭曲CookieManager API的用途。请参考documentation和CookieManager documentation。 您的供应商的要求是维护服务器发送的会话cookie。要实现此要求,您需要两个步骤:
假设您使用的是Spring 3.1或更高版本,请在下面的配置类中找到:
@Configuration
@EnableWebMvc // this annotation imports the class WebMvcConfigurationSupport which bootstraps web mvc
@ComponentScan(basePackages = { "com.orgname" })
public class WebConfig extends WebMvcConfigurerAdapter {
@Bean
public ViewResolver viewResolver() {
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
viewResolver.setViewClass(JstlView.class);
viewResolver.setPrefix("/view/jsp/");
viewResolver.setSuffix(".jsp");
return viewResolver;
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
}
/**
* This method invocation bean stands for the method call:
* CookieHandler.setDefault(new CookieManager());
* which should be done at the beginning of an HTTP session to bootstrap
* the Java 6 Http state management mechanism for the application as a whole.
* (http://docs.oracle.com/javase/tutorial/networking/cookies/cookiehandler.html)
*
*/
@Bean(name="cookieHandlerSetDefaultBean")
public MethodInvokingFactoryBean methodInvokingFactoryBean() {
MethodInvokingFactoryBean methodInvokingFactoryBean = new MethodInvokingFactoryBean();
methodInvokingFactoryBean.setTargetClass(CookieHandler.class);
methodInvokingFactoryBean.setTargetMethod("setDefault");
CookieManager cookieManager = new CookieManager();
methodInvokingFactoryBean.setArguments(new Object[]{cookieManager});
return methodInvokingFactoryBean;
}
}
鉴于您的客户端类是Spring Service或Component。请在下面找到它的代码。
/**
* This service aggregates the default CookieManager as explained in the API
* (http://docs.oracle.com/javase/6/docs/api/java/net/CookieManager.html).
* A system-wide CookieManager that is used by the HTTP protocol handler
* can be retrieved by calling CookieHandler.getDefault().
* A CookieManager is initialized with aآ CookieStoreآ which manages storage
* A CookieStore supports add(cookie) and getCookie() methods
* A CookieStore is responsible of removing Cookie instances which have expired.
*
*/
@Service(value="serviceConfigBean")
@DependsOn(value="cookieHandlerSetDefault") //This is the bean initialized in the Configuration class. It is needed to be initialized before the container initializes the Service
public class ClientCookiesStore {
private static final Logger logger = LoggerFactory.getLogger(ClientCookiesStore.class);
protected CookieStore inmemoryCookieStore;
protected URI clientURI;
/**
* The @PostConstruct (lifecycle callback method) indicates this method should be invoked after all
* dependency injection is complete. Thus helps in initializing any resources needed by the
* service.
*
* In this particular initializing method:
* (as per http://docs.oracle.com/javase/6/docs/api/java/net/CookieManager.html
* and http://docs.oracle.com/javase/tutorial/networking/cookies/cookiemanager.html)
* The CookieHandler default is installed in the application via
* a method invoking factory bean, namely "cookieHandlerSetDefault" which
* exists in the java configuration file WebConfig.java
* (1) A cookieManager property needs 2 steps setup as indicated in the code
* (2) The internal in-memory implementation of the CookieStore interface is initialized
* through the cookieManager defaults. It is assigned to the inmemoryCookieStore property.
* (3) Since a CookieStore aggregates many groups of cookies, each group is identified
* by a URI instance. ClientCookiesStore is associated with the Client URI as indicated in
* the code.
*
* @throws Exception
*/
@PostConstruct
protected void initializeBean() throws Exception {
// (1) Step#1 Initialize a CookieManager with the current Http session default
// which was already set in the configuration class
CookieManager cookieManager = (CookieManager)CookieHandler.getDefault();
// Step#2 Then set up the CookiePolicy.
cookieManager.setCookiePolicy(CookiePolicy.ACCEPT_ALL);
// (2) Assign a CookieStore instance to the in-memory cookie store implemented by the API
inmemoryCookieStore = cookieManager.getCookieStore();
// (3) Initialize URI instance which will identify the Client cookies in the CookieStore
try {
clientURI = new URI("http://vendor.webservices.com/endpoint");
} catch (URISyntaxException e) {
throw new Exception("URISyntaxException created while creating a URI instance for url= "+clientUrl);
}
}
}
剩下的就是添加新cookie和从内存存储中检索cookie的两种方法。这两种方法都属于上面的ClientCookiesStore类。
public List<HttpCookie> getCookiesList() throws Exception {
List<HttpCookie> httpCookiesList = inmemoryCookieStore.get(clientURI);
return httpCookiesList;
}
public void addCookie(HttpCookie newCookie) {
inmemoryCookieStore.add(clientURI, newCookie);
}