1
0

SysLoginController.java 3.8 KB

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