发表于: 2020-06-28 23:35:26
1 1750
今天完成的事情:
最近几天服务器的数据库老是被黑,烦死了
修改了服务器mysql的密码和端口,打开了服务器防火墙,不知道有没有用。
研究了一下昨天那个DESUtil,好像它就是一个工具类
里面的getEncryptString()方法是加密的
getDecryptString()则是解密的。
我们要使用的时候只需要调用这两个方法就可以了。
再新建一个MD5Util类:
package com.jnshu.util;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class MD5Util {
public final static String stringToMD5(String s){
char hexDigits[]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
byte[] btInput = s.getBytes();
MessageDigest mdInst = null;
try {
// 获得MD5摘要算法的 MessageDigest 对象
mdInst = MessageDigest.getInstance("MD5");
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
// 使用指定的字节更新摘要
mdInst.update(btInput);
// 获得密文
byte[] md = mdInst.digest();
// 把密文转换成十六进制的字符串形式
int j = md.length;
char str[] = new char[j * 2];
int k = 0;
for (int i = 0; i < j; i++) {
byte byte0 = md[i];
str[k++] = hexDigits[byte0 >>> 4 & 0xf];
str[k++] = hexDigits[byte0 & 0xf];
}
return new String(str);
}
}
改写controller和拦截器:
登录页面:
@RequestMapping(value = "login")
public String login(Account account, Model model, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception{
logger.info("加密前的信息:"+account);
if (account.getUsername() != null && account.getPassword() != null) {
//将密码通过MD5进行加密
String passwordMD5 = MD5Util.stringToMD5(account.getPassword());
account.setPassword(passwordMD5);
logger.info("加密后的信息:"+account);
Account account1 = accountService.select(account);
//验证账号密码是否正确
if ( account1 != null) {
logger.info("登录成功");
Long id = account1.getId();//根据用户名获取id
//使用系统当前时间生成唯一token,格式为键值对
String token = id + "=" + System.currentTimeMillis();
//使用DES加密
String tokenDES =DESUtils.getEncryptString(token);
logger.info("加密后的token:" + tokenDES);
//保存到cookies中
Cookie cookie = new Cookie("token",tokenDES);
//设置cookie过期时间 单位为秒
cookie.setMaxAge(7000);
//设置cookie有效路径
cookie.setPath("/");
httpServletResponse.addCookie(cookie);
model.addAttribute("username", account.getUsername());
//httpServletResponse.sendRedirect(httpServletRequest.getContextPath() + "/u/profession");
return "redirect:/u/profession";
} else {
model.addAttribute("error", "账号或密码错误");//如果账号密码错误则提示该消息
//httpServletResponse.sendRedirect(httpServletRequest.getContextPath() + "/login");
return "login";
}
} else {
model.addAttribute("error","请先登录");//如果未登录就访问/u/profession则提示该消息
httpServletResponse.sendRedirect(httpServletRequest.getContextPath() + "/login");
return "login";
}
}
拦截器:
package com.jnshu.Interceptor;
import com.jnshu.pojo.Account;
import com.jnshu.service.AccountService;
import com.jnshu.util.DESUtils;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* 自定义拦截器
*/
public class MyInterceptor1 implements HandlerInterceptor {
@Autowired
AccountService accountService;
private static Logger logger = LogManager.getLogger(MyInterceptor1.class);
public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object handler) throws Exception {
// 判断session
Cookie[] cookies = httpServletRequest.getCookies();
logger.info("Cookie长度为: " + cookies.length);
logger.info("拦截器获取到的Cookie: " + String.valueOf(cookies));
if (cookies != null) {
logger.info("开始遍历");
// 遍历
for (Cookie cookie : cookies) {
logger.info("当前cookie的值: " + cookie.getValue() + " 名字为:" + cookie.getName());
// 判断是否有token
if (cookie.getName().equals("token")) {
String tokenDES = cookie.getValue();
logger.info("tokenDES: " + tokenDES);
String token = DESUtils.getDecryptString(tokenDES);//解密
logger.info("token的解密value:" + token);
// 分割字符串 获取id
Long id = Long.valueOf(token.split("=")[0]);
logger.info("id为: " + id);
return true;
}
}
}
// token验证失败 跳回登陆页面
// httpServletRequest https://blog.csdn.net/gris0509/article/details/6340987
httpServletResponse.sendRedirect(httpServletRequest.getContextPath() + "/tologin");
return false;
}
}
这里我理解错了,以为是用DES和MD5将密码加密两次。后面咨询了师兄之后了解到:好像是用户第一次登录时输入账号密码,将密码用DES进行加密,用MD5对cookie进行加密。
收获:学习了一下MD5加密,修改了代码对登录进行加密。
遇到的问题:关于Cookie跨域的问题,以及session还有些不懂。
明天计划的事情:完善代码,补一下session和cookie的基础知识。
评论