码迷,mamicode.com
首页 > 其他好文 > 详细

Filter--对敏感目录进行认证--笔记

时间:2018-04-18 23:45:21      阅读:281      评论:0      收藏:0      [点我收藏+]

标签:Filter--对敏感目录进行认证--笔

admin/admin.html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>admin.html</title>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  </head>
  <body>
    <font size="44">欢迎管理员光临</font>
  </body>
</html>

Filter.java


import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

//对敏感目录进行认证
public class FilterDemo6 implements Filter {
    public void init(FilterConfig filterConfig) throws ServletException {
    }
    public void doFilter(ServletRequest req, ServletResponse res,FilterChain chain) throws IOException, ServletException {

        HttpServletRequest request = (HttpServletRequest) req;
        HttpServletResponse response = (HttpServletResponse) res;

        //取得用户请求参数
        String username = request.getParameter("username");
        String password = request.getParameter("password");

        //判段
        if(username!=null && password!=null){
            if(username.equals("admin") && password.equals("123")){
                //允许进入敏感资源
                chain.doFilter(request,response);

            }else{
                //转发到message.jsp页面
                request.setAttribute("message","用户名或密码不正确");
                request.getRequestDispatcher("/message.jsp").forward(request,response);
            }
        }else{
            //转发到message.jsp页面
            request.setAttribute("message","必须填入用户名和密码");
            request.getRequestDispatcher("/message.jsp").forward(request,response);
        }
    }
    public void destroy() {
    }
}

web.xml

<filter>
    <filter-name>FilterDemo6</filter-name>
    <filter-class>cn.web.filter.FilterDemo6</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>FilterDemo6</filter-name>
    <url-pattern>/admin/*</url-pattern>
  </filter-mapping>

message.jsp

<%@ page language="java" pageEncoding="UTF-8"%>
<%@ page isELIgnored="false" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <body>
    ${message}  
  </body>
</html>

Filter--对敏感目录进行认证--笔记

标签:Filter--对敏感目录进行认证--笔

原文地址:http://blog.51cto.com/357712148/2105123

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!