中国IT动力,最新最全的IT技术教程
最新100篇 | 推荐100篇 | 专题100篇 | 排行榜 | 搜索 | 在线API文档 | 网通镜像
首 页 | 程序开发 | 操作系统 | 软件应用 | 图形图象 | 网络应用 | 精文荟萃 | 教育认证 | 硬件维护 | 未整理篇 | 站长教程
ASP JS PHP工程 ASP.NET 网站建设 UML J2EESUN .NET VC VB VFP 网络维护 数据库 DB2 SQL2000 Oracle Mysql
服务器 Win2000 Office C DreamWeaver FireWorks Flash PhotoShop 上网宝典 CorelDraw 协议大全 网络安全 微软认证
硬件维护  CPU  主板  硬盘  内存  显卡  显示器  键盘鼠标  声卡音箱  打印机  机箱电源  BIOS  网卡  C#  Java  Delphi  vs.net2005
  当前位置:> 程序开发 > 编程语言 > Java > 综合文章
服务器是怎么要求客户端强行弹出身份验证窗口的
作者:未知 时间:2005-07-27 22:47 出处:CSDN 责编:chinaitpower
              摘要:服务器是怎么要求客户端强行弹出身份验证窗口的

我们访问tomcat服务器的时候如果试图访问Tomcat Manager就会发现浏览器弹出一个登陆对话框,和我们平常的网页对话框不同,而且查看页面的时候查不到生成这个对话框的代码,禁止脚本也毫无作用。手头的资料对这个东西没有任何介绍,它到底是怎么弹出来的呢?


用jmeter创建一个http request的sampler,再建一个view result tree的lisenter来看看服务器返回了些什么污七八糟的:

HTTP response headers:
HTTP/1.1 401 Unauthorized
Pragma: No-cache
Cache-Control: no-cache
Expires: Thu, 01 Jan 1970 08:00:00 CST
WWW-Authenticate: Basic realm="Tomcat Manager Application"
Content-Type: text/html;charset=utf-8
Content-Length: 954
Date: Thu, 30 Jun 2005 09:27:26 GMT
Server: Apache-Coyote/1.1


嘿嘿,原来是一个带WWW-Authenticate的401错误啊。自己写个jsp模拟一下看看怎么样:

<%
response.addHeader("WWW-Authenticate","Basic realm=\"Tomcat Manager Application\"");
response.sendError(401,"Unauthorized");
%>

果然,一个一模一样的登陆窗口跳出来了。不过身份验证的具体过程要怎么做呢?google了一下,果然看到好东西了: http://www.chinadata.cn/showContent.asp?projectID=2083



按照说明构造了一个jsp文件:

<%
sun.misc.BASE64Decoder decoder 
= new sun.misc.BASE64Decoder();
boolean authenticated 
= false;
String authorization 
= request.getHeader("authorization");
System.
out.println("authorization:"+authorization);
if (authorization != null{
  
if (authorization.startsWith("Basic")){
    authorization 
= authorization.substring(authorization.indexOf(' ')+1);
    
byte[] bytes = decoder.decodeBuffer(authorization);
    authorization 
= new String(bytes);
    String userName 
= authorization.substring(0,authorization.indexOf(':'));
    String password 
= authorization.substring(authorization.indexOf(':')+1);
    System.
out.println("userName:"+userName);
    System.
out.println("password:"+password);
    authenticated 
=userName.equals("abc"&& password.equals("abc");
  }
else if (authorization.startsWith("Digest")){
    String userName 
= authorization.substring(authorization.indexOf("username="));
    userName 
= userName.substring("username=\"".length());
    userName = userName.substring(0,userName.indexOf('"'));
    String password 
= authorization.substring(authorization.indexOf("response="));
    password 
= password.substring("response=\"".length());
    password = password.substring(0,password.indexOf('"'));
    authenticated 
=userName.equals("abc"&& password.equals("3cf1135d3b8e20dd9272d06288569a56");
  }

}

if (!authenticated){
  
//   response.addHeader("WWW-Authenticate","Digest realm=\"Tomcat Manager Application\"");
  response.addHeader("WWW-Authenticate","Basic realm=\"Tomcat Manager Application\"");
  response.sendError(
401,"Unauthorized");
}
else{
  
out.println("hello abc");
}

%>



cool,和tomcat一模一样的登陆页面做出来了。

用户名密码均为abc,hard code在代码里面了。不过还没查到Digest方式的情况下是对哪些信息进行MD5的


关闭本页
 
首页 | 投资与合作 | 服务条款 | 隐私政策 | 收藏本站 | 设为首页 | 新用户注册 | 免责声明 | 使用帮助
Copyright ©2005-2008 chinaitpower.com All rights reserved. www.chinaitpower.com 版权所有