【学习笔记】Filter过滤器实现登录验证

Filter过滤器实现登录验证

用户登录了才能访问某个页面,不登录无法访问该页面

登录Servlet:

package servlet;
​
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import util.Constant;
​
import java.io.IOException;
​
public class LoginServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("username");
        if (username.equals("admin")){
            req.getSession().setAttribute(Constant.USER_SESSION,"admin");
            resp.sendRedirect("/sys/success.jsp");
        }else {
            resp.sendRedirect("/error/error.jsp");
        }
    }
​
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}

 

注销Servlet:

package servlet;
​
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import util.Constant;
​
import java.io.IOException;
​
public class Logout extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       if (req.getSession().getAttribute(Constant.USER_SESSION) != null){
           req.getSession().removeAttribute(Constant.USER_SESSION);
           resp.sendRedirect("/login.jsp");
       }
​
    }
​
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}

登录页:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>登录</title>
</head>
<body>
<form action="/servlet/login" method="post">
    <input type="text" name="username">
    <input type="submit" value="登录">
</form>
</body>
</html>

错误页面:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>登录失败</title>
</head>
<body>
<h1>登录失败</h1>
<h2>没有权限,或用户名错误</h2>
<a href="/login.jsp">回到登录页</a>
</body>
</html>

登录成功页:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>首页</title>
</head>
<body>
<h1>登录成功</h1>
<a href="/servlet/logout">注销</a>
</body>
</html>

 

过滤器Filter:

package Filter;
​
import jakarta.servlet.*;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import util.Constant;
​
import java.io.IOException;
​
public class LoginFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        Filter.super.init(filterConfig);
    }
​
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
​
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;
​
        if (request.getSession().getAttribute(Constant.USER_SESSION) == null){
            response.sendRedirect("/error/error.jsp");
        }
​
        filterChain.doFilter(servletRequest,servletResponse);
    }
​
    @Override
    public void destroy() {
        Filter.super.destroy();
    }
}
posted @ 2022-10-22 17:21  GrowthRoad  阅读(20)  评论(0编辑  收藏  举报