首页 技术 正文
技术 2022年11月15日
0 收藏 761 点赞 2,585 浏览 3176 个字
 <%@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>
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,030
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,520
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,368
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,148
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,781
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,859