Worklight 单步验证和 SQL 适配器

Posted

技术标签:

【中文标题】Worklight 单步验证和 SQL 适配器【英文标题】:Worklight Single Step Authentication and SQL adapter 【发布时间】:2014-05-20 17:10:57 【问题描述】:

我正在开发基于 jQuery 的移动应用程序,并尝试使用我的自定义登录页面使用单步身份验证。登录页面从用户那里收集贷款号和密码,并使用它们来查询 MS SQL Server 数据库,如下所示。

我创建了 SQL 适配器来访问 MS SQL Server 数据库,当我选择适配器 -> 运行方式 -> 调用适配器过程时,它可以从工作区成功运行。但它不适用于我的 challaengerHandler.js 并引发异常。我已经将 sqljdbc4.jar 文件存储到项目的 server/lib 文件夹中。

这里是adapter.xml的内容


<displayName>LoanInfoMSSQLAdapter</displayName>
<description>LoanInfoMSSQLAdapter</description>
<connectivity>
    <connectionPolicy xsi:type="sql:SQLConnectionPolicy">
        <!-- Example for using a JNDI data source, replace with actual data source name -->
        <!-- <dataSourceJNDIName>java:/data-source-jndi-name</dataSourceJNDIName> -->

        <!-- Example for using mysql connector, do not forget to put the MySQL connector library in the project's lib folder -->
        <dataSourceDefinition>
           <driverClass>com.microsoft.sqlserver.jdbc.SQLServerDriver</driverClass>
           <url>jdbc:sqlserver://9.22.93.21:1433;database=CustDB</url>
           <user>lbps</user>
           <password>lbps</password>
        </dataSourceDefinition>
    </connectionPolicy>
    <loadConstraints maxConcurrentConnectionsPerNode="5" />
</connectivity>

<!-- Replace this with appropriate procedures -->
<procedure name="getLoanInfoFromDB"/>

这是来自adapter-impl.js的内容


var getLoanInfoStatement = WL.Server.createSQLStatement( 
"SELECT account_id, pin, borr_ssn " +
"FROM vw_wcc_cust " +
"WHERE vw_wcc_cust.account_id = ? AND pin = ?;"
);


function getLoanInfoFromDB(loan, pin) 
return WL.Server.invokeSQLStatement(
    preparedStatement : getLoanInfoStatement,
    parameters : [loan, pin]
);


这里是Challangerhandler.js的内容


$("#AuthSubmitButton").bind('click', function () 
WL.Logger.debug("SUBMIT button clicked...");

var loan = $("#AuthLoan").val();
var pin = $("#AuthPin").val();

WL.Logger.debug("AuthLoan = " + loan);
WL.Logger.debug("AuthPin = " + pin);

WL.Logger.debug("Invoking WL.Client.connect() function to check for connectivity...");

var connectOptions = 
    onSuccess : function() 
        WL.Logger.debug("Device is connected to WL server");

        var invocationData = 
            adapter : "SetSnapshotAuthAdapter",
            procedure : "submitAuthentication",
            parameters : [ loan, pin ]
        ;

            SetSnapshotAuthRealmChallengeHandler.submitAdapterAuthentication(invocationData, );
    , 
     onFailure: function()  
        WL.Logger.debug("Device is NOT connected to WL server");
        alert("Worklight Server Unavailable.  Try your request later-001");
       
;

WL.Client.connect(connectOptions);
);

这里是单步认证Adapeter的js文件的submitAuthentication()函数的内容


function submitAuthentication(loan, pin)

WL.Logger.info("Inside submitAuthentication");

WL.Logger.info("Loan = " + loan);
WL.Logger.info("Pin = " + pin);


var loanInfos = WL.Server.invokeSQLStatement(
    preparedStatement : getLoanInfoStatement,
    parameters : [loan, pin]
);




var username = "seterus";

// if (用户名 === "seterus" && 密码 === "seterus")

if (loanInfos) 

    var userIdentity = 
            userId: username,
            displayName: username, 
            attributes: 
                foo: "bar",
                loan: loan,
                pin: pin,
                isSuccessful: loanInfos.isSuccessful
            
    ;

    WL.Logger.info("Loan from userIdentity = " + userIdentity.attributes.loan);
    WL.Logger.info("Pin from userIdentity = " + userIdentity.attributes.pin);

    WL.Server.setActiveUser("SetSnapshotAuthRealm", userIdentity);

    WL.Logger.info("Returning authRequired as FALSE to display loan info...");

    return  
        authRequired: false
    ;


return onAuthRequired(null, "Invalid login credentials");


输入贷款# 和 pin 然后点击提交按钮后,我在 Eclipse 控制台视图中收到以下异常:


    [ERROR   ] FWLSE0099E: An error occurred while invoking procedure  [project SeterusSnapShotJQ]SetSnapshotAuthAdapter/SqlStatementFWLSE0100E:  parameters: [project SeterusSnapShotJQ]
   "arr": [
      
         "parameters": [
            "1111111",
            "3333"
         ],
         "preparedStatement": "SELECT account_id, pin, borr_ssn FROM vw_wcc_cust WHERE vw_wcc_cust.account_id = ? AND pin = ?;"
      
   ]


com.worklight.adapters.http.HttpClientContext cannot be cast to java.sql.Connection
FWLSE0101E: Caused by:  [project SeterusSnapShotJQ]nulljava.lang.ClassCastException: com.worklight.adapters.http.HttpClientContext cannot be cast to java.sql.Connection
    at com.worklight.adapters.sql.SQLQuery.invoke(SQLQuery.java:71)
    at com.worklight.integration.model.ProcedureInvoker.invokeProcedure(ProcedureInvoker.java:57)
    at com.worklight.integration.model.Procedure.invoke(Procedure.java:166)
    at com.worklight.integration.model.InvocationContext.call(InvocationContext.java:169)
    at com.worklight.integration.model.InvocationContext.call(InvocationContext.java:38)
    at java.util.concurrent.FutureTask$Sync.innerRun(Unknown Source)
    at java.util.concurrent.FutureTask.run(Unknown Source)
    at com.worklight.integration.model.InvocationContext$DirectExecutorService.execute(InvocationContext.java:284)
    at java.util.concurrent.AbstractExecutorService.submit(Unknown Source)
    at com.worklight.integration.model.InvocationContext.submit(InvocationContext.java:138)
    at com.worklight.integration.model.InvocationContextManager.submitInvocation(InvocationContextManager.java:58)
    at com.worklight.integration.services.impl.DataAccessServiceImpl.callProcedure(DataAccessServiceImpl.java:484)
    at com.worklight.integration.services.impl.DataAccessServiceImpl.access$100(DataAccessServiceImpl.java:56)
    at com.worklight.integration.services.impl.DataAccessServiceImpl$4.execute(DataAccessServiceImpl.java:387)
    at com.worklight.core.auth.impl.AuthenticationServiceBean.accessResource(AuthenticationServiceBean.java:76)
    at com.worklight.integration.services.impl.DataAccessServiceImpl.invokeProcedureInternal(DataAccessServiceImpl.java:384)
    at com.worklight.integration.services.impl.DataAccessServiceImpl.invokeDynamicProcedure(DataAccessServiceImpl.java:443)
    at com.worklight.integration.services.impl.DataAccessServiceImpl.invokeDynamicProcedure(DataAccessServiceImpl.java:427)
    at com.worklight.integration.js.javascriptIntegrationLibraryImplementation.invokeDynamicProcedure(JavaScriptIntegrationLibraryImplementation.java:125)
    at sun.reflect.GeneratedMethodAccessor206.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.mozilla.javascript.MemberBox.invoke(MemberBox.java:126)
    at org.mozilla.javascript.NativeJavaMethod.call(NativeJavaMethod.java:225)
    at org.mozilla.javascript.optimizer.OptRuntime.callN(OptRuntime.java:52)
    at org.mozilla.javascript.gen._integration_js_16._c_anonymous_19(/integration.js:226)
    at org.mozilla.javascript.gen._integration_js_16.call(/integration.js)
    at org.mozilla.javascript.optimizer.OptRuntime.call1(OptRuntime.java:32)
    at org.mozilla.javascript.gen.C_3A_5CUsers_5CIBM_ADMIN_5CDesktop_5CworkspaceJQuery_5CSeterusSnapShotJQ_5Cadapters_5CSetSnapshotAuthAdapter_SetSnapshotAuthAdapter_impl_js_78._c_submitAuthentication_2(C%3A%5CUsers%5CIBM_ADMIN%5CDesktop%5CworkspaceJQuery%5CSeterusSnapShotJQ%5Cadapters%5CSetSnapshotAuthAdapter/SetSnapshotAuthAdapter-impl.js:57)
    at org.mozilla.javascript.gen.C_3A_5CUsers_5CIBM_ADMIN_5CDesktop_5CworkspaceJQuery_5CSeterusSnapShotJQ_5Cadapters_5CSetSnapshotAuthAdapter_SetSnapshotAuthAdapter_impl_js_78.call(C%3A%5CUsers%5CIBM_ADMIN%5CDesktop%5CworkspaceJQuery%5CSeterusSnapShotJQ%5Cadapters%5CSetSnapshotAuthAdapter/SetSnapshotAuthAdapter-impl.js)
    at org.mozilla.javascript.ContextFactory.doTopCall(ContextFactory.java:394)
    at org.mozilla.javascript.ScriptRuntime.doTopCall(ScriptRuntime.java:3091)
    at org.mozilla.javascript.gen.C_3A_5CUsers_5CIBM_ADMIN_5CDesktop_5CworkspaceJQuery_5CSeterusSnapShotJQ_5Cadapters_5CSetSnapshotAuthAdapter_SetSnapshotAuthAdapter_impl_js_78.call(C%3A%5CUsers%5CIBM_ADMIN%5CDesktop%5CworkspaceJQuery%5CSeterusSnapShotJQ%5Cadapters%5CSetSnapshotAuthAdapter/SetSnapshotAuthAdapter-impl.js)
    at com.worklight.integration.js.JavaScriptManager.callFunction(JavaScriptManager.java:240)
    at com.worklight.integration.js.JavaScriptManager.invokeFunction(JavaScriptManager.java:214)
    at com.worklight.integration.js.JavaScriptManager.invokeFunction(JavaScriptManager.java:194)
    at com.worklight.integration.services.impl.AdapterManagerImpl.invokeFunction(AdapterManagerImpl.java:104)
    at com.worklight.integration.js.JavaScriptProcedureInvoker.invoke(JavaScriptProcedureInvoker.java:42)
    at com.worklight.integration.model.ProcedureInvoker.invokeProcedure(ProcedureInvoker.java:57)
    at com.worklight.integration.model.Procedure.invoke(Procedure.java:166)
    at com.worklight.integration.model.InvocationContext.call(InvocationContext.java:169)
    at com.worklight.integration.model.InvocationContext.call(InvocationContext.java:38)
    at java.util.concurrent.FutureTask$Sync.innerRun(Unknown Source)
    at java.util.concurrent.FutureTask.run(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at com.worklight.server.util.ProjectLocal$1RunnableWrapper.run(ProjectLocal.java:267)
    at java.lang.Thread.run(Unknown Source)

                                                                                                               com.worklight.common.log.filters.ErrorFilter

我尝试遵循 SQL 适配器示例以及单步身份验证适配器示例。该代码适用于单步身份验证适配器和硬编码数据,但一旦我尝试包含用于硬编码数据的 SQL 适配器,它就会给我这个问题。

有一些帖子有同样的问题,我试图按照那里的代码来实现它,但我仍然没有得到想要的结果。

如果您需要更多信息,请告诉我。如果由于我是 Stack Overflow 网站的新手,代码 sn-ps 在帖子中的表现不佳,请原谅我。

谢谢, 沙伊莱什

【问题讨论】:

您与数据库的连接在过去的某个时间点是否有效?您能否验证这不会发生在一个新的 SQL 适配器中,该适配器只是对数据库进行非常简单的查询,而没有与身份验证或您的应用程序相关的任何内容? 我成功连接到数据库并在测试应用程序中执行了查询。我使用了 Worklight InfoCenter 的“高级适配器使用和混搭”部分的示例练习。我在实际应用程序中使用了相同的样式,并且成功地从 Microsft SQL Server 检索记录。谢谢你的建议。 - Shailesh 你能把这个写成答案吗? 【参考方案1】:

我成功连接到数据库并在测试应用程序中执行了查询。我使用了 Worklight InfoCenter 的“高级适配器使用和混搭”部分的示例练习。我在实际应用程序中使用了相同的样式,并且成功地从 Microsft SQL Server 检索记录。谢谢你的建议。 - 沙伊莱什

【讨论】:

以上是关于Worklight 单步验证和 SQL 适配器的主要内容,如果未能解决你的问题,请参考以下文章

需要 Worklight 适配器身份验证

worklight http 适配器和 NTLM 身份验证

如何模拟 Worklight 适配器基本身份验证

IBM Worklight 6.0 - 如果客户端注销/登录,具有基本身份验证的适配器不会更新身份验证标头

使用适配器身份验证的 Worklight 推送通知不起作用

使用基于 Worklight 适配器的身份验证时如何获取正在注销的用户的 userIdentity