gpt4 book ai didi

java - 在 WebLogic 上设置默认的 CookieManager 没有效果

转载 作者:行者123 更新时间:2023-11-30 11:25:34 34 4
gpt4 key购买 nike

我正在使用 Spring 的 WebServiceGatewaySupport 连接到供应商的 SOAP Web 服务。该服务的要求之一是客户端必须维护服务器发送的 session cookie。

我能够确定 WebServiceGatewaySupport 在内部使用 HttpURLConnection 类来发出请求。简单调用

CookieHandler.setDefault(new CookieManager());

在聚会开始之前添加一个默认的 cookie 管理器,一切都在我本地的 Tomcat 实例上运行得非常愉快(我什至注意到我的机器旁边出现了一个小彩虹)。

但是,当我部署到 WebLogic 10.3.6.0 时,Miley Cyrus 一切顺利。它不像以前那样摇晃,我的 cookies 被扔掉了。

通过覆盖 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 请求,但也应用于传出连接。我找不到任何有用的 weblogic 部署描述符选项。

SCSS 。

我可能可以让它与 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);
}

但它很奇怪

最佳答案

我认为您在扭曲 CookieManager API 的预期用途。请引用documentationCookieManager documentation .您的供应商的要求是维护服务器发送的 session cookie。要实现此要求,您需要两个步骤:

  1. 在 Spring 容器中,连接一个 Spring bean,它包含方法调用 CookieHandler.setDefault(new CookieManager());
  2. 在客户端代码中初始化一个 URI 实例,该实例将标识 CookieStore 中的 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 服务或组件。请在下面找到它的代码。

/**
* 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 的 2 种方法。这两个方法都属于上面的 ClientCookiesStore 类。

public List<HttpCookie> getCookiesList() throws Exception {
List<HttpCookie> httpCookiesList = inmemoryCookieStore.get(clientURI);
return httpCookiesList;
}

public void addCookie(HttpCookie newCookie) {
inmemoryCookieStore.add(clientURI, newCookie);
}

关于java - 在 WebLogic 上设置默认的 CookieManager 没有效果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20235207/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com