Filter 自动登录

已有 58 次阅读2017-6-12 21:03

LoginFilter :

 

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.annotation.WebFilter;

import javax.servlet.http.Cookie;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

 

/**

 * Servlet Filter implementation class LoginFilter

 */

//@WebFilter("/*")

public class LoginFilter implements Filter {

 

    /**

     * Default constructor.

     */

    public LoginFilter() {

        // TODO Auto-generated constructor stub

    }

 

    /**

     * @see Filter#destroy()

     */

    public void destroy() {

       // TODO Auto-generated method stub

    }

 

    /**

     * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)

     */

    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {

       // TODO Auto-generated method stub

       // place your code here

       HttpServletRequest req=(HttpServletRequest) request;

       HttpServletResponse rep=(HttpServletResponse) response;

       //获取完整路径  cookieTest/loginTest.jsp;

       String uri=req.getRequestURI();

       //获取项目名 cookieTest

       String path=req.getContextPath();

       //获得页面名称

       String varpath=uri.substring(path.length());

       System.out.println(varpath);

      

       if("/login.jsp".equals(varpath)){

           chain.doFilter(request, response);

       }else{

           System.out.println("其他");

           //1.先判断是否已经登录过

           String session = (String) req.getSession().getAttribute("username");

           if(session!=null){

              System.out.println("session");

              chain.doFilter(request, response);

           }

           //当服务器端session没有的时候,其他页面访问,还可以判断是否cookie有携带用户名和密码

           else{

              //得到cookie

              Cookie[] cookie = req.getCookies();

              String name=null;

              String pwd=null;

             

              for(Cookie s:cookie){

                  if("name".equals(s.getName())){

                     name=s.getValue();

                  }

 

                  if("pwd".equals(s.getName())){

                     pwd=s.getValue();

                  }

              }

              boolean flag=false;

             

              if(name!=null && pwd!=null){

                  //登录验证

                  System.out.println("cookie");

                     if("admin".equals(name) && "123".equals(pwd)){

                         flag=true;

                     }

              }

              if(flag){

                  chain.doFilter(request, response);

              }

              else{

              req.setAttribute("info","还未登录");

              req.getRequestDispatcher("loginTest.jsp").forward(req, rep);

              }

           }

      

       }

       // pass the request along the filter chain

      

    }

   

 

    /**

     * @see Filter#init(FilterConfig)

     */

    public void init(FilterConfig fConfig) throws ServletException {

       // TODO Auto-generated method stub

    }

 

}

 

 

 

 

LoginServletTest:

 

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

       String username=request.getParameter("username");

       String pwd= request.getParameter("pwd");

       if("admin".equals(username)&& "123".equals(pwd)){

           //第一次登录成功,添加session (访问其他页面,判断是否登录过)

           request.getSession().setAttribute("username",username);

           //第一次登录成功,添加cookie (访问其他页面,判断是否登录过)

           Cookie cookie1 = new Cookie("name",username);

           Cookie cookie2 = new Cookie("pwd", pwd);

           cookie1.setMaxAge(30*60);

           cookie2.setMaxAge(30*60);

           response.addCookie(cookie1);

           response.addCookie(cookie2);

           request.getRequestDispatcher("result.jsp").forward(request, response);

       }

       else{

           response.sendRedirect("loginTest.jsp");

       }

    }

 

 

 

LoginTest.jsp:

 

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8">

<title>Insert title here</title>

</head>

<body>

${info }

 <form action="LoginServletTest" method="post">

    <input type="text" name="username" >

    <input type="text" name="pwd">

    <input type="submit" value="测试">

 

</form>

</body>

</html>

 

 

 

Result.jsp:

 

 

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8">

<title>Insert title here</title>

</head>

<body>

登录成功页面${username }

</body>

</html>


路过

鸡蛋

鲜花

握手

雷人

全部作者的其他最新日志

评论 (0 个评论)

facelist

doodle 涂鸦板

您需要登录后才可以评论 登录 | 立即注册

Copyright;  ©2015-2017  知了堂学习社区  Powered by  知了堂Edu!     ( 蜀ICP备16011312号-1 )