@wendal 这是我自己写的验证方法:
@At
@Filters()
public Object authcUser(HttpServletRequest resq, HttpSession session, HttpServletResponse resp) {
String status = resq.getParameter("status");//获取是否选中的状态
String code = resq.getParameter("username");
String password = resq.getParameter("password");
User user = userOpService.authcUser(code, password);
if (null != user) {
List<String> arr = new ArrayList<>();
//获取所包含的站点id集合
List<String> ids = deptOpService.getStationIds(arr, user.getDeptid());
//获取所包含的站点的sno集合
List<String> snos = deptOpService.getStationSnos(arr, user.getDeptid());
//获取所有站点编码和站点名称对应的map集合
Map<String,String> snoMap = deptOpService.getSnoMap();
if (status.equals("1")) {
//创建两个Cookie对象
Cookie nameCookie = new Cookie("username", code);
//设置Cookie的有效期为3天
nameCookie.setMaxAge(60 * 60 * 24 * 3);
String key = "bjbeyondb";
Cookie pwdCookie = new Cookie("password", CyptoUtils.encode(key, password));
pwdCookie.setMaxAge(60 * 60 * 24 * 3);
resp.addCookie(nameCookie);
resp.addCookie(pwdCookie);
}
Cookie statusCookie = new Cookie("status", status);
statusCookie.setMaxAge(60 * 60 * 24 * 3);
resp.addCookie(statusCookie);
session.setAttribute("me", user);
session.setAttribute("ids", ids);//存储到session当中
session.setAttribute("snos", snos);//存储到session当中
session.setAttribute("snoMap", snoMap);
resq.setAttribute("name", user.getUsername());
resq.setAttribute("code", user.getUsercode());
return new NutMap().setv("data", "success");
} else {
return new NutMap().setv("data", "fail");
}
}
然后是跳转到login.jsp执行的操作,:
//点击登录按钮
$("#loginBtn").on("click",function(){
var username = $('input[name="username"]').val();
var password = $('input[name="password"]').val();
var status = $('input[name="rememberCk"]').val();
if(username==''||password==''){
alert("用户名或密码为空,请重新输入!");
return;
}
//发送ajax请求判断是否用户已经登录并且跳转页面
$.ajax({
url: "<%=basePath%>user/authcUser",
type:"POST",
data:{
"username":username,
"password":password,
"status":status
},
dataType:"json",
success: function(data){
if(data.data=="success"){
window.location.href = "<%=basePath%>user/toFrontIndex";
}else{
alert("该用户不存在,请重新登录!");
$('input[name="username"]').val("");
$('input[name="password"]').val("");
return;
}
}
});
});