具有安全 SPARQL 端点的联合查询

Federated query with secured SPARQL endpoint

我正在尝试通过 Fuseki 端点对 Jena 使用联合查询。在我的 SPARQL 查询中使用 SERVICE 关键字,我正在连接到一个 Stardog 端点。由于它是安全的 URL,端点指定如下:http://admin:admin@url。由于这不安全,Jena 会显示以下消息:

Code: 36/HAS_PASSWORD in USER: Including passwords in URIs is deprecated.

根据 docs,您可以为凭据指定 srv:queryAuthUsersrv:queryAuthPwd。有没有办法直接在 SPARQL 查询中执行此操作?或者,可以在Fuseki(ttl文件)中配置吗?

编辑

当我使用@RobV 的解决方案时,服务上下文似乎没有被拾取。这是上下文的样子:

symbol:http://jena.hpl.hp.com/ARQ#regexImpl = symbol:http://jena.hpl.hp.com/ARQ#javaRegex
symbol:http://jena.hpl.hp.com/ARQ#constantBNodeLabels = true
symbol:http://jena.hpl.hp.com/ARQ#strictGraph = false
symbol:http://jena.hpl.hp.com/ARQ#strictSPARQL = false
symbol:http://jena.hpl.hp.com/ARQ#stageGenerator = com.hp.hpl.jena.tdb.solver.StageGeneratorDirectTDB@6f2dd58d
symbol:http://jena.hpl.hp.com/ARQ#enablePropertyFunctions = true
symbol:http://jena.hpl.hp.com/ARQ#romanNumerals = false
symbol:http://jena.hpl.hp.com/ARQ#optFilterPlacement = false
symbol:http://jena.hpl.hp.com/ARQ#registryPropertyFunctions = com.hp.hpl.jena.sparql.pfunction.PropertyFunctionRegistry@6f05ca41
symbol:http://jena.hpl.hp.com/ARQ/system#opExecutorFactory = com.hp.hpl.jena.tdb.solver.OpExecutorTDB@2a1f5501

当我按原样保留 Fuseki 配置并在我的业务层中添加服务上下文时,似乎确实添加了服务上下文:

symbol:http://jena.hpl.hp.com/Service#serviceContext = {http://host:5820/db/query=symbol:http://jena.hpl.hp.com/Service#queryAuthPwd = usr
symbol:http://jena.hpl.hp.com/Service#queryAuthUser = pwd}

无论如何,我在执行联合查询时仍然收到未经授权的消息。

不,没有办法直接在 SPARQL 查询中执行此操作

理论上,您可以使用 Fuseki 配置文件中的 ja:context 属性 来指定上下文属性。但是实际上这不起作用,因为服务上下文是嵌套上下文,而汇编程序目前不支持嵌套上下文。

然而你可以做的是使用 ja:loadClass 机制来加载你添加到 class 路径的自定义 class 进行必要的静态初始化,例如

[] rdf:type fuseki:Server ;
   ja:loadClass "com.example.YourInitializer" ;
   fuseki:services (
     # Whatever services you are defining
   ) .

请注意,您必须将您的初始化程序与代表 fuseki:Server 实例的主题相关联,否则 ja:loadClass 三元组可能无法处理。

然后:

package org.apache.jena.playground;

import java.util.HashMap;
import java.util.Map;

import com.hp.hpl.jena.query.ARQ;
import com.hp.hpl.jena.sparql.engine.http.Service;
import com.hp.hpl.jena.sparql.util.Context;

public class StardogInitializer {

    public static void init() {
        // Prepare Stardog specific context
        Context stardogContext = new Context();
        stardogContext.set(Service.queryAuthUser, "admin");
        stardogContext.set(Service.queryAuthPwd, "admin");

        // Associate context with your endpoint URL
        Map<String, Context> serviceContexts = new HashMap<>();
        // temp here is the name of the Stardog database to be queried
        serviceContexts.put("http://localhost:5820/temp/query", stardogContext);

        // Associate service contexts with the global ARQ context
        ARQ.getContext().set(Service.serviceContext, serviceContexts);
    }
}

请注意,该方法需要是静态的,并且需要调用 init() 以便 Fuseki 调用它。

通过这个修改后的设置,我能够成功查询我的本地 Stardog 服务器。