表单数据处理类

1。将信息中的密码进行MD5转码
2。非法字符


package com.qinya.tools;

import java.security.*;

public class Encrypt {


public static String encodeMD5(String str) {
if (null==str) {
return null;
}
StringBuilder sb = new StringBuilder();
try {
MessageDigest code = MessageDigest.getInstance("MD5");
code.update(str.getBytes());
byte[] bs = code.digest();
for (int i = 0; i < bs.length; i++) {
int v = bs[i] & 0xFF;
if (v < 16) {
sb.append(0);
}
sb.append(Integer.toHexString(v));
}
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}

//转化成十六进制的字符串
return sb.toString().toUpperCase();
}
/*
*是否存在非法字符“> >= <= ! + = | ()”
*/
public static boolean isValidInput(String str) {
return str.matches("[a-z0-9]+");
}


}


实现用户登入的checkAccount()
在一个Servlet实现
private void checkAccount(HttpServletRequest request,
HttpServletResponse response) throws UnsupportedEncodingException,
ServletException, IOException {
String information = "";
userInfoDao = new UserInfoDao();
session = request.getSession();
String rand = (String) session.getAttribute("rand");
String code = (String) request.getParameter("code");
String account = request.getParameter("account");
String password = request.getParameter("password");
if (com.wy.tools.Encrypt.isValidInput(account)
&& com.wy.tools.Encrypt.isValidInput(password)) {
account = new String(account.getBytes("ISO8859_1"), "gb2312");
password = com.wy.tools.Encrypt.encodeMD5(password);
} else {
information = "您输入的用户或密码存在非法字符串";
}
if (information.equals("")) {

if (rand.equals(code)) {
userInfo = userInfoDao.queryUserInfo(account);
if (null != userInfo) {
if (userInfo.getPassword().equals(password)) {
session.setAttribute("userInfo", userInfo);
} else {
information = "您输入的密码不正确";
}
} else {
information = "您输入的用户名不存在!";
}
} else {
information = "您输入的验证码不正确!";
}
}
System.out.println(information);
request.setAttribute("information", information);
request.getRequestDispatcher("index.jsp").forward(request, response);
}


验证账号是否正确
public ActionForward checkUserInfo(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
HttpSession session = request.getSession();
userInfoForm = (UserInfoForm) form;
dao = new UserInfoDao();
String account = userInfoForm.getAccount();
String password = com.wy.tools.Encrypt.encodeMD5(userInfoForm
.getPassword());
userInfoForm = dao.queryUserInfo(account);
if (null == userInfoForm) {
request.setAttribute("information", "用户名不存在!");
} else if (!userInfoForm.getPassword().equals(password)) {
request.setAttribute("information", "用户登录密码有误!");
} else {
request.setAttribute("information", "用户登录成功!");
session.setAttribute("userInfo", userInfoForm);
}
return mapping.findForward("opeationUserInfo");
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值