簡體   English   中英

Graphdb.js Node.js - 使用 GraphDB 進行節點服務器身份驗證

[英]Graphdb.js Node.js - Node Server Authentication with GraphDB

我知道 GraphDB 本身提供了幾種身份驗證方式。 假設我鎖定了對 GraphDB 服務器的訪問,並且只允許具有憑據的用戶訪問它。 假設我使用用戶名和密碼創建了一個授權用戶。

我正在使用 Node.js 尤其是 graphdb.js 來建立不安全的連接。 但是如何在節點服務器和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. 如果服務器要求對請求進行身份驗證,則必須在 ServerClientConfig 和 RepositoryClientConfig 中配置用於身份驗證的用戶名和密碼。 如果提供了這些,則客戶端假定身份驗證是強制性的,並在第一次 API 調用之前自動執行使用提供的憑據登錄。 成功登錄后,收到的用戶詳細信息和身份驗證令牌存儲在 AuthenticationService 中。 從那一刻起,每個 API 調用都會發送一個授權 header ,並以令牌作為值。 如果令牌過期,則第一個 API 調用將被拒絕,並出現 http 錯誤,狀態為 401。客戶端通過使用相同的憑據重新登錄用戶來自動處理此問題,更新存儲的令牌並重試 ZDB974238714CA8ACE404 調用。 此行為是默認行為,如果 ServerClientConfig 或 RepositoryClientConfig 配置為 keepAlive=false,則可以更改此行為。

那么需要遵循的示例代碼的編碼步驟是什么。 我還沒有在某處看到這樣做的例子。 有人可以幫忙嗎?

除了@Konstantin Petrov 所說的之外,我還要提到 graphdbjs 的本機身份驗證是一項仍在進行中的功能。 您可以關注PR那里也會添加示例。 在此之前,可以通過發出登錄請求並使用響應返回的授權令牌來創建一個配置了授權 header 和令牌的 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);
});

啟用安全性后,您必須通過傳遞 JWT 令牌來授權請求。 要接收 JWT 令牌,您可以以管理員用戶身份發送請求。 為簡單起見,所有示例都使用 curl,但想法是相同的。

POST 'http://localhost:7200/rest/login/admin' -H 'X-GraphDB-Password: root' --compressed

返回的 header 包括 JWT 令牌:

-H 'Authorization: GDB eyJ1c2VybmFtZSI6ImFkbWluIiwiYXV0aGVudGljYXRlZEF0IjoxNTQwODA1MTQ2MTE2fQ==.K0mo2dSa/Gw+AR995qTrsA1zJGwlfOVEaIokZnhINh0='

您可以在下一個請求中使用該令牌:

curl 'http://localhost:7200/rest/monitor/query/count' -H 'Authorization: GDB eyJ1c2VybmFtZSI6ImFkbWluIiwiYXV0aGVudGljYXRlZEF0IjoxNTQwODA1MTQ2MTE2fQ==.K0mo2dSa/Gw+AR995qTrsA1zJGwlfOVEaIokZnhINh0=' -H 'X-GraphDB-Repository: news' --compressed

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM