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

时间:2015-03-02 14:27:40

标签: sparql jena triplestore stardog federated-queries

我尝试通过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:queryAuthUser srv: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$1@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}

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

1 个答案:

答案 0 :(得分:2)

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

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

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

[] 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服务器。

相关问题