使用Alamofire的Swift 2.0:如何使用X-WSSE授权发送http标头?

时间:2016-01-22 17:16:47

标签: ios swift http-headers alamofire

这是我的代码:

>   let uuid: CFUUIDRef = CFUUIDCreate(nil)
    let nonce: CFStringRef = CFUUIDCreateString(nil, uuid)
    let dateFormatter: NSDateFormatter = NSDateFormatter()
    let timestamp = NSDate()
    let secretKey = "DAqE6lBUgzxJRuR287GR"
    let username = "us_demo003"

    let formattedDate: String = dateFormatter.stringFromDate(timestamp)


    let text = String((nonce as String)+formattedDate+secretKey)

    let sha1 = text.sha1()

    let passwordDigest = sha1.toBase64()

    let headers = ["Username":username,"PasswordDigest": passwordDigest, "Nonce":(nonce as String), "Created":formattedDate]

    let theUrlString = "url here"

    manager.request(.GET, theUrlString, parameters: nil, encoding: ParameterEncoding.URL, headers: theHeaders).responseJSON { (result) -> Void in
        print("BEGIN")
        print("\n\n\n\n\n\n\n\nBEGIN\n\(result)\n\n\n\n\n\nEND")
        print("STOP")
    }

我得到的结果如下:

SUCCESS: {
data = "";
replyCode = 1;
replyText = "No Authentication Header";

}

我如何使用Alamofire发送身份验证标头?我已经检查了github上的文档。我做得不对劲。

由于

1 个答案:

答案 0 :(得分:1)

而是要创建Authorization标题,其中Authorization是字典键,WWSE profile=\"username_token\"是值。然后,您还需要将X-WSSE标题密钥设置为UsernameToken Username="admin", PasswordDigest="buctlzbeVflrVCoEfTKB1mkltCI=", Nonce="ZmMzZDg4YzMzYzRmYjMxNQ==", Created="2014-03-22T15:24:49+00:00"

let token = "some generated token"

let headers = [
    "Authorization": "WWSE profile=\"\(token)\""
    "X-WSSE": "UsernameToken Username=\"admin\", PasswordDigest=\"buctlzbeVflrVCoEfTKB1mkltCI=\", Nonce=\"ZmMzZDg4YzMzYzRmYjMxNQ==\", Created=\"2014-03-22T15:24:49+00:00\""
]

有关WSSE auth的更多信息,请访问here