【JavaWeb】Servlet身份验证过滤器

16 篇文章 0 订阅

使用过滤器验证用户的示例

下面来看看如何使用过滤器验证用户的简单示例。
在这个示例中创建了以下几个主要的代码文件:

  • Authentication.html - 首页
  • MyAuthenticationFilter.java - 过滤器,用于处理用户登录信息和跳转。
  • AdminServlet.java - 管理员的Servlet
  • web.xml - 项目描述符和配置信息。

AdminServlet.java

public class AdminServlet extends HttpServlet {
    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
        // 设置服务器端内容类型
        response.setContentType("text/html;charset=utf-8");
        // 设置服务器端编码
        response.setCharacterEncoding("utf-8");
        // 设置客户端编码
        request.setCharacterEncoding("utf-8");
        // 获取服务器端输出对象
        PrintWriter out = response.getWriter();

        out.println("欢迎来到 Admin页面");
        out.close();
    }
}

MyAtuthenticationFilter.java

public class MyAuthenticationFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        System.out.println("Filter init");
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        // 设置服务器端内容类型
        servletResponse.setContentType("text/html;charset=utf-8");
        // 设置服务器端编码
        servletResponse.setCharacterEncoding("utf-8");
        // 设置客户端编码
        servletRequest.setCharacterEncoding("utf-8");
        // 获取服务器端输出对象
        PrintWriter out = servletResponse.getWriter();

        //获取密码
        String password = servletRequest.getParameter("password");
        if (password == null){
            password = "";
        }

        if (password.equals("admin")){
            filterChain.doFilter(servletRequest,servletResponse);
        }
        else {
            // include的请求转发
            out.println("密码错误");
            RequestDispatcher dispatcher = servletRequest.getRequestDispatcher("Authentication.html");
            dispatcher.include(servletRequest,servletResponse);
        }
    }

    @Override
    public void destroy() {
        System.out.println("Filter destroy");
    }
}

Authentication.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>使用过滤器验证用户</title>
</head>
<body>
    <div style="text-align: center">
        <form action="login" method="post">
            用户名:<input type="text" name="userName" value="krislin"><br/>
            密码:<input type="password" name="password"><br/>
            <input type="submit" value="登录">
        </form>
    </div>
</body>
</html>

web.xml

<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         id="WebApp_ID" version="3.1">
    <display-name>ServletRequest</display-name>
    <welcome-file-list>
        <welcome-file>Authentication.html</welcome-file>
        <welcome-file>Filter.html</welcome-file>
        <welcome-file>Context.html</welcome-file>
        <welcome-file>dbConfig.html</welcome-file>
        <welcome-file>redirect.html</welcome-file>
        <welcome-file>forward.html</welcome-file>
        <welcome-file>head.html</welcome-file>
    </welcome-file-list>
    <servlet>
        <servlet-name>AdminServlet</servlet-name>
        <servlet-class>AdminServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>AdminServlet</servlet-name>
        <url-pattern>/login</url-pattern>
    </servlet-mapping>
    <filter>
        <filter-name>MyAuthentication</filter-name>
        <filter-class>MyAuthenticationFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>MyAuthentication</filter-name>
        <url-pattern>/login</url-pattern>
    </filter-mapping>
<web-app>
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

攻城狮·建哥

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值