gpt4 book ai didi

node.js - Graphdb.js Node.js - 使用 GraphDB 进行 Node 服务器身份验证

转载 作者:太空宇宙 更新时间:2023-11-03 22:57:06 24 4
gpt4 key购买 nike

我知道 GraphDB 本身提供了多种身份验证方式。假设我锁定了对 GraphDB 服务器的访问,只允许拥有凭据的用户访问它。假设我使用用户名和密码创建一个授权用户。

我正在使用 Node.js,特别是 graphdb.js 来建立不安全的连接。但是如何在 Node 服务器和 graphdb 服务器的通信之间添加身份验证?文档说:

If the library is going to be used against a secured server, then all API calls must be authenticated by sending an http authorization header with a token which is obtained after a call to rest/login/user_name with a password provided as a specific header. In case the server requires that requests should be authenticated, then in the ServerClientConfig and RepositoryClientConfig must be configured the username and password which to be used for the authentication. If those are provided, then the client assumes that authentication is mandatory and the login with the provided credentials is performed automatically before the first API call. After a successful login, user details which are received and the auth token are stored in the AuthenticationService. From that moment on, with every API call is sent also an authorization header with the token as value. If the token expires, then the first API call will be rejected with an http error with status 401. The client handles this automatically by re-login the user with the same credentials, updates the stored token and retries the API call. This behavior is the default and can be changed if the ServerClientConfig or RepositoryClientConfig are configured with keepAlive=false.

那么示例代码需要遵循哪些编码步骤。我还没有在某处看到这样做的例子。有人可以帮忙吗?

最佳答案

除了 @Konstantin Petrov 所说的之外,我还想提一下,使用 graphdbjs 进行 native 身份验证是一项仍在开发中的功能。您可以关注PR还将添加示例。在此之前,可以通过发出登录请求并使用随响应返回的授权 token 来创建配置有授权 header 和 token 的 RDFRepositoryClient 实例来解决此问题。下面给出了一个示例。

const {RepositoryClientConfig, RDFRepositoryClient} = require('graphdb').repository;
const {RDFMimeType} = require('graphdb').http;
const {SparqlJsonResultParser} = require('graphdb').parser;
const {GetQueryPayload, QueryType} = require('graphdb').query;
const axios = require('axios');

axios.post('http://localhost:7200/rest/login/admin', null, {
headers: {
'X-GraphDB-Password': 'root'
}
}).then(function(token) {
const readTimeout = 30000;
const writeTimeout = 30000;
const repositoryClientConfig = new RepositoryClientConfig(['http://localhost:7200/repositories/testrepo'], {
'authorization': token.headers.authorization
}, '', readTimeout, writeTimeout);
const repositoryClient = new RDFRepositoryClient(repositoryClientConfig);
repositoryClient.registerParser(new SparqlJsonResultParser());

const payload = new GetQueryPayload()
.setQuery('select * where {?s ?p ?o}')
.setQueryType(QueryType.SELECT)
.setResponseType(RDFMimeType.SPARQL_RESULTS_JSON)
.setLimit(100);
return repositoryClient.query(payload);
})
.then(function(stream) {
// here is the query response stream
})
.catch(function(error) {
console.log('error', error);
});

关于node.js - Graphdb.js Node.js - 使用 GraphDB 进行 Node 服务器身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58731559/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com