sql表
username password字段
User类
有 id username password等字段
Service有一函数
1 @Override 2 3 public User findUser(String username) 4 { 5 return userDao.findUser(username); 6 }
实现
1 @Override 2 public User findUser(String username) { 3 try { 4 return qr.query("select * from user where username=?", new BeanHandler<User>(User.class),username); 5 } catch (SQLException e) { 6 throw new DaoException(e); 7 } 8 }
Utils
WEBUtils.java
1 package cn.itcast.util; 2 3 import java.lang.reflect.InvocationTargetException; 4 import java.security.MessageDigest; 5 import java.security.NoSuchAlgorithmException; 6 7 import javax.servlet.http.Cookie; 8 import javax.servlet.http.HttpServletRequest; 9 import javax.servlet.http.HttpServletResponse; 10 11 import org.apache.commons.beanutils.BeanUtils; 12 13 import sun.misc.BASE64Encoder; 14 15 16 public class WebUtils { 17 18 public static <T> T fillBean(HttpServletRequest request, 19 Class<T> clazz) { 20 try { 21 T t=clazz.newInstance(); 22 BeanUtils.populate(t,request.getParameterMap()); 23 return t; 24 } catch(Exception e) { 25 throw new RuntimeException(e); 26 } 27 } 28 29 public static void addAutoLoginFunction(HttpServletRequest request, 30 HttpServletResponse response) { 31 32 33 String username=request.getParameter("username"); 34 String password=request.getParameter("password"); 35 //把帐号BASE64加密 _ 密码双次md5加密, 所以比较的时候 数据库中密码取出md5加密再和这个比较 36 37 String encodeUsername=new BASE64Encoder().encode(username.getBytes()); 38 String encodePassword=Md5Util.md5(password); 39 System.out.println("存入数据库 帐号:"+username); 40 System.out.println("存入数据库 密码:"+password); 41 System.out.println("存入Cookie 帐号:"+encodeUsername); 42 System.out.println("存入Cookie密码:"+encodePassword); 43 Cookie c=new Cookie("loginInfo",encodeUsername+"_"+encodePassword); 44 c.setMaxAge(10000); 45 c.setPath(request.getContextPath()); 46 response.addCookie(c); 47 } 48 //删除Cookie 49 public static void removeAutoLoginCookie(HttpServletRequest request, 50 HttpServletResponse response) { 51 Cookie cs[]=request.getCookies(); 52 if(cs!=null) 53 { 54 for(Cookie c:cs) 55 { 56 if(c.getName().equals("loginInfo")) 57 { 58 Cookie cookie=new Cookie("loginInfo",null); 59 cookie.setMaxAge(0); 60 cookie.setPath(request.getContextPath()); 61 response.addCookie(cookie); 62 // c.setMaxAge(0); 63 // c.setPath(request.getContextPath()); 64 System.out.println("删除Cookie"); 65 return; 66 } 67 } 68 } 69 } 70 71 72 73 }
MD5Util.java
1 package cn.itcast.util; 2 3 import java.security.MessageDigest; 4 import java.security.NoSuchAlgorithmException; 5 6 import sun.misc.BASE64Encoder; 7 8 public class Md5Util { 9 public static String md5(String message){ 10 try{ 11 MessageDigest md = MessageDigest.getInstance("md5"); 12 byte b[] = md.digest(message.getBytes()); 13 return new BASE64Encoder().encode(b); 14 }catch(Exception e){ 15 throw new RuntimeException(e); 16 } 17 } 18 }
Servlet中 由doGet()引出以下两个方法 operation=login operation=logout
private void logout(HttpServletRequest request, HttpServletResponse response) throws IOException { //移除Cookie 去除自动登录功能 WebUtils.removeAutoLoginCookie(request,response); request.getSession().invalidate(); response.sendRedirect(request.getContextPath()); } private void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String username=request.getParameter("username"); String password=request.getParameter("password"); BusinessService serviceDao=new BusinessServiceImpl(); password=Md5Util.md5(password); User user=serviceDao.login(username,password); if(null==user) { request.setAttribute("message", "用户名或者密码错误"); request.getRequestDispatcher("/client/message.jsp").forward(request, response); } else { request.getSession().setAttribute("user", user); String autoLogin=request.getParameter("autologin"); if(null!=autoLogin) { //添加自动登录功能 WebUtils.addAutoLoginFunction(request,response); } response.sendRedirect(request.getContextPath()); } }
filter过滤器中配置
1 package cn.itcast.web.filter; 2 3 import java.io.IOException; 4 5 import javax.servlet.Filter; 6 import javax.servlet.FilterChain; 7 import javax.servlet.FilterConfig; 8 import javax.servlet.ServletException; 9 import javax.servlet.ServletRequest; 10 import javax.servlet.ServletResponse; 11 import javax.servlet.http.Cookie; 12 import javax.servlet.http.HttpServletRequest; 13 import javax.servlet.http.HttpServletResponse; 14 import javax.servlet.http.HttpSession; 15 16 import sun.misc.BASE64Decoder; 17 18 import cn.itcast.domain.User; 19 import cn.itcast.service.BusinessService; 20 import cn.itcast.service.impl.BusinessServiceImpl; 21 import cn.itcast.util.Md5Util; 22 23 public class AutoLoginFilter implements Filter { 24 25 @Override 26 public void init(FilterConfig filterConfig) throws ServletException { 27 28 } 29 30 @Override 31 public void doFilter(ServletRequest req, ServletResponse resp, 32 FilterChain chain) throws IOException, ServletException { 33 34 HttpServletRequest request = (HttpServletRequest)req; 35 HttpServletResponse response = (HttpServletResponse)resp; 36 BusinessService bService=new BusinessServiceImpl(); 37 HttpSession session = request.getSession(); 38 User u = (User)session.getAttribute("user"); 39 if(null==u)只有没有登录时才自动登录,已经登录了就不需要了 40 { 41 System.out.println("自动登录开始执行"); 42 //1、获取名称为loginInfo的cookie 43 Cookie loginInfoCookie=null; 44 Cookie cs[]=request.getCookies(); 45 if(null!=cs) 46 { 47 for(Cookie c:cs) 48 { 49 if("loginInfo".equals(c.getName())) 50 { 51 loginInfoCookie=c; 52 break; 53 } 54 } 55 } 56 //2、有:取出cookie的值:用户名_加密的密码 57 if(null!=loginInfoCookie) 58 { 59 String usernamePassword=loginInfoCookie.getValue(); 60 System.out.println("帐号密码整串:"+usernamePassword); 61 //3、拆出用户名和密码 62 String username=usernamePassword.split("\\_")[0]; 63 String password=usernamePassword.split("\\_")[1]; 64 //根据登录的时候 username password放入Cookie的原理 65 //username BASE64解密 66 username=new String(new BASE64Decoder().decodeBuffer(username)); 67 System.out.println("Cookie的用户:"+username); 68 System.out.println("Cookie的密码:"+password); 69 User user=bService.findUser(username); 70 if(null!=user) 71 { 72 System.out.println("Cookie的用户:"+user.getUsername()); 73 System.out.println("Cookie的密码:"+Md5Util.md5(user.getPassword())); 74 //4、再次验证用户名和密码是否正确(根据用户名查出密码,加密后再与cookie中的那个密码进行比对) 75 //将数据库查出的密码 md5加密和cookie中的密码相比 76 if(Md5Util.md5(user.getPassword()).equals(password)) 77 { 78 //5、正确:得到用户对象,放到HttpSession中(自动登录) 79 session.setAttribute("user", user); 80 //自动登录搞定 81 System.out.println("自动登录完成"); 82 } 83 } 84 } 85 } 86 //放行 87 chain.doFilter(request, response); 88 } 89 90 @Override 91 public void destroy() { 92 93 } 94 95 }
web.xml中可以配置对指定的页面进行 自动登录的过滤器, 因为放到session中,所以不需要每个页面都过滤
url-pattern 配置 过滤地址