|
|
|
|
@ -12,79 +12,100 @@ import javax.servlet.http.HttpSession;
|
|
|
|
|
import com.hua.entity.User;
|
|
|
|
|
import com.hua.impl.UserDAOImpl;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 定义一个WebFilter,用于拦截特定的请求并进行相关处理
|
|
|
|
|
// 配置该过滤器作用于不同的DispatcherType(请求分发类型)以及特定的URL模式
|
|
|
|
|
@WebFilter(dispatcherTypes = {
|
|
|
|
|
DispatcherType.REQUEST,
|
|
|
|
|
DispatcherType.FORWARD,
|
|
|
|
|
DispatcherType.INCLUDE,
|
|
|
|
|
DispatcherType.ERROR
|
|
|
|
|
}
|
|
|
|
|
, urlPatterns = { "/loginChangeServlet" })
|
|
|
|
|
DispatcherType.REQUEST,
|
|
|
|
|
DispatcherType.FORWARD,
|
|
|
|
|
DispatcherType.INCLUDE,
|
|
|
|
|
DispatcherType.ERROR
|
|
|
|
|
}
|
|
|
|
|
, urlPatterns = { "/loginChangeServlet" })
|
|
|
|
|
public class LoginFilter extends HttpFilter {
|
|
|
|
|
|
|
|
|
|
// 重写父类的doFilter方法,该方法是过滤器的核心处理逻辑所在
|
|
|
|
|
@Override
|
|
|
|
|
public void doFilter(HttpServletRequest request,
|
|
|
|
|
HttpServletResponse response, FilterChain filterChain)
|
|
|
|
|
HttpServletResponse response, FilterChain filterChain)
|
|
|
|
|
throws IOException, ServletException {
|
|
|
|
|
// 获取当前请求对应的HttpSession对象,用于在会话中存储和获取数据
|
|
|
|
|
HttpSession session = request.getSession();
|
|
|
|
|
|
|
|
|
|
//获取请求参数username和password
|
|
|
|
|
|
|
|
|
|
// 从请求参数中获取名为"username"的参数值,即用户名
|
|
|
|
|
String username = request.getParameter("username");
|
|
|
|
|
String password = request.getParameter("password");
|
|
|
|
|
|
|
|
|
|
//防止重复提交
|
|
|
|
|
// 从请求参数中获取名为"password"的参数值,即用户密码
|
|
|
|
|
String password = request.getParameter("password");
|
|
|
|
|
|
|
|
|
|
// 调用repeatSubmit方法检查是否是重复提交,如果是重复提交则直接返回,不再执行后续逻辑
|
|
|
|
|
if(!repeatSubmit(request, response)) return;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 判断用户名和密码是否都为空,如果为空则执行以下逻辑
|
|
|
|
|
if(username == "" && password == ""){
|
|
|
|
|
// 创建一个提示信息,表示账号和密码不能为空
|
|
|
|
|
String message = "账号和密码不能为空";
|
|
|
|
|
// 将提示信息存储到HttpSession中,以便在登录页面可以获取并显示该信息
|
|
|
|
|
session.setAttribute("message", message);
|
|
|
|
|
//重定向回到登录页面
|
|
|
|
|
response.sendRedirect("login/login.jsp");
|
|
|
|
|
// 重定向到登录页面(login/login.jsp),让用户重新输入账号和密码
|
|
|
|
|
response.sendRedirect("login/login.jsp");
|
|
|
|
|
}else{
|
|
|
|
|
// 调用check方法检查用户名和密码是否匹配,如果不匹配则执行以下逻辑
|
|
|
|
|
if(!check(username,password)){
|
|
|
|
|
//密码匹配失败后返回登录页面并带回一条信息:输入密码错误,请重新输入
|
|
|
|
|
// 创建一个提示信息,表示账号或密码不正确,请重新输入
|
|
|
|
|
String message = "账号或密码不正确,请重新输入";
|
|
|
|
|
// 将提示信息存储到HttpSession中,以便在登录页面可以获取并显示该信息
|
|
|
|
|
session.setAttribute("message", message);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 将用户名也存储到HttpSession中,可能用于在登录页面回显用户名等用途
|
|
|
|
|
session.setAttribute("username", username);
|
|
|
|
|
//重定向回到登录页面
|
|
|
|
|
// 重定向到登录页面(login/login.jsp),让用户重新输入正确的密码
|
|
|
|
|
response.sendRedirect("login/login.jsp");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
// 如果用户名和密码匹配,则放行请求,让请求继续向下传递到后续的过滤器或者目标Servlet等资源
|
|
|
|
|
filterChain.doFilter(request, response);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private boolean repeatSubmit(HttpServletRequest request, HttpServletResponse response)
|
|
|
|
|
// 该方法用于检查当前请求是否是重复提交
|
|
|
|
|
private boolean repeatSubmit(HttpServletRequest request, HttpServletResponse response)
|
|
|
|
|
throws IOException, ServletException{
|
|
|
|
|
// 获取当前请求对应的HttpSession对象
|
|
|
|
|
HttpSession session = request.getSession();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 从HttpSession中获取名为"token"的属性值,该值用于标识唯一性,防止重复提交
|
|
|
|
|
Object token = session.getAttribute("token");
|
|
|
|
|
// 从请求参数中获取名为"token"的参数值
|
|
|
|
|
String tokenVlaue = request.getParameter("token");
|
|
|
|
|
|
|
|
|
|
if(token != null && token.equals(tokenVlaue)){
|
|
|
|
|
|
|
|
|
|
// 如果从会话中获取的token和请求参数中的token值相等,说明不是重复提交
|
|
|
|
|
if(token!= null && token.equals(tokenVlaue)){
|
|
|
|
|
// 移除会话中的token,避免下次重复使用
|
|
|
|
|
session.removeAttribute("token");
|
|
|
|
|
return true;
|
|
|
|
|
}else{
|
|
|
|
|
// 如果是重复提交,则重定向到重复提交提示页面(/repeatsubmit.jsp)
|
|
|
|
|
response.sendRedirect(request.getContextPath() + "/repeatsubmit.jsp");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 该方法用于检查用户名和密码是否在数据库中匹配
|
|
|
|
|
private boolean check(String username, String password) {
|
|
|
|
|
//进入数据库查询
|
|
|
|
|
// 创建UserDAOImpl对象,该对象应该是用于操作数据库中用户数据相关的实现类
|
|
|
|
|
UserDAOImpl userDAOImpl = new UserDAOImpl();
|
|
|
|
|
// 通过UserDAOImpl的方法根据用户名获取对应的用户对象(从数据库查询)
|
|
|
|
|
User user = userDAOImpl.get(username);
|
|
|
|
|
if(user != null){
|
|
|
|
|
// 如果查询到的用户对象不为空,说明用户名存在,继续检查密码是否匹配
|
|
|
|
|
if(user!= null){
|
|
|
|
|
// 比较从数据库获取的用户密码和传入的密码是否相等,如果相等则返回true,表示用户名和密码匹配成功
|
|
|
|
|
if(user.getPassword().equals(password)){
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// 如果用户名不存在或者密码不匹配,则返回false
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|