您的位置:首页 > 其它

关于web应用程序的安全验证

2007-10-19 13:18 447 查看
通常对于一般的web应用程序,都是自己写验证,输入用户名和密码,然后到数据库去验证,然后返回。但是对于安全性要求较高的应用,自己写的安全验证则会出现许多没考虑到的地方,这时应该使用安全框架。
我使用的是struts框架,服务器是weblogic8.14,配置了基于FORM的验证方式,具体配置如下:
1、目录结构:根目录文件:index.jsp,login.html,error.jsp
admin目录:存放系统模块业务页面的路径
pages目录:存放公用页面的路径
2、login.html:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<style type="text/css">
body{
margin:0;
font-size:12px;
height:100%;
}
#content {
position:absolute;
left:58%;
top:42%;
width:190px;
height:104px;
z-index:1;
}
.int{
width:120px;
height:13px;
border:#c0c0c0 1px solid;
}
.btn{padding-top:2px;}
</style>
</head>
<body>
<div id="content">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<form method="post" action="j_security_check">
<tr>
<td height="25" colspan="2" align="right" valign="bottom"> </td>
</tr>
<tr>
<td width="60" height="35" align="right" valign="bottom">用户id:</td>
<td width="120" valign="bottom"><input type="text" name="j_username" id="j_username" class="int"/></td>
</tr>
<tr>
<td width="60" height="25" align="right" valign="bottom">密码:</td>
<td valign="bottom"><input type="password" name="j_password" id="j_password" class="int"/></td>
</tr>
<tr>
<td colspan="2"> </td>
</tr>
<tr>
<td height="30" colspan="2" align="right">
<input type="submit" id="btnok" value="登录" class="btn"/>
<input id="reset" type="reset" value="取消" class="btn"/></td>
</tr>
</form>
</table>
</div>
<table width="100%" height="100%" border="0" cellpadding="0" cellspacing="0">
<tr>
<td><table width="601" height="364" border="0" align="center" cellpadding="0" cellspacing="0" background="images/login.jpg">
<tr>
<td> </td>
</tr>
</table></td>
</tr>
</table>
</body>
</html>

需要注意的是这里的用户名,密码和提交页面必须固定为j_username,j_password,j_security_check见文件的反显部分。

3、error.jsp
<%@ page contentType="text/html; charset=GBK" %>
<html>
<head>
<title>
error
</title>
</head>
<body bgcolor="#ffffff">
<h1>
Login error!
</h1>
</body>
</html>

4、在web.xml里配置要保护的资源
<security-constraint>
<web-resource-collection>
<web-resource-name>admin</web-resource-name>
<url-pattern>/admin/*</url-pattern>
<http-method>PUT</http-method>
<http-method>GET</http-method>
</web-resource-collection>
<web-resource-collection>
<web-resource-name>pages</web-resource-name>
<url-pattern>/pages/*</url-pattern>
<http-method>PUT</http-method>
<http-method>GET</http-method>
</web-resource-collection>
<web-resource-collection>
<web-resource-name>indexservlet</web-resource-name>
<url-pattern>/indexservlet</url-pattern>
<http-method>PUT</http-method>
<http-method>GET</http-method>
</web-resource-collection>
<web-resource-collection>
<web-resource-name>index</web-resource-name>
<url-pattern>/index.jsp</url-pattern>
<http-method>PUT</http-method>
<http-method>GET</http-method>
</web-resource-collection>
<auth-constraint>
<role-name>mgr</role-name>
</auth-constraint>
<user-data-constraint>
<transport-guarantee>NONE</transport-guarantee>
</user-data-constraint>
</security-constraint>
这里我配置了对admin目录、index.jsp、indexservlet的保护,只允许mgr角色可以访问,注意不要对error.jsp也保护了,否则会导致登陆失败时达不到错误页面。

5、配置角色:
mgr是这样配置的:
在web.xml里加入:
<security-role>
<role-name>mgr</role-name>
</security-role>

在weblogic.xml里加入角色映射:
<security-role-assignment>
<role-name>mgr</role-name>
<principal-name>admin</principal-name>
</security-role-assignment>
该段表示将weblogic服务器里配置的admin用户映射为这里的mgr角色。

6、配置验证方式:
在web.xml里加入
<login-config>
<auth-method>FORM</auth-method>
<form-login-config>
<form-login-page>/login.html</form-login-page>
<form-error-page>/error.jsp</form-error-page>
</form-login-config>
</login-config>

现在,打包发布你的程序,输入任何保护的资源,在未验证的情况下就会跳转到登陆页面了,从而完成对资源的保护。
本文出自 “java探讨” 博客,转载请与作者联系!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: