我使用spring resttemplate通过基本身份验证调用外部服务。
如下。
已延长createHttpContext
并覆盖protected HttpContext createHttpContext(HttpMethod httpMethod, URI uri) {
AuthCache authCache = new BasicAuthCache();
// Generate BASIC scheme object and add it to the local auth cache
BasicScheme basicAuth = new BasicScheme();
authCache.put(this.host, basicAuth);
// Add AuthCache to the execution context
HttpClientContext localcontext = new HttpClientContext();
localcontext.setAuthCache(authCache);
if (this.appProperties.getProperty(this.userName) != null) {
BasicCredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(
new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT,
AuthScope.ANY_REALM),
new UsernamePasswordCredentials(this.appProperties
.getProperty(this.userName), this.appProperties
.getProperty(this.password)));
localcontext.setCredentialsProvider(credsProvider);
}
return localcontext;
}
方法,如下所示。
clienthttprequestfactory
在调用方法中创建继承的 this.customFactory = new HttpComponentsClientHttpRequestFactoryExtended(
this.appProperties);
this.restTemplate.setRequestFactory(this.customFactory)
this.restTemplate.getForObject(url);
<platform name="ios">
<icon src="resources\ios\icon\icon.png" width="57" height="57"/>
<icon src="resources\ios\icon\icon@2x.png" width="114" height="114"/>
<icon src="resources\ios\icon\icon-40.png" width="40" height="40"/>
<icon src="resources\ios\icon\icon-40@2x.png" width="80" height="80"/>
<icon src="resources\ios\icon\icon-50.png" width="50" height="50"/>
<icon src="resources\ios\icon\icon-50@2x.png" width="100" height="100"/>
<icon src="resources\ios\icon\icon-57.png" width="57" height="57"/>
<icon src="resources\ios\icon\icon-60.png" width="60" height="60"/>
<icon src="resources\ios\icon\icon-60@2x.png" width="120" height="120"/>
<icon src="resources\ios\icon\icon-60@3x.png" width="180" height="180"/>
<icon src="resources\ios\icon\icon-72.png" width="72" height="72"/>
<icon src="resources\ios\icon\icon-72@2x.png" width="144" height="144"/>
<icon src="resources\ios\icon\icon-76.png" width="76" height="76"/>
<icon src="resources\ios\icon\icon-76@2x.png" width="152" height="152"/>
<icon src="resources\ios\icon\icon-120.png" width="120" height="120"/>
<icon src="resources\ios\icon\icon-152.png" width="152" height="152"/>
<icon src="resources\ios\icon\icon-small.png" width="29" height="29"/>
<icon src="resources\ios\icon\icon-small@2x.png" width="58" height="58"/>
<icon src="resources\ios\icon\icon-small@3x.png" width="87" height="87"/>
</platform>
<icon src="resources\ios\icon\icon-small@3x.png"/>
这给了我 401未经授权的错误。 但是相同的代码适用于不同的服务,基本身份验证工作正常