gpt4 book ai didi

spring - 使用 HttpComponentsMessageSender 的具有基本身份验证的 WebServiceTemplate

转载 作者:行者123 更新时间:2023-12-03 15:26:55 34 4
gpt4 key购买 nike

我正在尝试测试当前使用基本身份验证保护的 Spring Web 服务。对于这些测试,我使用 Spring 的 WebServiceTemplate 编写了一个 Web 服务客户端。类。

当我将模板的 MessageSender 创建为 org.springframework.ws.transport.http.CommonsHttpMessageSender 时,我的 Web 服务客户端调用 Web 服务工作正常。对象 bean 与 org.apache.commons.httpclient.UsernamePasswordCredentials并且,尽管客户端可以工作,但代码中突出显示了一条警告,指出 CommonsHttpMessageSender类现在已弃用,我应该使用 HttpComponentsMessageSender相反。

我尝试重新配置客户端的 WebServiceTemplate使用较新的 HttpComponentsMessageSender 工作类,但我无法正确配置基本身份验证部分。换新HttpComponentsMessageSender类,我已经使用 org.apache.http.auth.UsernamePasswordCredentials 创建了凭据类,但是,当我调用 Web 服务时,凭据似乎无法用于请求?是否有 WebServiceTemplate 客户端的工作示例使用这些较新的类来验证请求等?

我的工作代码与旧的弃用类使用的 jar :commons-httpclient-3.1 , spring-ws-core-2.2.0.RELEASE .

我的新类的非工作代码使用的 jar :httpclient-4.3.4 , httpcore-4.3.2 , spring-ws-core-2.2.0.RELEASE .

测试配置,因为它代表非工作代码:

package com.company.service.a.ws.test.config;

import java.io.IOException;

import org.apache.http.HttpException;
import org.apache.http.HttpRequest;
import org.apache.http.HttpRequestInterceptor;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.protocol.HTTP;
import org.apache.http.protocol.HttpContext;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.oxm.jaxb.Jaxb2Marshaller;
import org.springframework.ws.client.core.WebServiceTemplate;
import org.springframework.ws.soap.saaj.SaajSoapMessageFactory;
import org.springframework.ws.transport.http.HttpComponentsMessageSender;

@PropertySource("classpath:/${environment}-use-case-data.properties")
@ComponentScan(basePackages = "com.company.service.a.ws.test")
@Configuration
public class TestConfig {

@Value("${ws.url}")
private String wsUrl;

@Value("${ws.username}")
private String username;

@Value("${ws.password}")
private String password;

private static final Logger logger = LogManager.getLogger();

@Bean
public SaajSoapMessageFactory messageFactory() {
return new SaajSoapMessageFactory();
}

@Bean
public Jaxb2Marshaller marshaller() {
Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
marshaller.setContextPath("com.company.service.a.ws.model.data");
return marshaller;
}

@Bean RequestConfig requestConfig() {

RequestConfig requestConfig = RequestConfig.custom()
.setAuthenticationEnabled(true)
.build();
return requestConfig;
}

@Bean
@DependsOn( value = "propertyConfigurer" )
public UsernamePasswordCredentials credentials() {

logger.debug("creating credentials for username: {} passowrd={}",
username, password);

UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(
username, password);

return credentials;
}

@Bean
public CredentialsProvider credentialsProvider() {
CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(AuthScope.ANY, credentials());
return credentialsProvider;
}

private static class ContentLengthHeaderRemover implements HttpRequestInterceptor{

@Override
public void process(HttpRequest request, HttpContext context)
throws HttpException, IOException {

// fighting org.apache.http.protocol.RequestContent's
// ProtocolException("Content-Length header already present");
request.removeHeaders(HTTP.CONTENT_LEN);
}
}

@Bean
public HttpComponentsMessageSender messageSender() {

RequestConfig requestConfig = RequestConfig.custom()
.setAuthenticationEnabled(true)
.build();

HttpClientBuilder httpClientBuilder = HttpClients.custom();

HttpClient httpClient = httpClientBuilder
.addInterceptorFirst(new ContentLengthHeaderRemover())
.setDefaultRequestConfig(requestConfig)
.setDefaultCredentialsProvider(credentialsProvider())
.build();

HttpComponentsMessageSender messageSender = new HttpComponentsMessageSender(httpClient);
return messageSender;
}

@Bean( name = "propertyConfigurer" )
public static PropertySourcesPlaceholderConfigurer propertyConfigurer() {
PropertySourcesPlaceholderConfigurer configurer =
new PropertySourcesPlaceholderConfigurer();

return configurer;
}

@Bean
public WebServiceTemplate webServiceTemplate() {

logger.debug("creating webServiceTemplate to url: {}", wsUrl);

WebServiceTemplate webServiceTemplate = new WebServiceTemplate(messageFactory());
webServiceTemplate.setDefaultUri(wsUrl);
webServiceTemplate.setMarshaller(marshaller());
webServiceTemplate.setUnmarshaller(marshaller());
webServiceTemplate.setMessageSender(messageSender());
return webServiceTemplate;
}

}

提前致谢,
下午

最佳答案

使用 HttpComponentsMessageSenderUsernamePasswordCredentials .请注意 HttpComponentsMessageSender必须创建为 Spring bean 或者您必须调用 afterPropertiesSet手动正确设置 http 客户端。
这对我有用:

@Configuration
public class WsClientConfiguration {


@Bean
public ESignatureProcessorClient eSignatureProcessorClient() {
ESignatureProcessorClient client = new ESignatureProcessorClient();
client.setWebServiceTemplate(mwWebServiceTemplate());
return client;
}

@Bean
public WebServiceTemplate mwWebServiceTemplate() {
Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
marshaller.setContextPath("cz.csas.services.esignatureprocessor.v02_02");
WebServiceTemplate template = new WebServiceTemplate(marshaller, marshaller);
template.setDefaultUri("https://osb-st2.vs.csin.cz:5001/CSMW/WS_MW_ESignatureProcessor_v02_02");
template.setMessageSender(defaultMwMessageSender());
return template;
}

@Bean
public HttpComponentsMessageSender defaultMwMessageSender() {
HttpComponentsMessageSender messageSender = new HttpComponentsMessageSender();
messageSender.setCredentials(new UsernamePasswordCredentials("user", "password"));
return messageSender;
}

}

关于spring - 使用 HttpComponentsMessageSender 的具有基本身份验证的 WebServiceTemplate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24609751/

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