发表于: 2018-05-19 22:44:22
1 1251
今天完成的事情:
1. 把demo集成到任务代码中,并调整好逻辑
效果:点击职业后,因为是/u/的路径,所以被拦截,cookie验证不成功,转到登录页面
没有账号,就点击注册进入注册页面
注册成功后返回登录页面,输入账号密码,成功跳转到,/u/的职业页面,不成功返回登录页
逻辑代码
@RequestMapping(value = "/register",method = RequestMethod.POST)
public ModelAndView register(User user){
logger.info("进入注册页");
logger.info("前台传进来的信息:"+user.toString());
//以用户名为盐
String salt = user.getUserName();
//对密码加盐和md5加密
String passwordMd5 = Md5Util.stringToMD5(user.getPassword()+salt);
logger.info("加盐加密后的密码:"+passwordMd5);
user.setPassword(passwordMd5);
serviceManage.insertUser(user);
logger.info("注册的信息是:"+user);
ModelAndView mav = new ModelAndView();
mav.setViewName("login");
return mav;
}
@RequestMapping(value = "/login",method = RequestMethod.POST)
public ModelAndView login(HttpServletRequest request, HttpServletResponse response) throws Exception {
logger.info("进入登陆页");
String userName = request.getParameter("userName");
String password = request.getParameter("password");
logger.info("前台传进来的登录账号:"+userName+",密码:"+password);
//对密码加盐加密,然后和数据库里的比较
String passwordMd5 = Md5Util.stringToMD5(password+userName);
logger.info("加盐加密后的密码:"+passwordMd5);
User user = serviceManage.findNameUser(userName);
System.out.println(user);
if (user != null && (user.getUserName()).equals(userName) && (user.getPassword()).equals(passwordMd5)){
logger.info("登录成功");
Long id = user.getUserId();
// 使用用户ID+系统当前时间生成唯一token, 格式为键值对
String token = id + "|" + System.currentTimeMillis();
logger.info("生成的token是:"+token);
// 用DES加密jey 必须8位
byte[] bytes = DESUtil.encrypt(token,skey);
logger.info("加密后的token: " + DESUtil.toHexString(bytes).toUpperCase());
logger.info("加密后的Base64 token: " + Base64.encodeBase64String(bytes));
// 保存到 Cookie 中
// 使用 Base64.encodeBase64String(bytes)) 报错 RFC6265 Cookie values may not contain control characters .
// 原因: import org.apache.commons.net.util.Base64 生成的Base64带换行符, 会导致报错
// 更换为 org.apache.commons.codec.binary.Base64;
Cookie cookie = new Cookie("token",Base64.encodeBase64String(bytes));
// 设置 Cookie 过期时间 单位为秒
cookie.setMaxAge(7000);
// 设置 Cookie 有效路径
cookie.setPath("/");
logger.info("新生成的Cookie-效时间-值: " + cookie.getName() + "-->" + cookie.getMaxAge() + "-->" + cookie.getValue() + cookie.getPath());
response.addCookie(cookie);
ModelAndView mav = new ModelAndView();
mav.setViewName("redirect:/u/pro");
return mav;
}else {
logger.info("登录失败");
ModelAndView mav = new ModelAndView();
mav.setViewName("redirect:/main");
return mav;
}
}
明天计划的事情:
深度思考和算法
遇到的问题:
1. 在登录的时候,如果用户名输入没有的用户名会出现500错误
找到相应的代码
删除掉之后就好了
收获:
彻底完成任务的代码部分
成功展示部分,因为时间晚了,遇到问题部分的那行代码没删(明天从新打个包),所以登录的时候用注册过的代码,可以自己注册,也可以使用(username:444 password:444)
任务进度:task5
任务开始时间:2018年5月15日
预计demo时间:2018年5月21日
是否延期:
延期理由:
评论