http認證(二) - DIGEST 認證

和講Basic篇的內容差不多,不同的是過程採用的是DIGEST認證:

[b]Tomcat配置:[/b]
1 在tomcat的webapps下新建一個目錄authen,再建立子目錄subdir,下面放一個index.jsp

2 在authen目錄下建立WEB-INF目錄,下放web.xml文件,內容如下

<security-constraint>
<web-resource-collection>
<web-resource-name>
My App
</web-resource-name>
<url-pattern>/subdir/*</url-pattern>
</web-resource-collection>
<auth-constraint>
<role-name>test</role-name>
</auth-constraint>
</security-constraint>

<login-config>
<auth-method>DIGEST</auth-method> <!-- DIGEST here -->
<realm-name>My Realm</realm-name>
</login-config>

3 在tomcat的tomcat-users.xml文件中添加一個用戶名密碼爲test,test的用戶,角色test。


[b]客戶端訪問:[/b]
訪問http://localhost:port/authen/subdir/index.jsp
會彈出對話框提示認證,輸入test test可以登錄。
[img]http://dl.iteye.com/upload/attachment/185241/62c76571-fdd2-3d32-a24d-014ab98ae923.jpg[/img]

[b]工作流程(通過firebug可以查看請求頭)[/b]
1 客戶端先發請求(不知道要認證,頭裏不包含任何特殊信息)

2 服務器發一個401返回,並含有下面的頭
[img]http://dl.iteye.com/upload/attachment/185243/1299fe63-d366-3052-9b0f-d1392e6e9f2b.jpg[/img]

3 客戶端認證,含有下面的頭
[img]http://dl.iteye.com/upload/attachment/185245/cbe8b59c-997c-3f76-9fcd-3c0cdb88a628.jpg[/img]
response="..."就是客戶端用來簽名的部分。

[b]缺點:[/b]
監聽到消息的攻擊者可以使用這個消息提交請求。

[b]httpclient中的實現[/b]
查看org.apache.commons.httpclient.auth包的DigestScheme類的authenticate方法。

RFC2617描述了計算方法:
A valid response contains a checksum (by default, the MD5 checksum) of the username, the password, the given nonce value, the HTTP method, and the requested URI.
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章