简体   繁体   English

带有Alamofire的Swift 2.0:如何通过X-WSSE授权发送HTTP标头?

[英]Swift 2.0 with Alamofire: How to send http headers with X-WSSE Authorization?

Here is my code: 这是我的代码:

>   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")
    }

The result I'm getting is the following: 我得到的结果如下:

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

} }

How exactly do I send an authentication header with Alamofire? 我如何使用Alamofire发送身份验证标头? I've checked the documentation on the github. 我已经检查了github上的文档。 Something I'm not doing right. 我做错了什么。

Thanks 谢谢

Instead you want to create the Authorization header where Authorization is the dictionary key and WWSE profile=\\"username_token\\" is the value. 而是要创建Authorization标头,其中Authorization是词典密钥,而WWSE profile=\\"username_token\\"是值。 Then you also need to set the X-WSSE header key to UsernameToken Username="admin", PasswordDigest="buctlzbeVflrVCoEfTKB1mkltCI=", Nonce="ZmMzZDg4YzMzYzRmYjMxNQ==", Created="2014-03-22T15:24:49+00:00" . 然后您还需要将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\""
]

More info about WSSE auth can be found here . 可以在此处找到有关WSSE auth的更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM