发表于: 2020-08-26 21:54:05
1 1442
重构任务5的代码 测试性能
package com.jnshu.controller;
import com.jnshu.pojo.Account;
import com.jnshu.service.AccountService;
import com.jnshu.util.DESUtils;
import com.jnshu.util.MD5Util;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.shiro.util.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;
@Controller
public class AccountController {
private static final Logger logger = LogManager.getLogger(AccountController.class);
@Autowired
private AccountService accountService;
//转到注册页面
@RequestMapping(value="toregister")
public String toregister(){
return "register";
}
//注册账户
@RequestMapping(value = "register",method = RequestMethod.POST)
public String register(Account account,Model model){
if(accountService.selectAccount(account.getUsername())==null){
account.setCreateat(1L);
account.setCreateby("管理员");
account.setUpdateat(1L);
account.setUpdateby("管理员");
accountService.insert(account);
model.addAttribute("msg","注册成功");
return "login";
}else {
model.addAttribute("msg","该账号已存在");
return "register";
}
}
//转到登录页面
@RequestMapping(value = "tologin")
public String tologin(){
System.out.println("跳转登录");
return "login";
}
//登录
@RequestMapping(value = "login")
public String login(Account account,String username,String password,Model model,HttpServletRequest httpServletRequest,HttpServletResponse httpServletResponse){
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:/a/profession";
} else {
model.addAttribute("error", "账号或密码错误");//如果账号密码错误则提示该消息
return "login";
}
}else{
model.addAttribute("error", "该账号不存在");//如果账号不存在则提示该消息
return "login";
}
} else {
model.addAttribute("error","请先登录");//如果未登录就访问/u/profession则提示该消息
httpServletResponse.sendRedirect(httpServletRequest.getContextPath() + "/login");
return "login";
}
}
**
* 自定义拦截器
*/
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;
}
}
评论