SysLoginController.java 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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.io.UnsupportedEncodingException;
  30. import java.net.URLDecoder;
  31. import java.util.Date;
  32. import java.util.HashSet;
  33. import java.util.Set;
  34. /**
  35. * 登录相关
  36. *
  37. * @author Scott
  38. * @email
  39. * @date 2016年11月10日 下午1:15:31
  40. */
  41. @Controller
  42. public class SysLoginController {
  43. @Autowired
  44. private Producer producer;
  45. @Autowired
  46. private CashierService cashierService;
  47. @Autowired
  48. private CashierLoginRecordService cashierLoginRecordService;
  49. public static int wsTokenExpireTime = 3600 * 24 * 30;
  50. private final Logger LOGGER = LoggerFactory.getLogger(SysLoginController.class);
  51. @RequestMapping("captcha.jpg")
  52. public void captcha(HttpServletResponse response, HttpSession session) throws ServletException, IOException {
  53. response.setHeader("Cache-Control", "no-store, no-cache");
  54. response.setContentType("image/jpeg");
  55. //生成文字验证码
  56. String text = producer.createText();
  57. //生成图片验证码
  58. BufferedImage image = producer.createImage(text);
  59. //保存到shiro session
  60. JedisUtil.set(Constants.KAPTCHA_SESSION_KEY, text, wsTokenExpireTime);
  61. LOGGER.info("生成验证码:"+text);
  62. ServletOutputStream out = response.getOutputStream();
  63. ImageIO.write(image, "jpg", out);
  64. }
  65. /**
  66. * 登录
  67. */
  68. @SysLog("登录")
  69. @ResponseBody
  70. @RequestMapping(value = "/sys/login", method = RequestMethod.POST)
  71. public R login(String username, String password, String captcha,String machineCode ,HttpSession session) throws IOException {
  72. String kaptcha = (String) JedisUtil.get(Constants.KAPTCHA_SESSION_KEY);
  73. LOGGER.info("获取验证码:"+kaptcha);
  74. System.out.println(kaptcha);
  75. JedisUtil.del(Constants.KAPTCHA_SESSION_KEY);
  76. if (!captcha.equalsIgnoreCase(kaptcha)) {
  77. return R.error("验证码不正确");
  78. }
  79. try {
  80. Subject subject = ShiroUtils.getSubject();
  81. //sha256加密
  82. password = new Sha256Hash(password).toHex();
  83. UsernamePasswordToken token = new UsernamePasswordToken(username, password);
  84. subject.login(token);
  85. } catch (UnknownAccountException e) {
  86. return R.error(e.getMessage());
  87. } catch (IncorrectCredentialsException e) {
  88. return R.error(e.getMessage());
  89. } catch (LockedAccountException e) {
  90. return R.error(e.getMessage());
  91. } catch (AuthenticationException e) {
  92. return R.error("账户验证失败");
  93. }
  94. Set<String> permsSet = new HashSet<>();
  95. Integer storeId = 0;
  96. if (null != ShiroUtils.getUserEntity()) {
  97. permsSet = ShiroUtils.getUserEntity().getPermsSet();
  98. storeId = ShiroUtils.getUserEntity().getStoreId();
  99. }
  100. // 判断是否是店员
  101. if(storeId != null){
  102. // 是店员,根据机器码查询机器
  103. CashierEntity cashierEntity = cashierService.queryByMachineCode(machineCode,storeId+"");
  104. if(cashierEntity == null){
  105. return R.error("该机器未录入,请联系管理员录入机器,该机器机器码为:"+machineCode);
  106. }
  107. // 记录登录记录
  108. CashierLoginRecordEntity cashierLoginRecordEntity = new CashierLoginRecordEntity();
  109. cashierLoginRecordEntity.setSallerId(ShiroUtils.getUserEntity().getUsername());
  110. cashierLoginRecordEntity.setCashierId(cashierEntity.getCashierSn());
  111. cashierLoginRecordEntity.setShopSn(cashierEntity.getShopSn());
  112. cashierLoginRecordEntity.setLoginTime(new Date());
  113. cashierLoginRecordService.save(cashierLoginRecordEntity);
  114. }
  115. return R.ok().put("permsSet", permsSet).put("storeId",storeId);
  116. }
  117. /**
  118. * 退出
  119. */
  120. @RequestMapping(value = "logout", method = RequestMethod.GET)
  121. public String logout() {
  122. ShiroUtils.logout();
  123. return "redirect:/";
  124. }
  125. }