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. 初见Python<7>:Python操作mysql

    1.基本介绍: python标准数据库接口为python DB-API,它为开发人员提供了数据库应用编程接口,可以支持mysql.Oracle.MSSQL.Sybase等多种数据库,不同的数据库需要下 ...

  2. 【高斯消元】CDOJ1783 曜酱的线性代数课堂(一)

    高斯消元求逆矩阵板子. #include<cstdio> #include<cmath> #include<algorithm> #include<cstri ...

  3. 【二分】Codeforces Round #404 (Div. 2) C. Anton and Fairy Tale

    当m>=n时,显然答案是n: 若m<n,在第m天之后,每天粮仓减少的量会形成等差数列,只需要二分到底在第几天,粮仓第一次下降到0即可. 若直接解不等式,可能会有误差,需要在答案旁边扫一下. ...

  4. Codeforces Beta Round #2 B. The least round way dp

    B. The least round way 题目连接: http://www.codeforces.com/contest/2/problem/B Description There is a sq ...

  5. Linux PHP 编译参数详解(二)

    对于喜欢玩开源软件的童鞋么,都喜欢自己编译安装程序,本文说明下如何编译安装php的详细参数. 示例: ./configure \ --prefix=/usr/local/php --with-zlib ...

  6. Do waiting or suspended tasks tie up a worker thread?

      https://blogs.msdn.microsoft.com/askjay/2012/07/29/do-waiting-or-suspended-tasks-tie-up-a-worker-t ...

  7. Inno Setup入门(二十一)——Inno Setup类参考(7)

    复选框 复选框(CheckBox)用于多个并不互斥的几个选项中作出一个或者多选择,例如字体可以有粗体.斜体和下划线,这三种状态可以任意组合,像这样的选项可以采用复选框实现.Pascal脚本中对应的类是 ...

  8. 【fastJSON】利用fastJSON处理循环引用的问题

    下载fastJSON jar   com.alibaba.fastjson 第一种:[写死的] 将需要序列化的字段传递进去,得到结果 //需要序列化的实体+字段 SimplePropertyPreFi ...

  9. 解决marathon上docker实例一直waitting的问题

    可能原因: 1. mesos-slave上资源不够,一般是内存不够.可上mesos-master:5050上查看 2. 宿主机上没有镜像,一直在拉或拉不到.上宿主机上查看: docker images ...

  10. 二.Consumer、Producer简单例子

    1.先导入jar包,我使用的是maven <dependency> <groupId>com.alibaba.rocketmq</groupId> <arti ...