登陆、注册Md5加密及cookie记住密码

这篇博客展示了如何在登录注册过程中使用MD5加密用户密码,并利用Cookie实现记住密码的功能。用户输入的用户名和密码经过MD5加密处理,提高安全性。同时,通过检查Cookie来自动填充已记住的用户名和密码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package com.ecai.front.controller;






import java.util.List;


import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;


import com.ecai.bean.Product;
import com.ecai.bean.User;
import com.ecai.front.service.SearchService;






/**  
* <p>Title: IndexController</p>  
* <p>Description: </p>  
* @author TF 
* @date 2018年4月30日  
*/ 
@Controller
public class IndexController {

@Autowired
private SearchService SearchService;





@RequestMapping(value="index")
    public String list(String keywords, Model model) throws Exception{  

List<Product> list = SearchService.searchBykeywords(keywords);
model.addAttribute("list", list);
System.out.println(list);
return "home/search";  
    }  

@RequestMapping("detail/{id}")
public ModelAndView productDetail(@PathVariable("id")String id,ModelAndView mav) throws Exception{  

Product product = SearchService.searchByid(id);
mav.addObject("detail", product);
mav.setViewName("home/detail");
return mav;  
}  

//默认跳转登陆页面
@RequestMapping("login")
public String  login(){

return "home/login";
}

//注册页面
@RequestMapping("register")
public String  register(){

return "home/register";
}

//注册页面
@RequestMapping("information")
public String  information(){

return "home/information";
}

   /*
     * 添加用户
     */
    @RequestMapping("/insertUser")
    public String insertUser(User u){
        //进行加密,页面传过来的不是明文,是一个哈希值,对哈希再加密
        String s=u.getUpwd();
        String smi=convertMD5(s);
        u.setUpwd(smi);
        SearchService.insertUser(u);
        return "home/login";
    }



    /*
     * MD5加密、cookie记住密码
     */
    @RequestMapping("dologin")  
    public String dologin(User u,ModelMap model,HttpServletRequest request,HttpServletResponse response)  {  
        //对用户登录传过来的哈希密码先进行加密
    HttpSession session = request.getSession();
   
String rem = request.getParameter("remember");
//处理中文账号和密码
if("yes".equals(rem)){
//创建cookie
Cookie c_username = new Cookie("cookie_username", u.getUname());
Cookie c_password = new Cookie("cookie_password", u.getUpwd());
response.addCookie(c_username);
response.addCookie(c_password);
     
        }
        String s=u.getUpwd();
        String smi=convertMD5(s);
        System.out.println("加密---------------"+smi);
        //加密后,与数据库存储的密码进行比对
        u.setUpwd(smi);
    User user= SearchService.dologin(u);
    System.out.println(user);
        if(user!=null ){  
                session.setAttribute("users",user);  
                model.put("hy", "欢迎光临!"); 
                return "home/index"; 
        }else{  
        model.put("error", " ");  
            model.put("error", "账号或密码错误");  
            return "home/login";  
        }  
    } 
 
/** 
      * 加密解密算法 执行一次加密,两次解密 
      */ 
 public static String convertMD5(String inStr){  


     char[] a = inStr.toCharArray();  
     for (int i = 0; i < a.length; i++){  
         a[i] = (char) (a[i] ^ 't');  
     }  
     String s = new String(a);  
     return s;  
 }


}


//Html

<%@page import="java.net.URLDecoder"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>


<head lang="en">
<meta charset="UTF-8">
<title>登录</title>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport"
content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">
<meta name="format-detection" content="telephone=no">
<meta name="renderer" content="webkit">
<meta http-equiv="Cache-Control" content="no-siteapp" />
<script type="text/javascript" src="<%=path%>/js/jquery-1.7.2.min.js"></script>
<script type="text/javascript" src="<%=path%>/js/jquery.md5.js"></script>
<link rel="stylesheet" href="<%=path%>/AmazeUI-2.4.2/assets/css/amazeui.css" />
<link href="<%=path%>/css/dlstyle.css" rel="stylesheet" type="text/css">


<script type="text/javascript">






</script>


<%
//接收cookie
Cookie[] cookies = request.getCookies();
if(cookies != null){
//遍历cookies数组
for(Cookie cs : cookies){
if("cookie_username".equals(cs.getName())){
String decode_username = URLDecoder.decode(cs.getValue(), "utf-8");
request.setAttribute("c_username", decode_username);
}
if("cookie_password".equals(cs.getName())){
String decode_password = URLDecoder.decode(cs.getValue(), "utf-8");
request.setAttribute("c_password", decode_password);
}
}
}
%>
</head>


<body>




${error}
<div class="login-boxtitle">
<a href="home.html"><img alt="logo"
src="<%=path%>/images/logobig.png" /></a>
</div>


<div class="login-banner">
<div class="login-main">
<div class="login-banner-bg">
<span></span><img src="<%=path%>/images/big.jpg" />
</div>
<div class="login-box">


<h3 class="title">登录商城</h3>


<div class="clear"></div>


<div class="login-form">
<form action="dologin" method="post" id="myForm" name="form22">
<div class="user-name uname" >
<label for="user"><i class="am-icon-user"></i></label> <input
type="text" name="uname" id="uname" value="${c_username}" placeholder="邮箱/手机/用户名">
</div>
<div id="userNameInfo"></div>
                </td>
<div class="user-pass">
<label for="password"><i class="am-icon-lock"></i></label> <input
type="password" name="upwd" id="upwd" value="${c_password}"  placeholder="请输入密码">
</div>



</div>


<div class="login-links">
<label class="checkbox-inline"> <input type="checkbox"
name="remember" id="remember"  value="yes" /> 记住密码
</label> <span class="pull-right"> <a href="#" class="am-fr">忘记密码</a>

<div class="am-cf">
<input type="submit" name="" value="登 录"
class="am-btn am-btn-primary am-btn-sm">
</div>
</form>

<a href="<%=path %>/register" class="zcnext am-fr am-btn-default">注册</a>
<br />
</div>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值