gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 07:45:18 25 4
gpt4 key购买 nike

我正在尝试让 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>

这是按照 here 的指示完成的和 here .

在我的 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 实例时,脚本失败。

问题:
我很高兴我做到了这一步,但我已经坐了几个小时了,不知道我做错了什么。希望有更多见解的人可以为我指明方向吗?

感谢您的帮助!

最佳答案

好的。有几个错误。

要使用 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

关于java - 如何在 Coldfusion8 中使用 javaloader 设置 java 库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10970133/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com