Java 在WebLogic上设置默认CookieManager无效
我正在使用Spring的WebServiceGatewaySupport连接到供应商的SOAP Web服务。此服务的要求之一是客户端必须维护服务器发送的会话cookie 我能够确定WebServiceGatewaySupport在内部使用HttpURLConnection类发出请求。简单的呼唤Java 在WebLogic上设置默认CookieManager无效,java,spring,soap,weblogic,spring-ws,Java,Spring,Soap,Weblogic,Spring Ws,我正在使用Spring的WebServiceGatewaySupport连接到供应商的SOAP Web服务。此服务的要求之一是客户端必须维护服务器发送的会话cookie 我能够确定WebServiceGatewaySupport在内部使用HttpURLConnection类发出请求。简单的呼唤 CookieHandler.setDefault(new CookieManager()); 在聚会开始之前,我添加了一个默认的cookie管理器,所有的东西都在我本地的Tomcat实例上运行得很好(我
CookieHandler.setDefault(new CookieManager());
在聚会开始之前,我添加了一个默认的cookie管理器,所有的东西都在我本地的Tomcat实例上运行得很好(我甚至注意到我的机器旁边出现了一个小彩虹)
但是,当我部署到WebLogic 10.3.6.0时,一切都进展顺利。它不像以前那样叽叽喳喳,我的饼干被扔了
我能够通过重写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);
另一个复杂的问题是,我需要在调用connect()之前和之后设置并获取cookie数据。因此,我创建了一个HttpURLConnectionProxy类,它将所有方法调用代理给url.openConnection()生成的方法调用,但在connect()之后执行cookie操作
但它确实起了作用,我认为您正在扭曲CookieManager API的预期用途。请参考和。 供应商的要求是维护服务器发送的会话cookie。要达到此要求,您需要两个步骤:
@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;
}
}
步骤2
假设您的客户机类是Spring服务或组件。请在下面找到它的代码
/**
* 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和从inmemory存储中检索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);
}
public List getCookiesList()引发异常{
List-httpCookiesList=inmemoryCookieStore.get(clientURI);
返回httpCookiesList;
}
公共void addCookie(HttpCookie newCookie){
添加(clientURI,newCookie);
}
Hi。谢谢你的回复。我试试这个。(请注意:我的“hack”中的CookieManager不是系统cookie管理器。它是一个自定义实现。)谢谢sam。我还没有时间尝试你的答案(我的hack工作正常,我的老板让我做其他事情)。我很感兴趣为什么这个简单的“CookieHandler.setDefault(new CookieManager());”调用在Tomcat上运行良好,但是Weblogic需要额外的连接。如果我没有机会很快测试它,我会接受它的解决。
/**
* 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);
}
}
public List<HttpCookie> getCookiesList() throws Exception {
List<HttpCookie> httpCookiesList = inmemoryCookieStore.get(clientURI);
return httpCookiesList;
}
public void addCookie(HttpCookie newCookie) {
inmemoryCookieStore.add(clientURI, newCookie);
}