于限制对web页面的访问的问题(30分)

  • 主题发起人 主题发起人 zhanggok
  • 开始时间 开始时间
Z

zhanggok

Unregistered / Unconfirmed
GUEST, unregistred user!
package coreservlets;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.Properties;
import sun.misc.BASE64Decoder;
/** Example of password-protected pages handled directly
* by servlets.
* <P>
* Taken from Core Servlets and JavaServer Pages
* from Prentice Hall and Sun Microsystems Press,
* http://www.coreservlets.com/.
* &amp;copy;
2000 Marty Hall;
may be freely used or adapted.
*/
public class ProtectedPage extends HttpServlet {
private Properties passwords;
private String passwordFile;
/** Read the password file from the location specified
* by the passwordFile initialization parameter.
*/

public void init(ServletConfig config)
throws ServletException {
super.init(config);
try {
passwordFile = config.getInitParameter("passwordFile");
passwords = new Properties();
passwords.load(new FileInputStream(passwordFile));
} catch(IOException ioe) {}
}

public voiddo
Get(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String authorization = request.getHeader("Authorization");
if (authorization == null) {
askForPassword(response);
} else
{
String userInfo = authorization.substring(6).trim();

BASE64Decoder decoder = new BASE64Decoder();
String nameAndPassword =
new String(decoder.decodeBuffer(userInfo));
int index = nameAndPassword.indexOf(":");
String user = nameAndPassword.substring(0, index);
String password = nameAndPassword.substring(index+1);
String realPassword = passwords.getProperty(user);
if ((realPassword != null) &amp;&amp;
(realPassword.equals(password))) {
String title = "Welcome to the Protected Page";
out.println(ServletUtilities.headWithTitle(title) +
"<BODY BGCOLOR=/"#FDF5E6/">/n" +
"<H1 ALIGN=CENTER>" + title + "</H1>/n" +
"Congratulations. You have accessed a/n" +userInfo+nameAndPassword.toString()+
"highly proprietary companydo
cument./n" +
"Shred or eat all hardcopies before/n" +
"going to bed tonight./n" +
"</BODY></HTML>");
} else
{
askForPassword(response);
}
}
}
// If no Authorization header was supplied in the request.

private void askForPassword(HttpServletResponse response) {
response.setStatus(response.SC_UNAUTHORIZED);
// Ie 401
response.setHeader("WWW-Authen
ticate",
"BASIC realm=/"privileged-few/"");
}
/** Handle GET and POST identically. */

public voiddo
Post(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
do
Get(request, response);
}
}

下面是passwords.properties的内容
#Passwords
#Thu Jan 15 14:42:50 CST 2004
bb=bbb
nathan=nathanpw
marty=martypw
lindsay=lindsaypw
下面是web.xml的内容
<servlet>
<servlet-name>
ProtectedPage
</servlet-name>
<servlet-class>
coreservlets.ProtectedPage
</servlet-class>
<init-param>
<param-name>
passwordFile
</param-name>
<param-value>
D:/Apache Tomcat 4.0/webapps/myapp/WEB-INF/classes/passwords.properties
</param-value>
</init-param>
</servlet>
当访问上面的servlet时会弹出一个登陆对话框要求输入用户名和密码.书中说是当访问上面的servle检查是否存在Authorization头如果存在则跳过"basic"对剩余的base64进行解码后生成username:password的字符串.我把字符串显示出来后发现YmI6Ym是未解码前的,bb:bbb是解码后的bb:bbb正好是passwords.properties文件中的内容,这时我就不明白Authorization头是如何和passwords.properties文件相关联的在初始化中只是读取passwords.properties文件,并且输入nathan和nathanpw也可以这就证明Authorization头遍历了passwords.properties这让我更不能理解.response.setStatus(response.SC_UNAUTHORIZED)是不是如果输入错误应弹出一个对话框,但并没有弹出是不是因为浏览器的不同或版本的原因书中用的是netscape .请高手指典.
 
下面一段就是把存放密码信息的文件读到properties里的操作过程。
try {
passwordFile = config.getInitParameter("passwordFile");
//得到"D:/Apache Tomcat 4.0/webapps/myapp/WEB-INF/classes/passwords.properties"
passwords = new Properties();
//新创建一个Properties的实例
passwords.load(new FileInputStream(passwordFile));
//把Properties文件的内容读到Properties里去。
} catch(IOException ioe) {}
参考一下我的笔记,也许对你有帮助:
http://www.delphibbs.com/keylife/iblog_show.asp?xid=913
 
存放密码信息的文件读到properties里这个我明白我只想知道为什么会到properties里面找密码信息而且还能遍历.
还有是在下面方法里弹出的对话框
private void askForPassword(HttpServletResponse response) {
response.setStatus(response.SC_UNAUTHORIZED);
// Ie 401
response.setHeader("WWW-Authen
ticate",
"BASIC realm=/"hjhjhjh/"");
}
但这个方法从代码上看根本就没有调用因在doGet方法中是通过
if (authorization == null) {
askForPassword(response);
}
来调用的但authorization!=null我测试过.所以askForPassword(response)根本就没有调用,但是事实上
askForPassword(response)被调用了我把其注释掉程序就没有对话框,不知是为什么.
 
如果你了解ini文件的操作,那么你也就明白properties文件是它类似的。
/// String realPassword = passwords.getProperty(user);
这行代码就是对应的根据用户名字去取密码。
///authorization!=null
我不知道你是怎么测出来它不为空的,
但是除非你已经调用过了askForPassword(response),
否则第一次的时候它肯定是空的。
 
接受答案了.
 
后退
顶部