Javascript调用java applet签名

时间:2015-05-22 17:13:23

标签: java javascript applet

我的功能小程序

public String sign(String data) {
    String rs = "";
    if (data != null && !data.isEmpty()) {
        loadKeyStore();
        if (lsCertificateId.size() > 0) {
            selectCertId();
        //doSign
            if (password != null && !password.isEmpty()
            && selectedId != null && !selectedId.isEmpty()) {
                byte[] signedData = doSign(data.getBytes());
                rs = Base64.encode(signedData);
            }
        }
    }
    return rs;
}

我从javascript调用函数applet,我允许在浏览器上运行Java(TM)

<html>
    <head>
        <title>TODO supply a title</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
    </head>
    <body>
        <script type="text/javascript" src="jquery-2.1.1.min.js" ></script>
        <form>
            <textarea id="txtData" ></textarea>
            <br/>
            <input type="button" id="btnSign" value="Sign"  />
            <br/>
            <div id="lblSignedData"></div>
            <!--            <object id="appSign" width="300" height="100" type="application/x-java-applet" >
                            <param name="scriptable" value="true" />
                            <param name="mayscript" value="true" />
                            <param name="archive" value="applet/DsApplet.jar" />
                            <param name="code" value="dsapplet.DsApplet" />                
                        </object>-->
            <!--                        <object type="application/x-java-applet" height="300" width="550">
                                        <param name="code" value="Sample" />
                                        <param name="archive" value="Sample.jar" />
                                        Applet failed to run.  No Java plug-in was found.
                                    </object>-->
            <applet width="1" height="1" id="appSign" 
                    archive="applet/DsApplet.jar"
                    code="com.alupvn.digitalbill.applet.SignerApplet">
                <param name="signedAlgorithm" value="SHA1withRSA" />
                <param name="dlls" value=" vdctdcsp11.dll,vnpt-ca_csp11.dll,BkavCA.dll,vnpt-ca_v34.dll,viettel-ca.dll,ShuttleCsp11_3003.dll,ngp11v211.dll,st3csp11.dll,gclib.dll,fpt-ca.dll,CA2_v34.dll,CA2_csp11.dll,psapkcs.dll,ostc1_csp11.dll,fpt-ca-stx.dll,viettel-ca_v1.dll,viettel-ca_v2.dll,viettel-ca_v3.dll,etpkcs11.dll,U1000AUTO.dll,safe-ca.dll,eToken.dll,Vina-CA.dll,Vina-CA_s.dll,vnpt-ca_cl_v1.dll,ostt1_csp11.dll,ostt2_csp11.dll,ostt3_csp11.dll,ostc2_csp11.dll,viettel-ca_v4.dll,viettel-ca_v5.dll,viettel-ca_v6.dll,Vina-CAv3.dll,Vina-CAv4.dll,Vina-CAv5.dll,nca_eps2k2a.dll,nca_eps2k3a.dll" />
            </applet>


            <script type="text/javascript">
                $("#btnSign").click(function() {
                    var dataSigned = document.appSign.sign($("#txtData").val());
                    $("#lblSignedData").html(dataSigned);
                    //                alert("adsf");
                });
            </script>
            <!--<script src="http://www.java.com/js/deployJava.js"></script>-->
            <!--            <script type="text/javascript">
                            var attributes = {id: 'appSign', code: 'applet.DsApplet',
                                archive: 'DsApplet.jar',
                                width: 300, height: 100};
                            var parameters = {jnlp_href: 'DsApplet.jnlp'};
                            deployJava.runApplet(attributes, parameters, '1.6');
                            alert(document.appSign.getArch()) ;
                        </script>-->
        </form>
    </body>
</html>

当我打开HTML文件时,我收到错误:

var dataSigned = document.appSign.sign($("#txtData").val()); is not a function

有谁知道如何修复它?

1 个答案:

答案 0 :(得分:0)

  1. 使用证书对您的applet和所有.jar依赖项进行签名。
  2. 使用下面提到的所有标记填充您的清单(它在xml中,因为我使用maven,您可以按照自己喜欢的方式编写)
  3. <codebase>http://location.of.your.jar/</codebase>
    <permissions>all-permissions</permissions>
    <Application-Library-Allowable-Codebase>http://location.of.your.jar/</Application-Library-Allowable-Codebase>
    <Manifest-Version>1.0</Manifest-Version>
    <Implementation-Title>App Name</Implementation-Title>
    <Implementation-Version>0.1.0</Implementation-Version>
    <Application-Name></Application-Name>
    <Created-By>1.8.0_45</Created-By>
    <Main-Class>package.YourClass</Main-Class>
    <mode>development (or production)</mode>
    <url>url of the application</url>
    
    1. 使用doPrivileged
    2. 围绕您的java方法
    3. 确保您的浏览器启用了Java插件
    4. 将您的网络应用的http路径放在java exception list
    5. 如果您的网址 _ (下划线/下划线),则可能无法识别。
    6. 尝试将.jar移动到html的同一文件夹,而不是使用/ applet文件夹。
    7. 看看这个post,我遇到了类似的问题。
    8. 请记住,此错误表示&#39;不是功能&#39;是因为你的.jar没有加载 - 或者你用js语法搞错了,我不这么认为。