使用cookie实现自动登录
一、从登录——>主页面,进行的过程是,输入 用户名和密码,以及验证码,点击“登录”跳转到Activity.jsp
login1.action(跳转到登录页面)
/** 跳转到login(有积分排行榜) */
@RequestMapping("/login1.action")
public String login() {
return "login";
}
login.action(从登录页面跳转到主页面)
/** 登录 */
@RequestMapping("/login.action")
public String login(String nickName, String password, String authCode,
String autoLogin, HttpSession session, Model model,
HttpServletRequest req, HttpServletResponse resp) {
System.out.println("autoLogin:" + autoLogin);//自动登录多选框状态,未选中时为null,选中时为on
// 登录积分和等级
PointAction loginPoint = null;
Graderecord loginLevel = null;
if (authCode == null || authCode == "") {
model.addAttribute("msg", "请填写验证码!");
return "login";
}
if (!authCode.equals(session.getAttribute("authCode"))) {
model.addAttribute("msg", "验证码错误");
return "login";
}
try {
// 根据页面用户名查询用户信息
Memberinfo memberinfo = memberservice.loginMemberInfo(nickName);
session.setAttribute("nickName", nickName);
// 判断密码是否正确
if (password.equals(memberinfo.getPassword())) {
memberservice.loginAction(memberinfo, loginPoint, session,
loginLevel);
if (autoLogin != null) {
// 保存cookie
try {
Cookie usernameCookie = new Cookie("nickname",
URLEncoder.encode(nickName, "utf-8"));
Cookie passwordCookie = new Cookie("password", password);
usernameCookie.setMaxAge( * * );// 设置一年有效期
passwordCookie.setMaxAge( * * );
usernameCookie.setPath("/");// 可在同一应用服务器内共享方法
passwordCookie.setPath("/");
resp.addCookie(usernameCookie);
resp.addCookie(passwordCookie);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
return "activity";
} else {
model.addAttribute("msg", "请输入正确密码");
}
return "login";
} catch (MemberServiceException e) {
e.printStackTrace();
model.addAttribute("msg", e.getMessage());
return "login";
}
}
在此时,进行Cookie的保存,即中间的这一段代码
if (autoLogin != null) {//判断自动登录多选框的状态,若选中则进行Cookie的保存
// 保存cookie
try {
Cookie usernameCookie = new Cookie("nickname",
URLEncoder.encode(nickName, "utf-8"));
Cookie passwordCookie = new Cookie("password", password);
usernameCookie.setMaxAge( * * );// 设置一年有效期
passwordCookie.setMaxAge( * * );
usernameCookie.setPath("/");// 可在同一应用服务器内共享方法
passwordCookie.setPath("/");
resp.addCookie(usernameCookie);
resp.addCookie(passwordCookie);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
return "activity";
在index.jsp页面中调用checkAutoLoginAction.action
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>"> <title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="refresh" content="0;url='checkAutoLoginAction.action'">
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
</head> <body>
</body>
</html>
checkAutoLoginAction.action(取出Cookie
// ------自动登录----------------------------------------------------------------------------------------------------------------------------
@RequestMapping("checkAutoLoginAction.action")
public String checkAutoLoginAction(HttpServletRequest req,
HttpServletResponse resp, HttpSession session) throws Exception {
Cookie[] cookies = req.getCookies();
System.out.println("cookie是否为空:" + cookies);
String nickname = "";
String password = "";
if (cookies != null) {//判断Cookie是否为空
for (Cookie c : cookies) {
if ("nickname".equals(c.getName())) {
nickname = URLDecoder.decode(c.getValue(), "utf-8");
}
if ("password".equals(c.getName())) {
password = URLDecoder.decode(c.getValue(), "utf-8");
}
}
Memberinfo m = memberservice.login(nickname, password);
session.setAttribute("nickName", m.getNickName());
System.out.println("m是否为空:" + m);
if (m != null) {//如果根据Cookie中的用户名和密码查询出的用户信息存在且正确,再进行一系列的更新跳转工作
Calendar c = Calendar.getInstance();
c.setTime(m.getLatestDate());
String date = new SimpleDateFormat("EEEE").format(c.getTime());
return "activity";
}
}
req.setAttribute("msg", "账户密码失效,请重新登录");
return "forward:/login1.action";
}
三、操作
第一步,输入http://localhost:8888/ssh/login1.action,跳转到登录页面
第二步,输入nickName和password,勾选“自动登录”,点击“登录”,跳转到Activity.jsp主页面
第三步,若成功登录到主页面,则注销
第四步,输入http://localhost:8888/ssh/index,即可使用checkAutoLoginAction.action,直接跳转到主页面,省略了第二步
使用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技术自动登录
user public class User implements Serializable{ private String username; private String nick; privat ...
- 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的出现就是为了解决这个问题,第一次登陆后服 ...
随机推荐
- nginx upstream的几种配置方式
nginx 的upstream目前支持4种方式的分配 1.轮询(默认) 每个请求按时间顺序逐一分配到不同的后端服务器 ,如果后端服务器down掉,能自动剔除. 2.weight指定轮询几率,weigh ...
- Oracle中REGEXP_SUBSTR函数(字符串转多行)
Oracle中REGEXP_SUBSTR函数 Oracle中REGEXP_SUBSTR函数的使用说明: 题目如下: 在oracle中,使用一条语句实现将'17,20,23'拆分成'17','20',' ...
- 如何利用MATLAB并行计算缩短程序运行时间
本来CPU就是双核,不过以前一直注重算法,没注意并行计算的问题.今天为了在8核的dell服务器上跑程序才专门看了一下.本身写的程序就很容易实现并行化,因为beamline之间并没有考虑相互作用.等于可 ...
- Maven构建war项目添加版本号
上午接到一个新的需求,项目的war包打包之后,放在了阿里的OSS上,供其他项目下载更新时使用,但是只有一个项目名,也就是pom的artifactId,预期的结果是要加上一个版本号,能区分出是什么时候打 ...
- 9-EasyNetQ之基于主题的路由
RabbitMQ有一个很酷的功能,基于主题的路由,这个功能允许订阅者基于多个条件去过滤消息.一个主题是由点号分隔的单词列表,随消息一同发布.例如:"stock.usd.nyse" ...
- jQuery-图片的放大镜显示效果(不需要大小图)
问题:当页面高度很大时,放大图片的图层不会跟随着 1.demo.html ;display:none;} #tip s {position:absolute;top:40px;l ...
- [poj1703]Find them, Catch them(种类并查集)
题意:食物链的弱化版本 解题关键:种类并查集,注意向量的合成. $rank$为1代表与父亲对立,$rank$为0代表与父亲同类. #include<iostream> #include&l ...
- 奇葩问题 eclipse下 maven项目 java Resource报个小红叉,然而里面却没有小红叉
之前没注意,不知是一开始就有还是这两天才有,说下解决方案: 右击项目“Properties”,在弹出的“Properties”的左侧边框,单击“Project Facets”,打开“Project F ...
- static、静态变量、静态方法
1 静态:static 1.1 用法 是一个修饰符:用于修饰成员(成员变量和成员函数) 1.2 好处 当成员变量被静态static修饰后,就多了一种调用方式,除了可以被对象调用外,还可以直接被类名调用 ...
- ROS Learning-019 learning_tf-03(编程) 添加额外的坐标系 (Python版)
ROS Indigo learning_tf-03 添加额外的坐标系 (Python版) 我使用的虚拟机软件:VMware Workstation 11 使用的Ubuntu系统:Ubuntu 14.0 ...