中文使用

2020-10-26  本文已影响0人  盗生一

今天试了下HTTP认证的资料. 主要是基本认证与摘要认证.
其中基本认证是指 Base64(user:pwd)后,放在Http头的Authorization中发送给服务端来作认证.
用Base64纯只是防君子不防小人的做法。所以只适合用在一些不那么要求安全性的场合。
不过如果是做WebAPI不是网页,且web服务器是自己可以控制的,其实没必要那么死板。
我就试了把Base64(AES(user:pwd))后,按基本认证的方式传参数。然后Web服务再依次解码得
到user与pwd做认证,只要两边都约定好,是没什么问题的。

至于摘要认证就要复杂点,我把维基百科的上东西拿过来做下笔记:

  1. Client -------> WebServer
    点链接或发请求
    /
    GET /dir/index.html HTTP/1.0
    Host: localhost
  1. Client <------- WebServer
    返加401 "Unauthorized" 响应码
    认证域(realm)
    服务端密码随机数(nonce)
    ///
    HTTP/1.0 401 Unauthorized
    Server: HTTPd/0.9
    Date: Sun, 10 Apr 2005 20:26:47 GMT
    WWW-Authenticate: Digest realm="testrealm@host.com",
    qop="auth,auth-int",
    nonce="dcd98b7102dd2f0e8b11d0f600bfb0c093",
    opaque="5ccc069c403ebaf9f0171e9517f40e41"
    Content-Type: text/html
    Content-Length: 311
    ///
  2. Client -------> WebServer
    返回同样的header,但多加一个认证头包括了响应代码
    //
    GET /dir/index.html HTTP/1.0
    Host: localhost
    Authorization: Digest username="Mufasa",
    realm="testrealm@host.com",
    nonce="dcd98b7102dd2f0e8b11d0f600bfb0c093",
    uri="/dir/index.html",
    qop=auth,
    nc=00000001,
    cnonce="0a4f113b",
    response="6629fae49393a05397450978507c4ef1",
    opaque="5ccc069c403ebaf9f0171e9517f40e41"
  1. Client <------- WebServer
    ///
    HTTP/1.0 200 OK
    Server: HTTPd/0.9
    Date: Sun, 10 Apr 2005 20:27:03 GMT
    Content-Type: text/html
    Content-Length: 7984
    ///

客户端在后续提交请求时,
重复用服务器密码随机数(nonce)
每次产生新的客户端密码随机数(cnonce)
计数器(nc)++
然后依 保护质量(qop)类型,确定response 值

服务端收到后,依qop类型,用同样的算法,算出值与客户端带过来的response值对比。

nonce过期处理:
当服务端密码随机数nonce过期时,
返回 401,并在认证头中添加stale=TRUE

// ":"作为各个参数的分割符
HA1 : md5("username:realm:pwd")
HA2 : md5("GET:/xx/xx/index.html")

qop:为"auth"或"auth-int"
Response = md5("HA1:nonce:nc:cnonce:qop:HA2")
qop:未指定
Response = md5("HA1:nonce:HA2")

     上面两种认证方式其实都是通过Authenticate传参数,只不过定义不同罢了。要是自己写Web服务,两边约定好。
上一篇 下一篇

猜你喜欢

热点阅读