<%@page import="sun.misc.BASE64Encoder"%>
<%@page import="java.util.Base64.Encoder"%>
<%@page import="java.security.MessageDigest"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%!
//定义MD5加密的KEY
public static final String KEY = "wooyoohoo@163.com";
%>
<%
//设置请求和响应的编码格式
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8"); //判断用户的行为
String action = request.getParameter("action"); System.out.println(action); if("login".equals(action)){
//登录
String userName = request.getParameter("username");
String pwd = request.getParameter("password");
//获取有效时长
String time = request.getParameter("time"); if(userName!=null && !userName.isEmpty()){
MessageDigest digest = MessageDigest.getInstance("MD5");
//将用户名称+KEY进行MD5加密
String encodeStr = new BASE64Encoder().encode(digest.digest((userName+KEY).getBytes("utf-8")));
//保存用户名称
Cookie userNameCookie = new Cookie("username",userName);
Cookie encodeCookie = new Cookie("ssid",encodeStr); //设置有效期
userNameCookie.setMaxAge(Integer.parseInt(time));
encodeCookie.setMaxAge(Integer.parseInt(time)); //设置Cookie
response.addCookie(userNameCookie);
response.addCookie(encodeCookie); //重新访问该页面(添加参数System.currentTimeMillis()禁止浏览器缓存页面内容)------------->此处重新请求该页面是为了在一个页面中处理完毕所有逻辑
response.sendRedirect(request.getRequestURI()+"?"+System.currentTimeMillis());
return;
}
}else if("logout".equals(action)){
//退出[清除userNameCookie和encodeCookie]
Cookie userNameCookie = new Cookie("username","");
Cookie encodeCookie = new Cookie("ssid",""); userNameCookie.setMaxAge(0);
encodeCookie.setMaxAge(0); response.addCookie(userNameCookie);
response.addCookie(encodeCookie); //重新访问该页面(添加参数System.currentTimeMillis()禁止浏览器缓存页面内容)------------->此处重新请求该页面是为了在一个页面中处理完毕所有逻辑
response.sendRedirect(request.getRequestURI()+"?"+System.currentTimeMillis());
return;
} String account = null;
String ssid = null; boolean isLogin = false; //获取Cookie信息
Cookie[] cookies = request.getCookies();
if(cookies!=null && cookies.length>0){
//判断用户信息
for(int i=0;i<cookies.length;i++){
if(cookies[i].getName().equals("username")){
//获取账号
account = cookies[i].getValue();
}else if(cookies[i].getName().equals("ssid")){
//获取账号和KEY加密后的字符串
ssid = cookies[i].getValue();
}
}
} if(account!=null && ssid!=null){
System.out.println(account);
String getSSID = new BASE64Encoder().encode(MessageDigest.getInstance("MD5").digest((account+KEY).getBytes("utf-8")));
System.out.println(getSSID);
System.out.println(ssid);
if(getSSID.equals(ssid)){
isLogin = true;
}
}
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>利用Cookie实现永久登录</title>
</head>
<body>
<%
if(isLogin){
%>
<!-- 显示登陆后的信息 -->
<span>欢迎回来<% out.print(account);%></span><button onclick="javascript:{window.location.href='<%=request.getRequestURI()%>?action=logout'}">注销</button>
<%
}else{
%>
<!-- 显示登录界面进行登录操作 -->
<form action="<%=request.getRequestURI()%>?action=login" method="post">
账号:&nbsp;<input type="text" name="username"><br>
密码:<input type="password" name="password">
<br>
<input type="radio" value="<%=30*60 %>" name="time">30分钟有效<br>
<input type="radio" value="<%=7*24*60*60 %>" name="time">7天有效<br>
<input type="radio" value="<%=30*24*60*60 %>" name="time">30天有效<br>
<input type="submit" value="登录">
</form>
<%
}
%>
</body>
</html>

利用Cookie保存用户身份信息实现免登录的更多相关文章

  1. java的web项目中使用cookie保存用户登陆信息

    本文转自:http://lever0066.iteye.com/blog/1735963 最近在编写论坛系统的实现,其中就涉及到用户登陆后保持会话直到浏览器关闭,同时可以使用cookie保存登陆信息以 ...

  2. jQuery 操作cookie保存用户浏览信息

    使用jQuery操作cookie之前需要引入jQuery的一个cookie小组件js,代码如下:   /*         jQuery cookie plugins */jQuery.cookie ...

  3. 使用Cookie保存用户和密码然后自动登录

    login.html <!DOCTYPE html> <html lang="en"> <head> <meta charset=&quo ...

  4. [转]Asp.Net Core 简单的使用加密的Cookie保存用户状态

    本文转自:http://www.cnblogs.com/Joes/p/6023820.html 在以前的Asp.Net中可以用 FormsAuthentication 类的一系列方法来使用加密的Coo ...

  5. Asp.Net Core 简单的使用加密的Cookie保存用户状态

    在以前的Asp.Net中可以用 FormsAuthentication 类的一系列方法来使用加密的Cookie存储用户身份,使用简单,可控性强.在Asp.Net Core中是否也可以的?答案是当然的. ...

  6. Winform框架中窗体基类的用户身份信息的缓存和提取

    在Winform开发中,有时候为了方便,需要把窗体的一些常规性的数据和操作函数进行封装,通过自定义基类窗体的方式,可以实现这些封装管理,让我们的框架统一化.简单化的处理一些常规性的操作,如这里介绍的用 ...

  7. 基于SqlSugar的开发框架循序渐进介绍(6)-- 在基类接口中注入用户身份信息接口

    在基于SqlSugar的开发框架中,我们设计了一些系统服务层的基类,在基类中会有很多涉及到相关的数据处理操作的,如果需要跟踪具体是那个用户进行操作的,那么就需要获得当前用户的身份信息,包括在Web A ...

  8. Servlet 利用Cookie实现一周内不重复登录

    import java.io.IOException;import java.io.PrintWriter; import javax.servlet.ServletException;import ...

  9. [py][mx]django的cookie和session操作-7天免登录

    浏览器同源策略(same-origin policy) csrf攻击防御核心点总结 django的cookie和session操作-7天免登录 flask操作cookie&django的see ...

随机推荐

  1. sell02 展现层编写

    # API ###商品列表 ``` GET /sell/buyer/product/list ``` 参数 ``` 无 ``` 返回 ``` { "code": 0, " ...

  2. Spring Boot 高效数据聚合之道

    项目地址和示例代码: https://github.com/lvyahui8/spring-boot-data-aggregator 背景 接口开发是后端开发中最常见的场景, 可能是RESTFul接口 ...

  3. Java对象在内存中的状态

    可达的/可触及的 Java对象呗创建后,如果被一个或者多个变量引用,那就是可达的,即从根节点可以触及到这个对象. 其实就是从根节点扫描,只要这个对象在引用链中,那就是可触及的. 可恢复的 Java对象 ...

  4. 4.XXE (XML External Entity Injection)

    XXE (XML External Entity Injection) 0x01 什么是XXE XML外部实体注入 若是PHP,libxml_disable_entity_loader设置为TRUE可 ...

  5. 2. Web渗透测试中常见逻辑漏洞解析与实战

    注:以下漏洞示例已由相关厂商修复,切勿非法测试! 0x01 漏洞挖掘 01  注册 注册中最常见的有两个,一个是恶意注册,另一个是账户遍历.一个好的注册界面应该是这样 或者这样的 而不是这样的 要么使 ...

  6. 实现one hot encode独热编码的两种方法

    实现one hot encode的两种方法: https://stackoverflow.com/questions/37292872/how-can-i-one-hot-encode-in-pyth ...

  7. JDK1.8源码(十一)——java.util.TreeMap类

    在前面几篇博客分别介绍了这样几种集合,基于数组实现的ArrayList 类,基于链表实现的LinkedList 类,基于散列表实现的HashMap 类,本篇博客我们来介绍另一种数据类型,基于树实现的T ...

  8. CentOS7 LVM磁盘扩容

    1:创建磁盘分区(注意红色命令部分) [root@hongyin-test- ~]# fdisk /dev/sda Welcome to fdisk (util-linux ). Changes wi ...

  9. kuangbin专题十二 HDU1029 Ignatius and the Princess IV (水题)

    Ignatius and the Princess IV Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32767 K ( ...

  10. shell学习(14)- who

    Linux who命令用于显示系统中有哪些使用者正在上面,显示的资料包含了使用者 ID.使用的终端机.从哪边连上来的.上线时间.呆滞时间.CPU 使用量.动作等等. 使用权限:所有使用者都可使用. 语 ...