SysLoginController.java 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. package com.kmall.admin.fromcomm.controller;
  2. import com.google.code.kaptcha.Constants;
  3. import com.google.code.kaptcha.Producer;
  4. import com.kmall.admin.entity.CashierEntity;
  5. import com.kmall.admin.entity.CashierLoginRecordEntity;
  6. import com.kmall.admin.service.CashierLoginRecordService;
  7. import com.kmall.admin.service.CashierService;
  8. import com.kmall.common.annotation.SysLog;
  9. import com.kmall.common.utils.R;
  10. import com.kmall.admin.utils.ShiroUtils;
  11. import com.kmall.manager.manager.redis.JedisUtil;
  12. import org.apache.shiro.authc.*;
  13. import org.apache.shiro.crypto.hash.Sha256Hash;
  14. import org.apache.shiro.subject.Subject;
  15. import org.slf4j.Logger;
  16. import org.slf4j.LoggerFactory;
  17. import org.springframework.beans.factory.annotation.Autowired;
  18. import org.springframework.stereotype.Controller;
  19. import org.springframework.web.bind.annotation.RequestMapping;
  20. import org.springframework.web.bind.annotation.RequestMethod;
  21. import org.springframework.web.bind.annotation.ResponseBody;
  22. import javax.imageio.ImageIO;
  23. import javax.servlet.ServletException;
  24. import javax.servlet.ServletOutputStream;
  25. import javax.servlet.http.HttpServletResponse;
  26. import javax.servlet.http.HttpSession;
  27. import java.awt.image.BufferedImage;
  28. import java.io.IOException;
  29. import java.util.Date;
  30. import java.util.HashSet;
  31. import java.util.Set;
  32. /**
  33. * 登录相关
  34. *
  35. * @author Scott
  36. * @email
  37. * @date 2016年11月10日 下午1:15:31
  38. */
  39. @Controller
  40. public class SysLoginController {
  41. @Autowired
  42. private Producer producer;
  43. @Autowired
  44. private CashierService cashierService;
  45. @Autowired
  46. private CashierLoginRecordService cashierLoginRecordService;
  47. public static int wsTokenExpireTime = 3600 * 24 * 30;
  48. private final Logger LOGGER = LoggerFactory.getLogger(SysLoginController.class);
  49. @RequestMapping("captcha.jpg")
  50. public void captcha(HttpServletResponse response, HttpSession session) throws ServletException, IOException {
  51. response.setHeader("Cache-Control", "no-store, no-cache");
  52. response.setContentType("image/jpeg");
  53. //生成文字验证码
  54. String text = producer.createText();
  55. //生成图片验证码
  56. BufferedImage image = producer.createImage(text);
  57. //保存到shiro session
  58. JedisUtil.set(Constants.KAPTCHA_SESSION_KEY, text, wsTokenExpireTime);
  59. LOGGER.info("生成验证码:"+text);
  60. ServletOutputStream out = response.getOutputStream();
  61. ImageIO.write(image, "jpg", out);
  62. }
  63. /**
  64. * 登录
  65. */
  66. @SysLog("登录")
  67. @ResponseBody
  68. @RequestMapping(value = "/sys/login", method = RequestMethod.POST)
  69. public R login(String username, String password, String captcha,String machineCode ,HttpSession session) throws IOException {
  70. String kaptcha = (String) JedisUtil.get(Constants.KAPTCHA_SESSION_KEY);
  71. LOGGER.info("获取验证码:"+kaptcha);
  72. System.out.println(kaptcha);
  73. JedisUtil.del(Constants.KAPTCHA_SESSION_KEY);
  74. if (!captcha.equalsIgnoreCase(kaptcha)) {
  75. return R.error("验证码不正确");
  76. }
  77. try {
  78. Subject subject = ShiroUtils.getSubject();
  79. //sha256加密
  80. password = new Sha256Hash(password).toHex();
  81. UsernamePasswordToken token = new UsernamePasswordToken(username, password);
  82. subject.login(token);
  83. } catch (UnknownAccountException e) {
  84. return R.error(e.getMessage());
  85. } catch (IncorrectCredentialsException e) {
  86. return R.error(e.getMessage());
  87. } catch (LockedAccountException e) {
  88. return R.error(e.getMessage());
  89. } catch (AuthenticationException e) {
  90. return R.error("账户验证失败");
  91. }
  92. Set<String> permsSet = new HashSet<>();
  93. Integer storeId = 0;
  94. if (null != ShiroUtils.getUserEntity()) {
  95. permsSet = ShiroUtils.getUserEntity().getPermsSet();
  96. storeId = ShiroUtils.getUserEntity().getStoreId();
  97. }
  98. // 判断是否是店员
  99. if(storeId != null){
  100. // 是店员,根据机器码查询机器
  101. CashierEntity cashierEntity = cashierService.queryByMachineCode(machineCode);
  102. // 记录登录记录
  103. CashierLoginRecordEntity cashierLoginRecordEntity = new CashierLoginRecordEntity();
  104. cashierLoginRecordEntity.setSallerId(ShiroUtils.getUserEntity().getUsername());
  105. cashierLoginRecordEntity.setCashierId(cashierEntity.getCashierSn());
  106. cashierLoginRecordEntity.setShopSn(cashierEntity.getShopSn());
  107. cashierLoginRecordEntity.setLoginTime(new Date());
  108. cashierLoginRecordService.save(cashierLoginRecordEntity);
  109. }
  110. return R.ok().put("permsSet", permsSet).put("storeId",storeId);
  111. }
  112. /**
  113. * 退出
  114. */
  115. @RequestMapping(value = "logout", method = RequestMethod.GET)
  116. public String logout() {
  117. ShiroUtils.logout();
  118. return "redirect:/";
  119. }
  120. }