if (Common.isEmpty(username) || Common.isEmpty(password)) {
request.setAttribute("error","用户名或密码不能为空!");
return "/background/framework/login";
}
// 验证用户账号与密码是否正确
User users = this.userDao.querySingleUser(username);
if (users == null || !users.getUserPassword().equals(password)) {
request.setAttribute("error", "用户或密码不正确!");
return "/background/framework/login";
}
Authentication authentication = myAuthenticationManager
.authenticate(new UsernamePasswordAuthenticationToken(username,password));
SecurityContext securityContext = SecurityContextHolder.getContext();
securityContext.setAuthentication(authentication);
HttpSession session = request.getSession(true);
session.setAttribute("SPRING_SECURITY_CONTEXT", securityContext);
// 当验证都通过后,把用户信息放在session里
request.getSession().setAttribute("userSession", users);
// 记录登录信息
UserLoginList userLoginList = new UserLoginList();
userLoginList.setUserId(users.getUserId());
userLoginList.setLoginIp(Common.toIpAddr(request));
userLoginListService.add(userLoginList);
} catch (AuthenticationException ae) {
request.setAttribute("error", "登录异常,请联系管理员!");
return "/background/framework/login";