使用HttpComponentsMessageSender的基于Auth的WebServiceTemplate

时间:2014-07-07 11:40:37

标签: spring web-services authentication spring-security spring-ws

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

当我使用org.springframework.ws.transport.http.CommonsHttpMessageSender创建模板的MessageSender作为org.apache.commons.httpclient.UsernamePasswordCredentials对象bean时,我的Web服务客户端调用Web服务工作正常,虽然客户端工作,但代码突出显示警告说现在已弃用CommonsHttpMessageSender类,而我应该使用HttpComponentsMessageSender

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

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

Jars表示我的新工作代码使用较新的类:httpclient-4.3.4httpcore-4.3.2spring-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;
    }

}

提前致谢, PM

5 个答案:

答案 0 :(得分:6)

const Select = (props) => { var requireField; if( props.requireField ) { requireField = (<span className="text-error">*</span>); } return ( <div className="form-group"> <label className="form-label">{props.title}</label> <select name={props.name} value={props.selectedOption} onChange={props.controlFunc} className={["form-control", props.extraClass].join(' ')}> <option value="">{props.placeholder}</option> </select> </div> ); } HttpComponentsMessageSender一起使用。请注意,必须将UsernamePasswordCredentials创建为Spring bean,或者必须手动调用HttpComponentsMessageSender才能正确设置http客户端。 这对我有用:

afterPropertiesSet

答案 1 :(得分:2)

这是使用org.apache.httpcomponents进行项目的锻炼: httpclient-4.5.3httpcore-4.4.6

我们创建拦截器标头RequestDefaultHeaders reqHeader = new RequestDefaultHeaders(headers),然后在构建.addInterceptorLast(reqHeader)时使用CloseableHttpClient添加到httpClient

配置类:

import org.apache.http.message.BasicHeader;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.Header;
import org.apache.http.client.protocol.RequestDefaultHeaders;


@Bean
HttpClient createHttpClient() {
    List<Header> headers = new ArrayList<>();
    BasicHeader authHeader = new BasicHeader("Authorization", "Basic " + base64authUserPassword());
    headers.add(authHeader);
    // add more header as more as needed

    RequestDefaultHeaders reqHeader = new RequestDefaultHeaders(headers);

    CloseableHttpClient httpClient = 
        HttpClients.custom()
            .addInterceptorFirst(new HttpComponentsMessageSender.RemoveSoapHeadersInterceptor())
            .addInterceptorLast(reqHeader)
            .build();
    return httpClient;
}

@Bean
public HttpComponentsMessageSender defaultMyMessageSender() 
        throws KeyManagementException, NoSuchAlgorithmException, KeyStoreException {

    HttpComponentsMessageSender messageSender = new HttpComponentsMessageSender(createHttpClient());
    //messageSender.setCredentials(credentials());
    return messageSender;
}

@Bean
WebServiceTemplate webServiceTemplate() throws KeyManagementException, NoSuchAlgorithmException, KeyStoreException{

    WebServiceTemplate wsTemplate = new WebServiceTemplate();
    wsTemplate.setDefaultUri(endpointURI);
    wsTemplate.setMessageSender(defaultMyMessageSender());

    return wsTemplate;
}

答案 2 :(得分:1)

我使用的一个解决方案是使用自定义CredentialsProvider创建自定义WebServiceMessageSender。此解决方案还设置了一个遵循默认Java代理设置的路由规划器。

@Configuration
public class WebServiceConfiguration {

    @Bean
    public WebServiceMessageSender webServiceMessageSender(@Value("${endpoint.uri}") endpointUri, 
                                                           @Value("${endpoint.username}") String username, 
                                                           @Value("${endpoint.password}") String password) throws Exception {
        SystemDefaultRoutePlanner routePlanner = new SystemDefaultRoutePlanner(
            ProxySelector.getDefault());
        BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider();
        credentialsProvider.setCredentials(new AuthScope(endpointUri.getHost(), endpointUri.getPort(), ANY_REALM, ANY_SCHEME), new UsernamePasswordCredentials(username, password););
        CloseableHttpClient httpclient = HttpClients.custom()
            .setRoutePlanner(routePlanner)
            .addInterceptorFirst(new HttpComponentsMessageSender.RemoveSoapHeadersInterceptor())
            .setDefaultCredentialsProvider(credentialsProvider)
            .build();

        return new HttpComponentsMessageSender(httpclient);
    }
}

答案 3 :(得分:0)

最后,为了使用当前WebServiceTemplatespring-ws-xxx.2.2.0.RELEASE类使用httpclient-4.3.+中的Spring httpcore-4.3.+进行基本身份验证,我添加了一个抢占式身份验证拦截器HttpClient(由@Oliv在Preemptive Basic authentication with Apache HttpClient 4中建议)。请注意,正如@Oliv所指出的,此解决方案会对所有请求添加身份验证。

我仍然不确定这是否是配置Spring WebServiceTemplate的最佳方式,但这是我找到(目前为止)启用抢先身份验证但没有直接访问HttpClient的唯一方法是HttpClientContext对象。任何更简单更好的答案我都非常欢迎......

拦截器代码:

private static class PreemptiveAuthInterceptor implements HttpRequestInterceptor {

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

        AuthState authState = (AuthState) context.getAttribute(
                HttpClientContext.TARGET_AUTH_STATE);

        // If no auth scheme is avaialble yet, initialize it preemptively
        if ( authState.getAuthScheme() == null ) {

            CredentialsProvider credsProvider = (CredentialsProvider) context.getAttribute(
                    HttpClientContext.CREDS_PROVIDER);

            HttpHost targetHost = (HttpHost) context.getAttribute(
                    HttpCoreContext.HTTP_TARGET_HOST);

            Credentials creds = credsProvider.getCredentials(
                    new AuthScope(targetHost.getHostName(), targetHost.getPort()));

            if ( creds == null ) {
                throw new HttpException("no credentials available for preemptive "
                        + "authentication");
            }

            authState.update(new BasicScheme(), creds);
        }
    }
}

答案 4 :(得分:0)

线程虽然陈旧,但总结。

根据春季文档:

UsernamePasswordCredentials和HttpComponentsMessageSender应该是spring bean。所以定义bean并注入它们。它应该解决问题。