如何在Coldfusion8中使用javaloader设置java库?

时间:2012-06-10 15:57:08

标签: java coldfusion coldfusion-8 bcrypt javaloader

我正在尝试让javaLoader在Coldfusion8应用程序中运行,我需要一些帮助才能让我跨越终点。

这是我到目前为止所做的:

application.cfc

...
THIS.mappings["/javaloader"] = GetDirectoryFromPath( GetCurrentTemplatePath() ) & "tools/javaloader";
... 

<cffunction name="onApplicationStart" returnType="boolean" output="false" hint="application initalizer">
    <cfscript>
    Application.str = structNew();
    Application.str.myJavaLoaderKey = "someUUID_javaloader";
    Application.str.jarPaths = arrayNew(1);
    </cfscript>
    <cfif ( NOT structKeyExists(server, Application.str.myJavaLoaderKey) )>
        <!--- add path to class files to jarPath Array --->
        <cfset Application.str.jarPaths[1] = expandPath("/classes/BCrypt.class")>
        <!--- this will map out to: ...htdocs/classes/BCrypt.class --->

        <cfif ( NOT structKeyExists(server, Application.str.myJavaLoaderKey) )>
            <cflock name="#Hash(Application.str.myJavaLoaderKey)#" type="exclusive" timeout="10">
                <cfset server[Application.str.myJavaLoaderKey] = createObject("component", "javaloader.JavaLoader")>
                <!--- tried .init(Application.str.jarPaths) here, but didn't do anything --->
            </cflock>
        </cfif>
    </cfif>
    <cfreturn true />
</cffunction>

这是按照herehere的说明完成的。

在我的 handler.cfc 中,我试图像这样访问javaloader和BCrypt类:

<cfsript>
    pass = "some_password";
    <!--- this is accessible --->
    cryptonite = server[Application.str.myJavaLoaderKey];
    <!--- now trying to call init() with respective path to create an instance --->
    <!--- BREAKS HERE --->
    bCrypt = cryptonite.init(Application.str.jarPaths[1]);

    hashed = bCrypt.hashpw(pass, bcrypt.gensalt());        
</cfscript>                             

我可以转储cryptonite变量,但是当我尝试创建BCrypt实例时,脚本失败了。

问题
我很高兴我做到这一点,但我现在已经坐了几个小时了,不知道我做错了什么。希望有更多洞察力的人能指出我的方向吗?

感谢您的帮助!

1 个答案:

答案 0 :(得分:2)

确定。有几个错误。

要使用Coldfusion8和BCrypt或您选择的Java类设置Javaloader,请执行以下操作:

1)将任何Java类(.java文件,而不是.class文件)放在webroot / htdocs(Apache)的文件夹中。我的BCrypt之路看起来像这样:

  htdocs/classes/jBCrypt/

2)为javaloader做同样的事情。我的路径看起来像这样:

  htdocs/tools/javaloader/

3)在 Application.cfc

<!--- create mapping to javaloder --->
<cfscript>        
    THIS.mappings["/javaloader"] = GetDirectoryFromPath( GetCurrentTemplatePath() ) & "tools/javaloader";
</cfscript>

<!--- Application start --->
<cffunction name="onApplicationStart" returnType="boolean" output="false" hint="">
    <cfscript>       
        <!--- store a UUID and emptry path array in Application scope --->
        Application.str = structNew(); 
        Application.str.myJavaLoaderKey = "your_uuid_javaloader";
        Application.str.jarPaths = arrayNew(1);
    </cfscript>
     <!--- check if exists --->
    <cfif ( NOT structKeyExists(server, Application.str.myJavaLoaderKey) )>

         <!--- put all paths to your .java files here, this is for JBCrypt --->
         <cfset Application.str.jarPaths[1] = expandPath("/classes/jBCrypt-0.3")>
         <cfif ( NOT structKeyExists(server, Application.str.myJavaLoaderKey) )>

            <cflock name="#Hash(Application.str.myJavaLoaderKey)#" type="exclusive" timeout="10">
                <!--- create javaloader object and init with all submitted paths --->
                <cfset server[Application.str.myJavaLoaderKey] = createObject("component", "javaloader.JavaLoader").init(sourceDirectories=Application.str.jarPaths )>
            </cflock>
        </cfif>
    </cfif>
</cffunction>

根据here,设置应位于应用程序范围内。这应该设置所有.java类,您现在可以从其他地方引用它们:

<cfscript>
    var pass = "a_password";
    javaLoader = server[Application.str.myJavaLoaderKey];
    // create an instance of javaloader-BCrypt
    bcrypt = javaLoader.create("BCrypt").init();
    // now you can call methods from bcrypt like so:
    hashed = bcrypt.hashpw(pass, bcrypt.gensalt());
</cfscript>

通过here阅读。事实证明你必须引用 .java 文件,而不是我最初做的 .class 文件。

以下链接也可能有所帮助:
http://blog.mxunit.org/2011/02/hashing-passwords-with-bcrypt-in.html
http://www.compoundtheory.com/javaloader/docs/
http://www.aliaspooryorik.com/blog/index.cfm/e/posts.details/post/using-bcrypt-in-coldfusion-10-370