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. 【Huffman树贪心+优先队列】POJ3253-Fence Repair

    思路详见之前的贪心专题,用优先队列来代替之前的插入排序,效率为O(nlogn) #include<iostream> #include<cstdio> #include< ...

  2. 【tarjan+缩点】BZOJ1051-受欢迎的牛

    [题意] 每一头牛的愿望就是变成一头最受欢迎的牛.现在有N头牛,给你M对整数(A,B),表示牛A认为牛B受欢迎. 这种关系是具有传递性的,如果A认为B受欢迎,B认为C受欢迎,那么牛A也认为牛C受欢迎. ...

  3. Python的函数参数和递归参数

    位置参数 def power(x): return x*x; 默认参数 指的是在函数定义的时候,就赋予一些参数默认值,在调用这个函数的时候不必多次传入重复的参数值. 如定义一个多次输出同一个年龄阶段和 ...

  4. HDU 5631 Rikka with Graph 暴力 并查集

    Rikka with Graph 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5631 Description As we know, Rikka ...

  5. centos6.5编译安装nginx

    系统64位 centos6.5 nginx官网:http://nginx.org/ 下载nginx源码包: wget  http://nginx.org/download/nginx-1.6.2.ta ...

  6. Ubuntu 16.04通过NetworkManager(GUI)配置网桥

    说明:配置好网桥之后一定要重启,不然不生效.这个是Desktop版GUI设置的问题.Server版不会. 配置: 参考: http://www.jb51.net/LINUXjishu/333778.h ...

  7. LaTeX:Figures, Tables, and Equations 插入图表和公式

    Figures To insert a figure in a LaTeX document, you write lines like this: \begin{figure} \centering ...

  8. 一、Instrument之Core Animation工具

    一.Instrument 三个方法: (1).按下Command + I打开Instrument; (2).Xcode->product->profile; (3).Xcode->O ...

  9. RMAN备份与恢复之概念一

    1.  数据库完全备份: 按归档模式分为归档和非归档 归档模式 打开状态,属于非一致性备份 关闭状态,可以分为一致性和非一致性 非归档模式 打开状态,非一致性备份无效 关闭状态,一致性备份,非一致性备 ...

  10. 安装red5 1.0.1版本Java_home不能用Java7

    安装red5     1.0.1一直出现问题,安装顺利可以过,但是一访问老是报错. 用1.0之前的版本则没有问题.好一顿折腾,查看log发现问题出在tomcat 的nio上,查询这个问题有回复说是jr ...