user

 public class User implements Serializable{
private String username;
private String nick;
private String password;
public User(){} public User(String username, String nick, String password) {
super();
this.username = username;
this.nick = nick;
this.password = password;
} public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getNick() {
return nick;
}
public void setNick(String nick) {
this.nick = nick;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
} }

util

md5

 import java.security.MessageDigest;

 import sun.misc.BASE64Encoder;

 public class MD5Util {
public static String md5(String message){
try{
MessageDigest md = MessageDigest.getInstance("md5");
byte b[] = md.digest(message.getBytes());
return new BASE64Encoder().encode(b);
}catch(Exception e){
throw new RuntimeException(e);
}
}
}

WebUtils

     //添加Cookie
1 public static void addAutoLoginFunction(HttpServletRequest request,
HttpServletResponse response, String username, String password) {
//把帐号BASE64加密 _ 密码双次md5加密, 所以比较的时候 数据库中密码取出md5加密再和这个比较
String encodeUsername=new BASE64Encoder().encode(username.getBytes());
String encodePassword=Md5Util.md5(password);
System.out.println("存入数据库 帐号:"+username);
System.out.println("存入数据库 密码:"+password);
System.out.println("存入Cookie 帐号:"+encodeUsername);
System.out.println("存入Cookie密码:"+encodePassword);
Cookie c=new Cookie("loginInfo",encodeUsername+"_"+encodePassword);
c.setMaxAge(10000);
c.setPath(request.getContextPath());
response.addCookie(c);
}
//删除Cookie
public static void removeAutoLoginCookie(HttpServletRequest request,
HttpServletResponse response) {
Cookie cs[]=request.getCookies();
if(cs!=null)
{
for(Cookie c:cs)
{
if(c.getName().equals("loginInfo"))
{
Cookie cookie=new Cookie("loginInfo",null);
cookie.setMaxAge(0);
cookie.setPath(request.getContextPath());
response.addCookie(cookie);
// c.setMaxAge(0);
// c.setPath(request.getContextPath());
System.out.println("删除Cookie");
return;
}
}
}
}

servlet

 import sun.misc.BASE64Encoder;
//完成用户登录
public class LoginServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 1、取到用户名和密码
String username = request.getParameter("username");
String password = request.getParameter("password");
// 2、验证是否正确
User user = UserDB.findUser(username, password);
if(user!=null){
// 3、正确,把用户放到HttpSession中
request.getSession().setAttribute("user", user);
// 4、判断用户是否需要自动登录
String autologin = request.getParameter("autologin");
if(autologin!=null){           
// 5、是:把用户名和密码保存到一个指定的cookie中
Cookie c = new Cookie("loginInfo",new BASE64Encoder().encode(username.getBytes())+"_"+MD5Util.md5(password));//存在客户端的cookie中,如果密码是名为,很危险
c.setMaxAge(Integer.MAX_VALUE);
c.setPath(request.getContextPath());
response.addCookie(c);
}
}
// 6、重定向到主页
response.sendRedirect(request.getContextPath()+"/autologin/index.jsp");
} public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { doGet(request, response);
} }

注销

     private void logout(HttpServletRequest request, HttpServletResponse response) throws IOException {
WebUtils.removeAutoLoginCookie(request,response);
request.getSession().invalidate();
response.sendRedirect(request.getContextPath());
}

过滤器

 public void doFilter(ServletRequest req, ServletResponse resp,
FilterChain chain) throws IOException, ServletException { HttpServletRequest request = (HttpServletRequest)req;
HttpServletResponse response = (HttpServletResponse)resp; HttpSession session = request.getSession();
User u = (User)session.getAttribute("user");
if(u==null){//只有没有登录时才自动登录,已经登录了就不需要了
// System.out.println("自动登录执行了");
// 1、获取名称为loginInfo的cookie
Cookie loginInfoCookie = null;
Cookie cs[] = request.getCookies();
for(int i=0;cs!=null&&i<cs.length;i++){
if("loginInfo".equals(cs[i].getName())){
loginInfoCookie = cs[i];
break;
}
}
if(loginInfoCookie!=null){
// 2、有:取出cookie的值:用户名_加密的密码
String usernamePassword = loginInfoCookie.getValue();// zql_slkdjflksjkfslkfls
// 3、拆出用户名和密码
String username = usernamePassword.split("\\_")[0];//用户名
username = new String(new BASE64Decoder().decodeBuffer(username));
String cookiePassword = usernamePassword.split("\\_")[1];//密码
// 4、再次验证用户名和密码是否正确(根据用户名查出密码,加密后再与cookie中的那个密码进行比对)
User user = UserDB.findUser(username);
if(user!=null){
//根据用户名查出密码,加密后再与cookie中的那个密码进行比对
if(cookiePassword.equals(MD5Util.md5(user.getPassword()))){
// 5、正确:得到用户对象,放到HttpSession中(自动登录)
session.setAttribute("user", user);
}
}
}
}
//放行
chain.doFilter(request, response);
}

cookie技术自动登录的更多相关文章

  1. cookie实现自动登录

    有很多Web程序中第一次登录后,在一定时间内(如2个小时)再次访问同一个Web程序时就无需再次登录,而是直接进入程序的主界面(仅限于本机).实现这个功能关键就是服务端要识别客户的身份.而用Cookie ...

  2. C#检测并安装https站点的数字证书,CefSharp和HttpWebRequest通过会话Cookie实现自动登录访问https站点

    HttpUtil工具类: using System; using System.Collections.Generic; using System.IO; using System.Linq; usi ...

  3. struts2与cookie实现自动登录和验证码验证

    主要介绍struts2与cookie结合实现自动登录 struts2与cookie结合时要注意采用.action 动作的方式实现cookie的读取 struts2的jar包 链接数据库文件 db.pr ...

  4. 使用cookie下次自动登录

    登录时勾选了自动登录处理: 1.加密账号和IP,保存在cookie中,cookie('auto', $value, $time) 2.解密cookie,取出账号和上次IP,判断上次IP==当前IP.账 ...

  5. 使用cookie实现自动登录

    一.从登录——>主页面,进行的过程是,输入 用户名和密码,以及验证码,点击“登录”跳转到Activity.jsp login1.action(跳转到登录页面) /** 跳转到login(有积分排 ...

  6. Spring mvc session cookie实现自动登录

    设计过程 1. user表存储用户名密码等信息,login表存放用户登陆状态的表 user表中存储username,pwd,等信息 login表存username,series(UUID),token ...

  7. 如何设计相对安全的cookie自动登录系统

    很多网站登录的时候,都会有一个"记住我"功能,用户可以在限定时间段内免登录, 比如豆瓣.人人.新浪微博等都有这种设计.这种技术其实就是基于 cookie的自动登录, 用户登录的时候 ...

  8. 自己Cookie写的自动登录功能 包含BASE64 和MD5的使用

    sql表 username  password字段 User类 有 id username password等字段 Service有一函数 @Override public User findUser ...

  9. 爬虫模拟cookie自动登录(人人网自动登录)

    什么是cookie? 在网站中,HTTP请求时无状态的,也就是说即使第一次和服务器连接后并且登录成功后,第二次请求服务器依然不能知道当前请求是谁,cookie的出现就是为了解决这个问题,第一次登陆后服 ...

随机推荐

  1. [HDU5354]Bipartite Graph(CDQ分治+并查集)

    经典动态二分图问题. 考虑solve(l,r)分治成l,mid和mid+1,r.先将区间[mid+1,r]中的点全部加入图中,若此时存在奇环则ans[l..mid]全部为0,否则递归到左边. 递归完左 ...

  2. bzoj 4017: 小Q的无敌异或

    4017: 小Q的无敌异或 Time Limit: 20 Sec  Memory Limit: 128 MBSubmit: 593  Solved: 197[Submit][Status][Discu ...

  3. [Lydsy1806月赛] 路径统计

    题面在这里! xjb想的做法竟然不小心把std艹爆了qwq,我也很无奈啊.... 那接下来就说一下我的神奇做法qwq 如果是经常读我博客的童鞋会发现其实我以前就想要做这个题啦,只不过当时读错题啦... ...

  4. mysqldumper

    介绍 MySQL自身的mysqldump工具支持单线程工作,依次一个个导出多个表,没有一个并行的机,这就使得它无法迅速的备份数据. mydumper作为一个实用工具,能够良好支持多线程工作,可以并行的 ...

  5. iOS 未读消息角标 仿QQ拖拽 简单灵活 支持xib(源码)

    一.效果 二.简单用法 超级简单,2行代码集成:xib可0代码集成,只需拖一个view关联LFBadge类即可 //一般view上加角标 _badge1 = [[LFBadge alloc] init ...

  6. HDU 4664 Triangulation(2013多校6 1010题,博弈)

    Triangulation Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tot ...

  7. 把网页转换成图片或者pdf--wkhtmltopdf

    一.下载并安装wkhtmltopdf https://wkhtmltopdf.org/downloads.html 按照需要自己下载安装就可以了: 二.使用步骤--启动和生成图片或pdf 1.下载wk ...

  8. Linux设备模型(总结)

    转:http://www.360doc.com/content/11/1219/16/1299815_173418267.shtml 看了一段时间的驱动编程,从LDD3的hello wrod到后来的字 ...

  9. 微服务:spring-cloud-archaius 起步

    原文:http://blog.csdn.net/qq_18675693/article/details/53337941 微服务:spring-cloud-archaius 起步 原创 2016年11 ...

  10. Kubernetes下的应用监控解决方案

    所谓应用监控,更多的是基于java jvm的监控,因为公司运行的中间件大部分都是基于tomcat,Springboot,SpringCloud,当然也必须支持WebLogic.在Kubernetes现 ...