我是编程新手,在Apache Olingo上进行基本身份验证时遇到了一些困难。
问题是我正在尝试使用基本身份验证访问ODATA。 当我尝试使用自己的凭据访问链接时,它工作正常。不幸的是,当我试图黯然失色时,我收到一条错误消息。 “主机名证书不匹配”。
这是我的代码。希望你能帮助我。
String serviceRoot = "";// URL that I'm trying to access
URI ExerciseUri = new URI(serviceRoot);
ODataClient client = ODataClientFactory.getV4();
client.getConfiguration().setHttpClientFactory(new BasicAuthHttpClientFactory("USERNAME", "PASSWORD"));
ODataRetrieveResponse<ODataEntity> responseExercise = client.getRetrieveRequestFactory()
.getEntityRequest(ExerciseUri).execute();
PS:我已经检查了该站点并找到了解决方案,但仍然无法正常工作。另外,我正在使用Java。
答案 0 :(得分:1)
Olingo客户端库使用Apache HttpComponents调用Dynamics。该错误消息表明SSL证书在客户端和服务器之间的握手期间无效。您可能要检查Java的SSL存储设置。张贴一些异常的堆栈跟踪信息对于人们更深入地研究问题很有用。
答案 1 :(得分:0)
尝试以下代码...
CustomeBasicAuthHttpClientFactory Customfact =
new CustomeBasicAuthHttpClientFactory(username, password);
client.getConfiguration().setHttpClientFactory(Customfact);
import java.util.ArrayList;
import javax.net.ssl.SSLContext;
import org.apache.http.*;
import org.apache.olingo.client.core.http.DefaultHttpClientFactory;
import org.apache.olingo.commons.api.http.HttpMethod;
public class CustomeBasicAuthHttpClientFactory extends DefaultHttpClientFactory {
private final String username;
private final String password;
public CustomeBasicAuthHttpClientFactory(String username, String password) {
this.username = username;
this.password = password;
}
public DefaultHttpClient create(HttpMethod method, URI uri) {
Logger.logMessage(LogLevel.LEVEL_DEBUG, ODATAstepAdaptor.ODATA_CLIENT_LOG_MODULE, "In side:create(HttpMethod method, URI uri)");
DefaultHttpClient client=null;
String[] tlsProtocols=null;
String[] cipherSuites=null;
try {
String filepath=System.getProperty("com.xxx.xxxx.home");
Logger.logMessage(LogLevel.LEVEL_DEBUG, ODATAstepAdaptor.ODATA_CLIENT_LOG_MODULE, "FilePath=>"+filepath+"/java/conf/tlsProtocols.dat");
BufferedReader in = new BufferedReader(new FileReader(filepath+"/java/conf/tlsProtocols.dat"));
String str=null;
ArrayList<String> lines = new ArrayList<String>();
while((str = in.readLine()) != null){
lines.add(str);
}
in.close();
tlsProtocols = lines.toArray(new String[lines.size()]);
SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(null, null, null);
//SSLSocketFactory sf = new SSLSocketFactory(sslContext);
SSLSocketFactory sf = new CustomizedSSLSocketFactory(sslContext,null,tlsProtocols,cipherSuites);
sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
HttpParams params = new BasicHttpParams();
HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
HttpProtocolParams.setContentCharset(params, HTTP.UTF_8);
SchemeRegistry registry = new SchemeRegistry();
registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
registry.register(new Scheme("https", sf, 443));
ClientConnectionManager ccm = new ThreadSafeClientConnManager(params, registry);
client=new DefaultHttpClient(ccm, params);
client.getCredentialsProvider().setCredentials(new AuthScope(uri.getHost(), uri.getPort()), new UsernamePasswordCredentials(this.username, this.password));
} catch (Exception e) {
Logger.logMessage(LogLevel.LEVEL_ERROR, ODATAstepAdaptor.ODATA_CLIENT_LOG_MODULE,e.getMessage());
}
return client;
}
}