简体   繁体   English

无法从 java 程序执行 web-crypto 脚本

[英]Unable to execute web-crypto script from java program

Java Code: Java 代码:

import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
public class WebCryptoInvoke {
  public static void main(String[] args) throws Exception {
    ScriptEngineManager manager = new ScriptEngineManager();
    ScriptEngine engine = manager.getEngineByName("JavaScript");
    if (!(engine instanceof Invocable)) {
      System.out.println("Invoking methods is not supported.");
      return;
    }
    Invocable inv = (Invocable) engine;
    String scriptPath = "/home/rajasekhar/Desktop/webcrypto.js";

    engine.eval("load('" + scriptPath + "')");
    Object webCrypto = engine.get("webcrypto");
    Object result = inv.invokeMethod(webCrypto, "generateKeyPair");
    System.out.println(result);
  }
}

JavaScript Code : JavaScript 代码

"use strict";
var webcrypto = new Object();

webcrypto.generateKeyPair = function ()
{
 var result = {};

window.crypto.subtle.generateKey(
    {
        name: "RSASSA-PKCS1-v1_5",
        modulusLength: 2048, //can be 1024, 2048, or 4096
        publicExponent: new Uint8Array([0x01, 0x00, 0x01]),
        hash: {name: "SHA-256"}, //can be "SHA-1", "SHA-256", "SHA-384", or "SHA-512"
    },
    true, //whether the key is extractable (i.e. can be used in exportKey)
    ["sign", "verify"] //can be any combination of "sign" and "verify"
)
.then(function(key){
    //returns a keypair object
    console.log(key);
    console.log(key.publicKey);
    console.log(key.privateKey);
result[0] = key.publicKey;
result[1] = key.privateKey;
})
.catch(function(err){
    console.error(err);
});
    return result;
};

Error:错误:

Exception in thread "main" javax.script.ScriptException: ReferenceError: "window" is not defined in /home/rajasekhar/Desktop/webcrypto.js at line number 9 at jdk.nashorn.api.scripting.NashornScriptEngine.throwAsScriptException(NashornScriptEngine.java:470) at jdk.nashorn.api.scripting.NashornScriptEngine.invokeImpl(NashornScriptEngine.java:392) at jdk.nashorn.api.scripting.NashornScriptEngine.invokeMethod(NashornScriptEngine.java:199) at WebCryptoInvoke.main(WebCryptoInvoke.java:20) Caused by: /home/rajasekhar/Desktop/webcrypto.js:9 ReferenceError: "window" is not defined at jdk.nashorn.internal.runtime.ECMAErrors.error(ECMAErrors.java:57) at jdk.nashorn.internal.runtime.ECMAErrors.referenceError(ECMAErrors.java:319) at jdk.nashorn.internal.runtime.ECMAErrors.referenceError(ECMAErrors.java:291) at jdk.nashorn.internal.objects.Global.线程“main”javax.script.ScriptException 中的异常:ReferenceError:“window”未在 jdk.nashorn.api.scripting.NashornScriptEngine.throwAsScriptException(NashornScriptEngine. java:470) 在 jdk.nashorn.api.scripting.NashornScriptEngine.invokeImpl(NashornScriptEngine.java:392) 在 jdk.nashorn.api.scripting.NashornScriptEngine.invokeMethod(NashornScriptEngine.java:199) 在 WebCryptoInvoke.main(WebCryptoInvoke.java :20) 原因:/home/rajasekhar/Desktop/webcrypto.js:9 ReferenceError:“窗口”未在 jdk.nashorn 的 jdk.nashorn.internal.runtime.ECMAErrors.error(ECMAErrors.java:57) 中定义。在 jdk.nashorn.internal.runtime.ECMAErrors.referenceError(ECMAErrors.java:291) 在 jdk.nashorn.internal.objects.Global 的 internal.runtime.ECMAErrors.referenceError(ECMAErrors.java:319)。 noSuchProperty (Global.java:1441) at jdk.nashorn.internal.scripts.Script$Recompilation$2$86$webcrypto.generateKeyPair(/home/rajasekhar/Desktop/webcrypto.js:9) at jdk.nashorn.internal.runtime.ScriptFunctionData.invoke(ScriptFunctionData.java:637) at jdk.nashorn.internal.runtime.ScriptFunction.invoke(ScriptFunction.java:494) at jdk.nashorn.internal.runtime.ScriptRuntime.apply(ScriptRuntime.java:393) at jdk.nashorn.api.scripting.ScriptObjectMirror.callMember(ScriptObjectMirror.java:199) at jdk.nashorn.api.scripting.NashornScriptEngine.invokeImpl(NashornScriptEngine.java:386) ... 2 more jdk.nashorn.internal.scripts.Script的 noSuchProperty (Global.java:1441)$Recompilation$2$86$webcrypto.generateKeyPair(/home/rajasekhar/Desktop/webcrypto.js:9) 在 jdk.nashorn.internal.runtime.ScriptFunctionData .invoke(ScriptFunctionData.java:637) 在 jdk.nashorn.internal.runtime.ScriptFunction.invoke(ScriptFunction.java:494) 在 jdk.nashorn.internal.runtime.ScriptRuntime.apply(ScriptRuntime.java:393) 在 jdk。 nashorn.api.scripting.ScriptObjectMirror.callMember(ScriptObjectMirror.java:199) 在 jdk.nashorn.api.scripting.NashornScriptEngine.invokeImpl(NashornScriptEngine.java:386) ... 2 更多

tl;dr use javafx 2 https://github.com/openjdk/jfx/search?q=document , https://stackoverflow.com/a/11266979/11711280 tl;博士使用 javafx 2 https://github.com/openjdk/jfx/search?q=documenthttps://stackoverflow.com/a/11266979/11711280

WebEngine webEngine = webView.getEngine();
webEngine.getLoadWorker().stateProperty().addListener(...)
webEngine.loadContent("load('" + scriptPath + "')");

However: " The full Oracle Java Runtime 8 ships with Nashorn."但是:“完整的 Oracle Java Runtime 8 随 Nashorn 一起提供。” " Oracle Java dev 1.8 comes with its own bundled Java FX version , which generally can't be overridden. To use Java FX11, you will need to run the project with Java 11 and set the System property java.library.path to the location of the FX framework." " Oracle Java dev 1.8 自带捆绑的 Java FX 版本,一般无法覆盖。要使用 Java FX11,您需要使用 Java 11 运行项目并将系统属性 java.library.path 设置为该位置外汇框架。”

String scriptPath = "/home/rajasekhar/Desktop/webcrypto.js";
function ScriptWindow(scriptPath){
  return eval(scriptPath);
}.call({window:{}},[...(args=[])]);

I would expect static mutable ScriptEngine scope我希望静态可变ScriptEngine范围

engine.put("window", {});
engine.eval("load('" + scriptPath + "')");

or for all ScriptEngineManager engines或所有ScriptEngineManager引擎

ScriptContext ctx = new SimpleScriptContext();
Bindings globalBindings = new SimpleBindings();
ctx.setBindings(globalBindings, ScriptContext.GLOBAL_SCOPE);
ctx.setAttribute("window", {}, ScriptContext.GLOBAL_SCOPE);

to be accessible in the function over int declarations (or class instantiations) yet https://stackoverflow.com/a/53561009/11711280 seems like you can set initial window that web-crypto might append.可以通过int声明(或class实例化)在函数中访问,但https://stackoverflow.com/a/53561009/11711280似乎您可以设置 web-crypto 可能附加的初始window

public class Document {
};
Document window = new Document();

Hard to tell if more than mutable window or document properties are required here https://github.com/w3c/webcrypto/blob/main/spec/dfn.js很难判断这里是否需要更多可变windowdocument属性https://github.com/w3c/webcrypto/blob/main/spec/dfn.js

" When a script attempts to access a global variable not defined within it, nashorn searches for the variable in Bindings of the current ScriptContext used." 一个脚本试图访问一个未在其中定义的全局变量时,nashorn 会在当前使用的 ScriptContext 的绑定中搜索该变量。”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM