Replicator/ReplicatorBuilder 忽略 URI 凭据
Replicator/ReplicatorBuilder ignores URI credentials
我目前使用的是 Cloudant-sync android 最新版本:2.0.2 和目标 Android api 21+.
我的服务器数据库是 CouchDB 2.1.0
我正在尝试从我的 android 应用触发复制拉取,以更新本地数据库。
我将源 URI 提供给 ReplicatorBuilder,例如:
"https: //user:password@databaseurl/db_name"
(如此处所述 https://github.com/cloudant/sync-android/blob/master/doc/replication.md)
但是在生成的复制器对象中,如果我检查 "replicator.strategy.sourceDb" db uri 只是
"https: //databaseurl/db_name" 所以,我得到了解除授权(如果我停用数据库服务器上的安全检查,它就可以正常工作)
这是我的 运行 代码:
public ReplicationListener pullAllAsync(Databases db_name) {
try {
URI uri = db_name.getServerURI();
DocumentStore ds = DocumentStore.getInstance(new File(db_name.getMobilePath(), db_name.toString()));
Replicator replicator = ReplicatorBuilder.pull().from(uri).to(ds).build();
ReplicationListener listener = new ReplicationListener(replicator);
replicator.getEventBus().register(listener);
replicator.start();
return listener;
} catch (Exception e) {
e.printStackTrace();
LogRepository.getInstance().escribirLog(LogRepository.TipoLog.ERROR, null, e, true);
return null;
}
}
我是不是做错了什么?
谢谢!
好吧,问题似乎出在 CookieAuthentication(这是在 URI 中传递 creds 时 Replicator 的默认身份验证方法)
我真的不知道为什么会失败,因为 CouchDB 服务器实例配置为接受 CookieAuthentication。
无论如何,这可能不是最佳解决方案,但却是一个足够好的解决方法。我通过强制 BasicAuthentication 方法完成了这项工作,如下所示:
public ReplicationListener pullAllAsync(Databases db_name) {
try {
URI uri = db_name.getServerURI();
DocumentStore ds = DocumentStore.getInstance(new File(db_name.getMobilePath(), db_name.toString()));
Replicator replicator = ReplicatorBuilder
.pull()
.from(uri)
.to(ds)
//WORKAROUND: forces BasicAuthentication method instead of CookieAuthentication
.addRequestInterceptors(new BasicAuthInterceptor(uri.getRawUserInfo()))
.build();
ReplicationListener listener = new ReplicationListener(replicator);
replicator.getEventBus().register(listener);
replicator.start();
return listener;
} catch (Exception e) {
e.printStackTrace();
LogRepository.getInstance().escribirLog(LogRepository.TipoLog.ERROR, null, e, true);
return null;
}
}
我目前使用的是 Cloudant-sync android 最新版本:2.0.2 和目标 Android api 21+.
我的服务器数据库是 CouchDB 2.1.0
我正在尝试从我的 android 应用触发复制拉取,以更新本地数据库。 我将源 URI 提供给 ReplicatorBuilder,例如: "https: //user:password@databaseurl/db_name" (如此处所述 https://github.com/cloudant/sync-android/blob/master/doc/replication.md) 但是在生成的复制器对象中,如果我检查 "replicator.strategy.sourceDb" db uri 只是 "https: //databaseurl/db_name" 所以,我得到了解除授权(如果我停用数据库服务器上的安全检查,它就可以正常工作)
这是我的 运行 代码:
public ReplicationListener pullAllAsync(Databases db_name) {
try {
URI uri = db_name.getServerURI();
DocumentStore ds = DocumentStore.getInstance(new File(db_name.getMobilePath(), db_name.toString()));
Replicator replicator = ReplicatorBuilder.pull().from(uri).to(ds).build();
ReplicationListener listener = new ReplicationListener(replicator);
replicator.getEventBus().register(listener);
replicator.start();
return listener;
} catch (Exception e) {
e.printStackTrace();
LogRepository.getInstance().escribirLog(LogRepository.TipoLog.ERROR, null, e, true);
return null;
}
}
我是不是做错了什么?
谢谢!
好吧,问题似乎出在 CookieAuthentication(这是在 URI 中传递 creds 时 Replicator 的默认身份验证方法) 我真的不知道为什么会失败,因为 CouchDB 服务器实例配置为接受 CookieAuthentication。
无论如何,这可能不是最佳解决方案,但却是一个足够好的解决方法。我通过强制 BasicAuthentication 方法完成了这项工作,如下所示:
public ReplicationListener pullAllAsync(Databases db_name) {
try {
URI uri = db_name.getServerURI();
DocumentStore ds = DocumentStore.getInstance(new File(db_name.getMobilePath(), db_name.toString()));
Replicator replicator = ReplicatorBuilder
.pull()
.from(uri)
.to(ds)
//WORKAROUND: forces BasicAuthentication method instead of CookieAuthentication
.addRequestInterceptors(new BasicAuthInterceptor(uri.getRawUserInfo()))
.build();
ReplicationListener listener = new ReplicationListener(replicator);
replicator.getEventBus().register(listener);
replicator.start();
return listener;
} catch (Exception e) {
e.printStackTrace();
LogRepository.getInstance().escribirLog(LogRepository.TipoLog.ERROR, null, e, true);
return null;
}
}