【发布时间】:2018-12-02 10:48:20
【问题描述】:
我正在开发一个 Vuejs 应用程序,该应用程序从 Apache Solr 索引中获取其数据,并且我的应用程序使用 Axios 与索引一起工作以建立连接。但是,添加了 HTTP 基本身份验证,我无法使身份验证正常工作。 Access-Control-Allow-Origin 标头在服务器上设置(它在将身份验证添加到索引之前工作),所以这不是问题。我正在尝试在axios.get() 的配置对象中使用auth 对象,如下所示:
const params = new URLSearchParams()
params.append('fl', 'label,bundle,entity_id,ss_language,url,is_product_slideshow')
if (this.nodeType) {
params.append('fq', 'bundle:' + this.nodeType)
} else {
params.append('fq', 'bundle:' + '(article OR document OR video OR product_display)')
}
params.append('fq', 'ss_language:' + this.language)
params.append('fq', 'bs_status:' + 1)
params.append('fq', '-is_field_restricted_to_internal_use:1')
params.append('fq', '-is_field_restricted_to_author_of_as:1')
params.append('fq', '-is_field_pp_confidential:1')
params.append('fq', '-is_field_pp_do_not_show_item_on_t:1')
params.append('q', qs)
params.append('sort', 'is_sort asc')
params.append('wt', 'json')
params.append('rows', 10)
params.append('start', queryStartRows)
axios.get('https://my-solr-url.com/solr/my-solr-index/select', {
params: params,
withCredentials: true,
auth: {
username: 'myusername',
password: 'mypassword'
}
})
.then(response => {
this.results = response.data.response.docs
var numFound = this.numFound = response.data.response.numFound
// Calculate last page.
var numFoundRounded = Math.floor(numFound / 10) * 10
var numPages = numFoundRounded / 10
this.lastPage = numPages
})
我可以让 Postman 的查询正常运行,所以我知道我有正确的凭据。
从我从文档和其他示例中可以看出,我做得对,但我仍然收到 401 错误。
我还使用 Drupal 站点中的这个索引,在那里,用户 ID 和密码作为 URL 的一部分传递:
https://myusername:mypassword@my-solr-url.com/solr/my-solr-index/select?...
但这种方法在这里也行不通。为了使身份验证正常工作,我需要做些什么不同的事情?
【问题讨论】:
-
您在 axios 请求中将数据与配置混合在一起。配置对象应该是
get请求的第三个参数:axios.get('http://example.com', {}, { auth: {...} })
标签: javascript http authentication axios basic-authentication