• 软件测试技术
  • 软件测试博客
  • 软件测试视频
  • 开源软件测试技术
  • 软件测试论坛
  • 软件测试沙龙
  • 软件测试资料下载
  • 软件测试杂志
  • 软件测试人才招聘
    暂时没有公告

字号: 小 中 大 | 推荐给好友 上一篇 | 下一篇

防止用户非法登录又一法

发布: 2007-7-01 18:47 | 作者: admin | 来源: | 查看: 13次 | 进入软件测试论坛讨论

领测软件测试网

由于用户名及密码都是由a-z,A-Z,0-9这几字符组成, 我参考了一下正则表达式的算法,故有已下想法: 把用户输入的用户名及密码判断一下,看是否是这几个字符组成, 如果是,进行登录验证,否则提示有非法字符
代码如下:
//login.jsp
<%@ page contentType="text/html; charset=GBK" language="java" import="java.io.*" errorPage="error.jsp" %>
<%!
boolean regex(String str){
java.util.regex.Pattern p=null; //正则表达式
java.util.regex.Matcher m=null; //操作的字符串
boolean value=true;
try{
p = java.util.regex.Pattern.compile("[^0-9A-Za-z]");
m = p.matcher(str);
if(m.find()) {
value=false;
}
}catch(Exception e){}
return value;
}
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GBK">
<title>无标题文档</title>
</head>

<body>
<table width="100%" height="100%" border="0">
<tr>
<td align="center" valign="middle">
<% String action=request.getParameter("action");

if (action!=null&&action.equals("login")){
String username=request.getParameter("username");
String password=request.getParameter("password");
if(!regex(username)||!regex(password)){
out.println("<Script language=´javascript´>");
out.println("alert(´1.用户名只能是a-z,A-Z,0-9的字符.密码只能是a-z,A-Z,0-9的字符.不允许空格´)");
out.println("history.go(-1)");
out.println("</script>");
out.print("用户名只能是a-z,A-Z,0-9的字符");
return;//跳出程序
}
out.println(action);//登录验证
out.println("用户名是"+username+"<br>");
out.println("密码"+password);
}
else{
%>
<form name="form1" method="post" action="#">
<table width="70%" border="0">
<tr>
<td>username</td>
<td><input name="username" type="text" id="username">
</td>
</tr>
<tr>
<td>password</td>
<td><input name="password" type="text" id="password"></td>
</tr>
<tr>
<td> </td>
<td><input name="action" type="hidden" id="action" value="login">
用户名及密码只能是a-z,A-Z,0-9的字符</td>
</tr>
<tr>
<td><input type="submit" name="Submit" value=" O K "></td>
<td><input type="reset" name="Submit2" value="Cancle"></td>
</tr>
</table>
</form><% } %></td>
</tr>
</table>
</body>
</html>

延伸阅读

文章来源于领测软件测试网 https://www.ltesting.net/


关于领测软件测试网 | 领测软件测试网合作伙伴 | 广告服务 | 投稿指南 | 联系我们 | 网站地图 | 友情链接
版权所有(C) 2003-2010 TestAge(领测软件测试网)|领测国际科技(北京)有限公司|软件测试工程师培训网 All Rights Reserved
北京市海淀区中关村南大街9号北京理工科技大厦1402室 京ICP备2023014753号-2
技术支持和业务联系:[email protected] 电话:010-51297073

软件测试 | 领测国际 | ISTQB | ISTQB官网 | TMMi | TMMi认证 | 国际软件测试工程师认证 | 领测软件测试网