jsp------实现MD5加密
index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<%@page import="java.net.*" %>
<%@page import="comm.MakeMD5" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>MyIndex</title>
<meta http-equiv="imurl" content="no-cache">
</head> <body>
<%
boolean loginFlag=false;
String account=null;
String md5Account=null;
Cookie cookieArr[]=request.getCookies();
if(cookieArr!=null&&cookieArr.length>0){
for(Cookie cookie:cookieArr){
if(cookie.getName().equals("account")){
account=cookie.getValue();
account=URLDecoder.decode(account,"utf-8");
//System.out.print(account);
}
if(cookie.getName().equals("md5Account")){
md5Account=cookie.getValue();
md5Account=URLDecoder.decode(md5Account,"utf-8");
//System.out.print(md5Account);
}
}
} if(account!=null&&md5Account!=null){
loginFlag=md5Account.equals(MakeMD5.getMD5(account));
} if(loginFlag){
//request.getRequestDispatcher("successlogin.jsp").forward(request, response);
//response.sendRedirect("successlogin.jsp");
%>
<fieldset>
<legend>欢迎您回来</legend>
<table align="center">
<tr>
<td><%=account %>,欢迎您登陆本网站</td>
<td align="center">
<a href="foreverlogin?action=logout">注销登陆</a>
</td>
</tr>
</table>
</fieldset>
<%
}else{
%>
<fieldset>
<legend>用户登录</legend>
<form action="foreverlogin?action=login" method="post">
<table>
<tr>
<td>账 号:</td>
<td><input type="text" name="account"></td>
</tr>
<tr>
<td>密 码:</td>
<td><input type="text" name="password"></td>
</tr>
<tr>
<td>有效期:</td>
<td>
<input type="radio" name="timeout" value="-1" checked="checked">
关闭浏览器即失效
<input type="radio" name="timeout" value="<%=30*24*60*60%>">
30天内有效
<input type="radio" name="timeout" value="<%=Integer.MAX_VALUE%>">
永久有效
</td>
</tr>
<tr>
<td>
<input type="submit" value="登陆">
<input type="reset" value="重置">
</td>
</tr>
</table>
</form>
</fieldset>
<%
}
%>
</body>
</html>
src/comm/foreverlogin.java
package comm; import java.io.IOException;
import java.net.URLEncoder; import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; public class foreverlogin extends HttpServlet {
private static final long serialVersionUID = 1L; public foreverlogin() {
super();
} public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
} public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { doPost(request,response);
} public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8"); String action=request.getParameter("action");
if(action.equals("login")){
login(request,response);
}
else if(action.equals("logout")){
logout(request,response);
}
} //login
public void login(HttpServletRequest request, HttpServletResponse response) throws ServletException,IOException{
String account=request.getParameter("account");
//String password=request.getParameter("password");
int timeout=Integer.parseInt(request.getParameter("timeout")); String md5Account=MakeMD5.getMD5(account); //采用MD5算法加密
account=URLEncoder.encode(account,"utf-8"); //账号为中文时需要转换Unicode才能保存在Cookie中
Cookie accountCookie=new Cookie("account",account);
accountCookie.setMaxAge(timeout);
Cookie md5AccountCookie=new Cookie("md5Account",md5Account);
md5AccountCookie.setMaxAge(timeout);
response.addCookie(accountCookie);
response.addCookie(md5AccountCookie); //将线程休眠1秒后在执行
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} //response.sendRedirect("cookie/resultlogin.jsp?"+System.currentTimeMillis());
response.sendRedirect("cookie/index.jsp?"+System.currentTimeMillis());
} //logout
public void logout(HttpServletRequest request, HttpServletResponse response) throws ServletException,IOException{
Cookie accountCookie=new Cookie("account","");
accountCookie.setMaxAge(0);
Cookie md5AccountCookie=new Cookie("md5Account","");
md5AccountCookie.setMaxAge(0);
response.addCookie(accountCookie);
response.addCookie(md5AccountCookie); //将线程休眠一秒后在执行
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} response.sendRedirect("cookie/index.jsp?"+System.currentTimeMillis());
} public void init() throws ServletException {
// Put your code here
} }
src/comm/MakeMD5.java
package comm;
import java.security.MessageDigest;
public class MakeMD5 {
public final static String getMD5(String str){
// 用来将字节转换成 16 进制表示的字符
char hexDiagiArr[]={'0','1','2','3','4','5','6','7','8','9','0','a','b','c','d','e','f'};
MessageDigest digest=null;
try{
digest=MessageDigest.getInstance("MD5"); //创建MD5算法摘要
digest.update(str.getBytes()); //更新摘要
byte mdBytes[]=digest.digest(); //加密,并返回字节数组
//新建字符数组,长度为myBytes字节数组的2倍,用于保存加密后的值
char newCArr[]=new char[mdBytes.length*2];
int k=0;
for(int i=0;i<mdBytes.length;i++){
byte byte0=mdBytes[i];
newCArr[k++]=hexDiagiArr[byte0>>>4&0x0f]; //取字节中高 4 位的数字转换,>>>为逻辑右移,将符号位一起右移
newCArr[k++]=hexDiagiArr[byte0&0x0f]; //取字节中低 4 位的数字转换
//针对字符0-9的,0-9的ascii码值为0x30,0x31,0x32 0x33 ...0x39,
//因此与0x0f按位与后只保留个位上的书即0x0,0x1,。。。0x9
// 0000 1010
//& 0000 1111
// 0000 1010
}
return String.valueOf(newCArr); //将转换后的字符转换为字符串
}
catch(Exception e){
e.printStackTrace();
}
return null;
}
}
jsp------实现MD5加密的更多相关文章
- SpringSecurity 登录 - 以及Md5加密
我们现在开放一个链接给其他系统,来访问我们的系统 http://localhost:8080/hulk-teller-web/haihui!init.jspa?loginId=teller01& ...
- 使用MD5加密的登陆demo
最近接手了之前的一个项目,在看里面登陆模块的时候,遇到了一堆问题.现在记录下来. 这个登陆模块的逻辑是这样的 1 首先在登陆之前,调用后台的UserLoginAction类的getRandomKey方 ...
- Java实现对文本文件MD5加密并ftp传送到远程主机目录
需求描述: 客户出完账之后需要把出账的数据以文本文件的方式传送给收入管理系统,客户以前是通过本地的一个工具软件上传的,由于安全监管的原因,不允许在本地使用工具上传,因此客户希望我们在已经上线使用的系统 ...
- JAVAEE——SSH项目实战05:用户注册、登陆校验拦截器、员工拜访客户功能和MD5加密
作者: kent鹏 转载请注明出处: http://www.cnblogs.com/xieyupeng/p/7170519.html 一.用户注册 显示错误信息到页面上的另一种方法: public ...
- MD5加密 及 防止重复提交
1.JSP页面 <%@page import="cn.gs.ly.app2.MD5Util"%> <%@page import="java.util.U ...
- 关于CryptoJS中md5加密以及aes加密的随笔
最近项目中用到了各种加密,其中就包括从没有接触过得aes加密,因此从网上各种查,官方的一种说法: 高级加密标准(英语:Advanced Encryption Standard,缩写:AES),在密码学 ...
- Android数据加密之MD5加密
前言: 项目中无论是密码的存储或者说判断文件是否是同一文件,都会用到MD5算法,今天来总结一下MD5加密算法. 什么是MD5加密? MD5英文全称“Message-Digest Algorithm 5 ...
- android MD5加密
public class MD5Uutils { //MD5加密,32位 public static String MD5(String str) { MessageDige ...
- IOS 杂笔-9 (MD5 加密)
首先是一段对MD5的简介 *出自一位大牛之手* Message Digest Algorithm MD5(中文名为消息摘要算法第五版)为计算机安全领域广泛使用的一种散列函数,用以提供消息的完整性保护 ...
- JS中使用MD5加密
下载 MD5 使用MD5加密的方法:下载md5.js文件,在网页中引用该文件: < script type="text/javascript" src="md5.j ...
随机推荐
- Ubuntu优化-py用机器
关闭防火墙 ufw disable pip换源 yum install python-pip -y mkdir ~/.pip cat > pip.conf<<a [global] i ...
- 使用js使某个按钮在5秒内不能重复点击
<head> <!--参考:http://illy.iteye.com/blog/1534276 --> <!-- http://y.dobit.top/Detail/1 ...
- oracle中if/else功能的实现的3种写法
1.标准sql规范 一.单个IF 1. if a=... then ......... end if; 2. if a=... then ...... else .... end if; 二.多个IF ...
- Activiti系列:为什么Activiti 5.18 的REST的api总是返回404错误
REST api可以访问了,如下 1.修改db.properties配置文件,让他访问sql server 2.在浏览器中输入如下地址,注意中间有一个service,这点和之前的不一样,在<Ac ...
- mysql命令行
mysql -u root -p create database bookstore; drop database bookstore; use bookstore create table user ...
- IOS开发之——自定义导航控制器
BasicNavigationViewController:UINavigationViwController /* 隐藏导航底部线条 */ -(void)viewDidLoad{ [super ...
- .NET MVC控制器向视图传递数据的四种方式
.NET MVC控制器向视图传递数据的四种方式: 1.ViewBag ViewBag.Mvc="mvc"; 2.ViewData ViewBag["Mvc"] ...
- Slider 滚动条 Pagination分页插件 JS Ajax 数据范围筛选 加载 翻页 笔记
入职以后的第二个任务 根据用户所选的价格范围 筛选数据 修复BUG - 筛选数据后 总数没有更新.列表显示错误.翻页加载错误 用到的一些知识点 jquery插件系列之 - Slider滑块 max ...
- Bootstrap系列 -- 39. 导航条添加标题
在Web页面制作中,常常在菜单前面都会有一个标题(文字字号比其它文字稍大一些),其实在Bootstrap框架也为大家做了这方面考虑,其通过“navbar-header”和“navbar-brand”来 ...
- [bzoj 1004][HNOI 2008]Cards(Burnside引理+DP)
题目:http://www.lydsy.com/JudgeOnline/problem.php?id=1004 分析: 1.确定方向:肯定是组合数学问题,不是Polya就是Burnside,然后题目上 ...