调用安全适配器返回响应错误 401

Posted

技术标签:

【中文标题】调用安全适配器返回响应错误 401【英文标题】:Calling a secure adapter returns Response Error 401 【发布时间】:2015-12-28 18:01:10 【问题描述】:

我正在尝试在我的手表套件应用程序中调用安全适配器。但我的请求失败,Xcode 的输出窗口中出现以下错误消息。

Request url is http://:10080/UProject/adapters/MyAdapter/getMyBalance
2015-12-28 21:49:29.222 My WatchKit 1 Extension[59266:1304116] [DEBUG] [WL_AFHTTPRequestOperationManagerWrapper_PACKAGE] -[WLAFHTTPRequestOperationManagerWrapper start] in WLAFHTTPRequestOperationManagerWrapper.m:320 :: Starting the request with URL http://:10080/UProject/adapters/MyAdapter/getMyBalance?param=700015005
2015-12-28 21:49:29.222 My WatchKit 1 Extension[59266:1304116] After adapter Request
2015-12-28 21:49:29.247 My WatchKit 1 Extension[59266:1304116] [DEBUG] [WL_AFHTTPRequestOperationManagerWrapper_PACKAGE] -[WLAFHTTPRequestOperationManagerWrapper requestFailed:error:] in WLAFHTTPRequestOperationManagerWrapper.m:352 :: Request Failed
2015-12-28 21:49:29.247 My WatchKit 1 Extension[59266:1304116] [DEBUG] [WL_AFHTTPRequestOperationManagerWrapper_PACKAGE] -[WLAFHTTPRequestOperationManagerWrapper requestFailed:error:] in WLAFHTTPRequestOperationManagerWrapper.m:353 :: Response Status Code : 401
2015-12-28 21:49:29.248 My WatchKit 1 Extension[59266:1304116] [DEBUG] [WL_AFHTTPRequestOperationManagerWrapper_PACKAGE] -[WLAFHTTPRequestOperationManagerWrapper requestFailed:error:] in WLAFHTTPRequestOperationManagerWrapper.m:354 :: Response Error : Request failed: unauthorized (401)




//WKInterfaceController
import WatchKit
import Foundation
//import IBMMobileFirstPlatformFoundation


class InterfaceController: WKInterfaceController 

@IBOutlet var btnLoadFeeds: WKInterfaceButton!
@IBOutlet var btnConnect: WKInterfaceButton!

var challengeHandler : ChallengeHandler?


@IBOutlet var feedsTable: WKInterfaceTable!

var feeds = []


override func awakeWithContext(context: AnyObject?) 
    super.awakeWithContext(context)

    // Configure interface objects here.

  btnLoadFeeds.setEnabled(false)

   let connectListener = MyConnectListener()
   WLClient.sharedInstance().registerChallengeHandler(MyChallengeHandler(vc: self))
   WLClient.sharedInstance().wlConnectWithDelegate(connectListener)

   


override func willActivate() 
    // This method is called when watch view controller is about to be visible to user
    super.willActivate()


override func didDeactivate() 
    // This method is called when watch view controller is no longer visible
    super.didDeactivate()




@IBAction func btnConnectClicked() 

    let url : NSURL = NSURL(string: "/adapters/SecureAdapter/getBalance")!
    let request : WLResourceRequest = WLResourceRequest(URL: url, method: WLHttpMethodGet)

    request.setQueryParameterValue("700015005", forName: "param")


    request.sendWithCompletionHandler 

        (response: WLResponse!, error: NSError!) -> Void in

        if error != nil
        
            NSLog("Adapter invocation failure. Error: %@", error.description)
         else
        
           NSLog(response.responseText)
        

    
    NSLog("After adapter Request")





@IBAction func btnLoadClicked() 
    btnConnect.setHidden(true)
    btnLoadFeeds.setHidden(true)

    if (feeds.count > 0 )
    
    feedsTable.setNumberOfRows(feeds.count, withRowType: "FeedRowController")


    for (index,feedItem) in feeds.enumerate()
    
     let row = feedsTable.rowControllerAtIndex(index) as! FeedRowController
        row.labelText.setText(feedItem["title"] as? String)
        print(feedItem)

    

    





func onFailure(response: WLFailResponse!) 
    print(" failure")



func onSuccess(response: WLResponse!) 
    print("success")
    print(response.responseText)

    feeds = response.getResponseJson()["Items"] as! NSArray



//我的挑战处理程序 导入基金会

类 MyChallengeHandler: ChallengeHandler

var challengeHandler : ChallengeHandler?

let vc : InterfaceController

init(vc: InterfaceController)
    self.vc = vc
    super.init(realm: "LDAPRealm")




override func isCustomResponse(response: WLResponse!) -> Bool 


    if response != nil && response.responseText != nil
        if response.responseText.lowercaseString.rangeOfString("j_security_check") != nil
            NSLog("Detected j_security_check string - returns true")

            return true
        
    
    return false


override func handleChallenge(response: WLResponse!) 
    NSLog("handleChallenge")
    vc.challengeHandler = self
    dispatch_async(dispatch_get_main_queue()) 
    self.challengeHandler?.submitLoginForm("j_security_check", requestParameters: ["j_username": "demouser", "j_password": "12345678"], requestHeaders: nil, requestTimeoutInMilliSeconds:0, requestMethod: "POST")
    



override func onSuccess(response: WLResponse!) 
    NSLog("zaq3 Challenge succeeded")
         self.submitSuccess(response)


override func onFailure(response: WLFailResponse!) 
    NSLog(" zaq3 Challenge failed")
    self.submitFailure(response)

谁能指出我在代码中缺少的内容。

【问题讨论】:

我通过更改 handleChallenge self.submitLoginForm("j_security_check", requestParameters: ["j_username": "demouser", "j_password": "12345678"], requestHeaders: nil, requestTimeoutInMilliSeconds:0 来修复它, requestMethod: "POST") 写下以上内容作为问题的答案,而不是评论。 【参考方案1】:

我通过更改 handleChallenge self.submitLoginForm("j_security_check", requestParameters: ["j_username": "demouser", "j_password": "12345678"], requestHeaders: nil, requestTimeoutInMilliSeconds:0, requestMethod: "POST ")

【讨论】:

请考虑使用正确的标记,例如backticks for code

以上是关于调用安全适配器返回响应错误 401的主要内容,如果未能解决你的问题,请参考以下文章

MobileFirst 适配器 - 为啥我在 MobileFirst 操作控制台的 Swagger 文档页面上发出请求时收到“401 Unauthorized”错误

Keycloak - 令牌过期后在 Ajax 调用中获取 401

NestJs Socket io 适配器:如果使用 false 调用 allowFunction,服务器不会将 CORS 标头添加到握手的响应中。错误或配置错误?

SpringBoot + Keycloak 适配器设置失败

将 cookie 附加到 WorkLight Adapter 响应标头

基于 Java 的适配器 - 找不到返回值