123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143 |
- package com.kmall.admin.fromcomm.controller;
- import com.google.code.kaptcha.Constants;
- import com.google.code.kaptcha.Producer;
- import com.kmall.admin.entity.CashierEntity;
- import com.kmall.admin.entity.CashierLoginRecordEntity;
- import com.kmall.admin.service.CashierLoginRecordService;
- import com.kmall.admin.service.CashierService;
- import com.kmall.common.annotation.SysLog;
- import com.kmall.common.utils.R;
- import com.kmall.admin.utils.ShiroUtils;
- import com.kmall.manager.manager.redis.JedisUtil;
- import org.apache.shiro.authc.*;
- import org.apache.shiro.crypto.hash.Sha256Hash;
- import org.apache.shiro.subject.Subject;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Controller;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RequestMethod;
- import org.springframework.web.bind.annotation.ResponseBody;
- import javax.imageio.ImageIO;
- import javax.servlet.ServletException;
- import javax.servlet.ServletOutputStream;
- import javax.servlet.http.HttpServletResponse;
- import javax.servlet.http.HttpSession;
- import java.awt.image.BufferedImage;
- import java.io.IOException;
- import java.io.UnsupportedEncodingException;
- import java.net.URLDecoder;
- import java.util.Date;
- import java.util.HashSet;
- import java.util.Set;
- /**
- * 登录相关
- *
- * @author Scott
- * @email
- * @date 2016年11月10日 下午1:15:31
- */
- @Controller
- public class SysLoginController {
- @Autowired
- private Producer producer;
- @Autowired
- private CashierService cashierService;
- @Autowired
- private CashierLoginRecordService cashierLoginRecordService;
- public static int wsTokenExpireTime = 3600 * 24 * 30;
- private final Logger LOGGER = LoggerFactory.getLogger(SysLoginController.class);
- @RequestMapping("captcha.jpg")
- public void captcha(HttpServletResponse response, HttpSession session) throws ServletException, IOException {
- response.setHeader("Cache-Control", "no-store, no-cache");
- response.setContentType("image/jpeg");
- //生成文字验证码
- String text = producer.createText();
- //生成图片验证码
- BufferedImage image = producer.createImage(text);
- //保存到shiro session
- JedisUtil.set(Constants.KAPTCHA_SESSION_KEY, text, wsTokenExpireTime);
- LOGGER.info("生成验证码:"+text);
- ServletOutputStream out = response.getOutputStream();
- ImageIO.write(image, "jpg", out);
- }
- /**
- * 登录
- */
- @SysLog("登录")
- @ResponseBody
- @RequestMapping(value = "/sys/login", method = RequestMethod.POST)
- public R login(String username, String password, String captcha,String machineCode ,HttpSession session) throws IOException {
- String kaptcha = (String) JedisUtil.get(Constants.KAPTCHA_SESSION_KEY);
- LOGGER.info("获取验证码:"+kaptcha);
- System.out.println(kaptcha);
- JedisUtil.del(Constants.KAPTCHA_SESSION_KEY);
- if (!captcha.equalsIgnoreCase(kaptcha)) {
- return R.error("验证码不正确");
- }
- try {
- Subject subject = ShiroUtils.getSubject();
- //sha256加密
- password = new Sha256Hash(password).toHex();
- UsernamePasswordToken token = new UsernamePasswordToken(username, password);
- subject.login(token);
- } catch (UnknownAccountException e) {
- return R.error(e.getMessage());
- } catch (IncorrectCredentialsException e) {
- return R.error(e.getMessage());
- } catch (LockedAccountException e) {
- return R.error(e.getMessage());
- } catch (AuthenticationException e) {
- return R.error("账户验证失败");
- }
- Set<String> permsSet = new HashSet<>();
- Integer storeId = 0;
- if (null != ShiroUtils.getUserEntity()) {
- permsSet = ShiroUtils.getUserEntity().getPermsSet();
- storeId = ShiroUtils.getUserEntity().getStoreId();
- }
- // 判断是否是店员
- if(storeId != null){
- // 是店员,根据机器码查询机器
- CashierEntity cashierEntity = cashierService.queryByMachineCode(machineCode,storeId+"");
- if(cashierEntity == null){
- return R.error("该机器未录入,请联系管理员录入机器,该机器机器码为:"+machineCode);
- }
- // 记录登录记录
- CashierLoginRecordEntity cashierLoginRecordEntity = new CashierLoginRecordEntity();
- cashierLoginRecordEntity.setSallerId(ShiroUtils.getUserEntity().getUsername());
- cashierLoginRecordEntity.setCashierId(cashierEntity.getCashierSn());
- cashierLoginRecordEntity.setShopSn(cashierEntity.getShopSn());
- cashierLoginRecordEntity.setLoginTime(new Date());
- cashierLoginRecordService.save(cashierLoginRecordEntity);
- }
- return R.ok().put("permsSet", permsSet).put("storeId",storeId);
- }
- /**
- * 退出
- */
- @RequestMapping(value = "logout", method = RequestMethod.GET)
- public String logout() {
- ShiroUtils.logout();
- return "redirect:/";
- }
- }
|