发表于: 2020-08-13 23:26:33
1 1417
shiro搞不定 任务9再战
重写task5
@Controller
public class UserController {
@Autowired
UserService userService;
private static final Logger log = LogManager.getLogger(UserController.class);
/**
* 注册模块
*
* @return
*/
@RequestMapping(value = "/a/register/0", method = RequestMethod.GET)
public String registered() {
return "register";
}
/**
* 登录模块
*
* @return
*/
@RequestMapping(value = "/a/login/0")
public String login() {
return "login";
}
/**
* 注册检测模块
*
* @param user
* @param name
* @return
*/
@RequestMapping(value = "/a/register/1")
public String insert(User user, String name, HttpServletResponse response) throws Exception {
log.info("user========================" + user);
// 对传入参数进行判空
if (!ObjectUtils.isEmpty(user.getName()) && !ObjectUtils.isEmpty(user.getPassword())) {
// 注册时,先根据用户名查数据库,如果查不出则插入,否则返回注册页面
List<User> userName = userService.selectByName(user.getName());
log.info("userName====================" + userName);
if (ObjectUtils.isEmpty(userName)) {
// 插入时,使用MD5加密再加盐
user.setPassword(Md5Util.getMd5(user.getPassword()+user.getId()));
log.info("用户名和加密后的密码=================" + user);
int row = userService.insert(user);
log.info("是否插入================" + row);
log.info("userId===============" + user.getId());
User dbUser = userService.selectByCondition(user.getName(), user.getPassword());
// 发一个token
log.info(dbUser);
String token =
DESUtil.encrypt(dbUser.getId()+"|"+
System.currentTimeMillis() + "|" + user.getName() );
log.info("token=================" + token);
Cookie cookie = new Cookie("token", token);
cookie.setMaxAge(0);
log.info("tokenName===================" + cookie.getName());
log.info("tokenValue==================" + cookie.getValue());
response.addCookie(cookie);
log.info("确认数据======================");
return "login";
}
log.info("用户名已存在=================");
return "redirect:/a/register/0";
}
log.info("用户名密码为空================");
return "redirect:/a/register/0";
}
/**
* 登录检测模块
*
* @param user
* @param name
* @param password
* @param response
* @return
*/
@RequestMapping(value = "/a/login/1", method = RequestMethod.POST)
public String checkLogin(User user, String name, String password, HttpServletRequest request,HttpServletResponse response) throws Exception {
if (!ObjectUtils.isEmpty(user.getName()) && !ObjectUtils.isEmpty(user.getPassword()+user.getId())) {
// 密码加密,对比数据
user.setPassword(Md5Util.getMd5(user.getPassword()+user.getId()));
log.info(user);
User dbUser = (User) userService.selectByCondition(user.getName(), user.getPassword());
if (!ObjectUtils.isEmpty(dbUser)) {
log.info("查出的数据==============" + dbUser);
// 发一个token,token由用户名,id,登录时间组成
// DES加密
String token =
DESUtil.encrypt( dbUser.getId()+"|"+
System.currentTimeMillis() + "|" + user.getName() );
log.info("token===============" + token);
Cookie cookie = new Cookie("token", token);
cookie.setMaxAge(30 * 60);
cookie.setPath("/");
log.info("tokenName=========" + cookie.getName());
log.info("tokenValue==========" + cookie.getValue());
response.addCookie(cookie);
log.info(cookie);
log.info("确认保存=============");
return "home";
} else {
log.info("无此用户,请注册============");
return "register";
}
}
log.info("数据为空==================");
return "login";
}
/**
* 注销当前账号模块
*
* @param request
* @param response
* @return
*/
@RequestMapping(value = "/a/u/logout", method = RequestMethod.GET)
public String loginOut(HttpServletRequest request, HttpServletResponse response) {
Cookie[] cookies = request.getCookies();
//这里就是把cookie中的所有数据都来出来比对
for (Cookie cookie : cookies) {
//当我们找到了要注销的账号的信息,就把这个账号的信息进行修改
if ("token".equals(cookie.getName())) {
//token的值设为null
cookie = new Cookie("token", null);
//将cookie时间设置为0
cookie.setMaxAge(0);
cookie.setPath("/");
//然后把这个cookie加到所有的cookie中存储
response.addCookie(cookie);
//重定向到登录页面
return "redirect:/a/login/0";
}
}
//重定向到登录页面
return "redirect:/a/login/0";
}
}
今日问题暂无
评论