是否有一种更简单的方法来设置http客户端以进行抢先式基本身份验证,而不是此处描述的内容?
在以前的版本(3.x)中,它曾经是一个简单的方法调用(例如httpClient.getParams().setAuthenticationPreemptive(true)).
我想避免的主要是将BasicHttpContext添加到我执行的每个方法.
自从HttpClient 4.3以来,我一直在使用HttpClientBuilder.我正在连接到具有基本身份验证的REST服务.我正在设置凭据如下:
HttpClientBuilder builder = HttpClientBuilder.create();
// Get the client credentials
String username = Config.get(Constants.CONFIG_USERNAME);
String password = Config.get(Constants.CONFIG_PASSWORD);
// If username and password was found, inject the credentials
if (username != null && password != null)
{
CredentialsProvider provider = new BasicCredentialsProvider();
// Create the authentication scope
AuthScope scope = new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, AuthScope.ANY_REALM);
// Create credential pair
UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(username, password);
// Inject the credentials
provider.setCredentials(scope, credentials);
// Set the default credentials provider
builder.setDefaultCredentialsProvider(provider);
}
Run Code Online (Sandbox Code Playgroud)
但是,这不起作用(我正在使用的REST服务返回401).出了什么问题?
我正在尝试使用Spring-WS构建一个简单的Web服务客户端,并且正在进行攻击.我试图调用的SOAP服务使用HTTP基本身份验证来提高安全性.
使用Spring-WS教程示例,我已将my配置WebServiceTemplate为使用HttpComponentsMessageSender我提供的凭据:
<bean id="webServiceTemplate" class="org.springframework.ws.client.core.WebServiceTemplate">
<constructor-arg ref="messageFactory"/>
<property name="messageSender">
<bean class="org.springframework.ws.transport.http.HttpComponentsMessageSender">
<property name="credentials">
<bean class="org.apache.http.auth.UsernamePasswordCredentials">
<constructor-arg value="john:secret"/>
</bean>
</property>
</bean>
</property>
<property name="defaultUri" value="http://example.com/WebService"/>
</bean>
Run Code Online (Sandbox Code Playgroud)
当我尝试执行客户端时,出现以下错误:
org.springframework.ws.client.WebServiceTransportException: Authorization Required [401]
我之前的(使用HttpComponentsMessageSender和HttpClientBuilder基本身份验证的基本身份验证的WebServiceTemplate)搜索表明问题是Spring-WS尝试首先连接到没有凭据的端点,这导致端点拒绝初始连接.
对于仍使用HTTP基本身份验证的Web服务,这似乎是一个非常常见的问题.
我的问题是如何让Spring在初始连接尝试中提供凭据?
我想避免为一些看似微不足道的事情重新实现整个框架.
(作为参考,端点是我无法控制的内部Web服务,并且没有其他选项可以使用HTTP基本身份验证,无论人们如何认为它是如何失效或不安全).
谢谢!