<%@page import="javax.jws.soap.SOAPBinding.Style"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<% String path = request.getContextPath(); %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<link rel="stylesheet" href="<%=path %>/css/index2.css" type="text/css"></link>
<script type="text/javascript" src="<%=path %>/js/jquery-1.8.3.js"></script>
<script type="text/javascript">
function checkLogin(){
$("span").html("");
var username = $("[name=username]").val();
var passeord = $("[name=password]").val();
if (username == "") {
$("#msg_username").text("用户名不能为空");
}
if (username == "") {
$("#msg_password").text("密码不能为空");
}
if ($("span").text() == "") {
$.ajax({
url:"<%=path%>/checkLogin.action",
type:"POST",
data:$("form").serialize(),
dataType:"json",
success:function(obj){
if (obj.msg == "success") {
alert("登陆成功");
//location.href = "list.action";
} else {
$("span1").text(obj.msg);
}
}
});
}
}
</script>
<style type="text/css">
span1{color: red;}
</style>
</head>
<body>
<form action="">
<table>
<tr>
<td colspan="2"><span1> </span1></td>
</tr>
<tr>
<td>用户名</td>
<td>
<input type="text" name="username"/>
<span id="msg_username"></span>
</td>
</tr>
<tr>
<td>密码</td>
<td>
<input type="text" name="password"/>
<span id="msg_password"></span>
</td>
</tr>
<tr>
<td colspan="2"><input type="button" value="登录" οnclick="checkLogin()"/> </td>
</tr>
</table>
</form>
</body>
</html>
package com.bw.service;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.bw.dao.UserDao;
import com.bw.dto.User;
/**
* :UserServiceImpl.java
**/
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserDao dao;
public Map<String, Object> checkLogin(User user) {
Map<String, Object> map = new HashMap<String, Object>();
//判断用户名
User u = dao.checkUsername(user);
if (u != null) {
//判断用户是否锁定
if (u.getStatus() == 1) {
//用户被锁定,判断锁定时间是否超过5个小时
if (compareTime(u)) {
//超过5个小时,解锁用户信息
dao.unLockUser(user);
//解锁成功,获取解锁之后的用户信息
u = dao.checkUsername(user);
//判断密码
map = checkPassword(user, u);
}else {//未超过5个小时,相应前台
map.put("msg", "你的用户已被锁定");
}
}else {//用户未被锁定,判断密码
map = checkPassword(user, u);
}
}else {
map.put("msg", "用户名不存在");//用户名不存在
}
return map;
}
//验证密码
public Map<String, Object> checkPassword(User user, User u) {
Map<String, Object> map = new HashMap<String, Object>();
if (user.getPassword().equals(u.getPassword())) {
//登录成功,初始化是数据
map.put("msg", "success");
dao.reLoadUser(u);
}else {//判断密码错误次数是否是0或1
if (u.getErrCount() <= 1) {
map.put("msg", "密码错误,你还有"+(3-1-u.getErrCount())+"次机会");
dao.increaceErrCount(u);
}if (u.getErrCount() == 2) {
//锁定用户
dao.lockUser(u);
map.put("msg", "密码错误3次,你的用户已锁定");
}
}
return map;
}
//判断时间是否超过5个小时
public boolean compareTime(User u) {
try {
String loginTime = u.getLoginTime();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
Date date = sdf.parse(loginTime);
Date now = new Date();
if (now.getTime() - date.getTime() > 5*60*60*1000) {
return true;
}
} catch (ParseException e) {
e.printStackTrace();
}
return false;
}
}