cookie技术自动登录
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技术自动登录的更多相关文章
- cookie实现自动登录
有很多Web程序中第一次登录后,在一定时间内(如2个小时)再次访问同一个Web程序时就无需再次登录,而是直接进入程序的主界面(仅限于本机).实现这个功能关键就是服务端要识别客户的身份.而用Cookie ...
- C#检测并安装https站点的数字证书,CefSharp和HttpWebRequest通过会话Cookie实现自动登录访问https站点
HttpUtil工具类: using System; using System.Collections.Generic; using System.IO; using System.Linq; usi ...
- struts2与cookie实现自动登录和验证码验证
主要介绍struts2与cookie结合实现自动登录 struts2与cookie结合时要注意采用.action 动作的方式实现cookie的读取 struts2的jar包 链接数据库文件 db.pr ...
- 使用cookie下次自动登录
登录时勾选了自动登录处理: 1.加密账号和IP,保存在cookie中,cookie('auto', $value, $time) 2.解密cookie,取出账号和上次IP,判断上次IP==当前IP.账 ...
- 使用cookie实现自动登录
一.从登录——>主页面,进行的过程是,输入 用户名和密码,以及验证码,点击“登录”跳转到Activity.jsp login1.action(跳转到登录页面) /** 跳转到login(有积分排 ...
- Spring mvc session cookie实现自动登录
设计过程 1. user表存储用户名密码等信息,login表存放用户登陆状态的表 user表中存储username,pwd,等信息 login表存username,series(UUID),token ...
- 如何设计相对安全的cookie自动登录系统
很多网站登录的时候,都会有一个"记住我"功能,用户可以在限定时间段内免登录, 比如豆瓣.人人.新浪微博等都有这种设计.这种技术其实就是基于 cookie的自动登录, 用户登录的时候 ...
- 自己Cookie写的自动登录功能 包含BASE64 和MD5的使用
sql表 username password字段 User类 有 id username password等字段 Service有一函数 @Override public User findUser ...
- 爬虫模拟cookie自动登录(人人网自动登录)
什么是cookie? 在网站中,HTTP请求时无状态的,也就是说即使第一次和服务器连接后并且登录成功后,第二次请求服务器依然不能知道当前请求是谁,cookie的出现就是为了解决这个问题,第一次登陆后服 ...
随机推荐
- SD 一轮集训 day4 圣城鼠
非常强的构造题. 很显然的是我们要构造一个类似菊花图的东西,因为这样的话两点之间路径的点数会非常少,很容易满足第二个条件. 但是因为直接菊花图的话会不满足第一个条件,,,所以我们可以构造一个类菊花图. ...
- JVM堆 栈 方法区详解
一.栈 每当启用一个线程时,JVM就为他分配一个JAVA栈,栈是以帧为单位保存当前线程的运行状态 栈是由栈帧组成,每当线程调用一个java方法时,JVM就会在该线程对应的栈中压入一个帧 只有在调用一个 ...
- 论MORMOT序列的JSON格式
论MORMOT序列的JSON格式 JSON 数据使用 UTF-8 编码 BLOB 字段值会用 Base64编码 JSON数据是一个对象数组: [ {"col1":val11,&qu ...
- JavaScript基础入门教程(六)
说明 在看这篇博文之前还是希望读者阅读本系列前几篇文章,还有就是该系列需要读者拥有其它语言的编程基础,一些基本的知识点,比如什么是形参和实参将不再赘述.这篇博文主要讲函数. 函数的定义 在js种支持函 ...
- 二十四种设计模式:桥接模式(Bridge Pattern)
桥接模式(Bridge Pattern) 介绍将抽象部分与它的实现部分分离,使它们都可以独立地变化. 示例有一个Message实体类,对它的操作有Insert()和Get()方法,现在使这些操作的抽象 ...
- Qt 5.7 亮瞎眼的更新
Qt 5.7的beta版已经出来了,这将是一个超级重大的更新,主要有几个商业版的模块在GPLv3 open source 版的用户也可以用了,其中包括了两个很炫酷的模块: Qt Charts Qt D ...
- 【转载】【Todo】银弹与我们的职业
看到一段文字,不得不单独拎出来. 然后再借用一下g9老大的<银弹和我们的职业>中的话: 银弹和我们的职业发展有什么相干?很简单:我们得把时间用于学习解决本质困难.新技术给高手带来方便.菜鸟 ...
- 基于CentOS与VmwareStation10搭建hadoop环境
基于CentOS与VmwareStation10搭建hadoop环境 目 录 1. 概述.... 1 1.1. 软件准备.... 1 1.2. 硬件准备.... 1 2. 安装与配置虚拟机.. ...
- python中常用第三方库记录
python中有很多很好用的第三方库,现在记录一下这些库以及如何下载 一.virtualenv,这是一个可以将生产环境隔离开的python库,非常好用 在linux下使用pip install vir ...
- ORA-04030
ORA-04030: 在尝试分配...字节(...)时进程内存不足的原因分析及解决办法 正在使用的oracle 11g数据库,前天在用一段时间后(开始要较长时间才出现,后来较短时间就出现),频繁报OR ...