| package com.doumee.service.system.impl; | 
|   | 
| import com.doumee.biz.system.SystemDictDataBiz; | 
| import com.doumee.core.constants.Constants; | 
| import com.doumee.core.constants.ResponseStatus; | 
| import com.doumee.core.exception.BusinessException; | 
| import com.doumee.core.model.LoginUserInfo; | 
| import com.doumee.core.utils.DateUtil; | 
| import com.doumee.core.utils.Utils; | 
| import com.doumee.dao.system.dto.LoginDTO; | 
| import com.doumee.dao.system.dto.WebLoginDTO; | 
| import com.doumee.dao.system.model.SystemLoginLog; | 
| import com.doumee.dao.system.model.SystemUser; | 
| import com.doumee.service.common.CaptchaService; | 
| import com.doumee.service.system.SystemLoginLogService; | 
| import com.doumee.service.system.SystemLoginService; | 
| import com.doumee.service.system.SystemUserService; | 
| import lombok.extern.slf4j.Slf4j; | 
| import org.apache.commons.lang3.StringUtils; | 
| import org.apache.shiro.SecurityUtils; | 
| import org.apache.shiro.authc.AuthenticationException; | 
| import org.apache.shiro.authc.UsernamePasswordToken; | 
| import org.apache.shiro.subject.Subject; | 
| import org.springframework.beans.factory.annotation.Autowired; | 
| import org.springframework.beans.factory.annotation.Value; | 
| import org.springframework.context.annotation.Lazy; | 
| import org.springframework.stereotype.Service; | 
|   | 
| import javax.servlet.http.HttpServletRequest; | 
| import java.util.Date; | 
|   | 
| @Slf4j | 
| @Service | 
| public class SystemLoginServiceImpl implements SystemLoginService { | 
|   | 
|     @Value("${project.version}") | 
|     private String systemVersion; | 
|   | 
|     @Autowired | 
|     private CaptchaService captchaService; | 
|   | 
|     @Autowired | 
|     private SystemUserService systemUserService; | 
|   | 
|     @Autowired | 
|     private SystemLoginLogService systemLoginLogService; | 
|   | 
|   | 
|     @Lazy | 
|     @Autowired | 
|     private SystemDictDataBiz systemDictDataBiz; | 
|   | 
|   | 
|     @Value("${debug_model}") | 
|     private Boolean debugModel; | 
|   | 
|     @Override | 
|     public String loginByPassword(LoginDTO dto,  HttpServletRequest request) { | 
|         return doLogin(dto,true,request); | 
|     } | 
|     @Override | 
|     public String loginByPasswordWeb(WebLoginDTO dto, HttpServletRequest request) { | 
|         LoginDTO d = new LoginDTO(); | 
|         d.setPassword(dto.getPassword()); | 
|         d.setUsername(dto.getUsername()); | 
|   | 
|         return doLogin(d,false,request); | 
|   | 
|     } | 
|   | 
|     public String doLogin(LoginDTO dto,boolean needCheckCode, HttpServletRequest request) { | 
|         if(StringUtils.isBlank(dto.getUsername()) | 
|                 ||StringUtils.isBlank(dto.getPassword()) | 
|                 ||(needCheckCode &&StringUtils.isBlank(dto.getCode()))){ | 
|             throw new BusinessException(ResponseStatus.BAD_REQUEST); | 
|         } | 
|   | 
|         SystemLoginLog loginLog = new SystemLoginLog(); | 
|         loginLog.setLoginUsername(dto.getUsername()); | 
|         loginLog.setLoginTime(new Date()); | 
|         loginLog.setSystemVersion(systemVersion); | 
|         loginLog.setIp(Utils.User_Client.getIP(request)); | 
| //        loginLog.setLocation(Utils.Location.getLocationString(loginLog.getIp())); | 
|         loginLog.setPlatform(Utils.User_Client.getPlatform(request)); | 
|         loginLog.setClientInfo(Utils.User_Client.getBrowser(request)); | 
|         loginLog.setOsInfo(Utils.User_Client.getOS(request)); | 
|         loginLog.setServerIp(Utils.Server.getIP()); | 
|         if(needCheckCode){ | 
|             // 校验验证码 | 
|             if(!debugModel) { | 
|                 try { | 
|                     captchaService.check(dto.getUuid(), dto.getCode()); | 
|                 } catch (Exception e) { | 
|                     log.error(e.getMessage(), e); | 
|                     loginLog.setReason(e.getMessage().length() > 200 ? (e.getMessage().substring(0, 190) + "...") : e.getMessage()); | 
|                     loginLog.setSuccess(Constants.ZERO); | 
|                     systemLoginLogService.create(loginLog); | 
|                     throw e; | 
|                 } | 
|             } | 
|         } | 
|   | 
|         // 校验用户名和密码 | 
|         Subject subject = SecurityUtils.getSubject(); | 
|         UsernamePasswordToken token = new UsernamePasswordToken(dto.getUsername(), dto.getPassword()); | 
|         try { | 
|             subject.login(token); | 
|             SystemUser u = new SystemUser(); | 
|             u.setLastLogin(DateUtil.getCurrentDate()); | 
|             u.setId(loginLog.getUserId()); | 
|             systemUserService.updateLoginDate(u); | 
|   | 
|             loginLog.setUserId(((LoginUserInfo)subject.getPrincipal()).getId()); | 
|             loginLog.setSuccess(Constants.ZERO); | 
|             systemLoginLogService.create(loginLog); | 
|   | 
|             return (String)subject.getSession().getId(); | 
|         } catch (AuthenticationException e) { | 
|             log.error(ResponseStatus.ACCOUNT_INCORRECT.getMessage(), e); | 
|             loginLog.setReason(e.getMessage().length() > 200 ? (e.getMessage().substring(0, 190) + "...") : e.getMessage()); | 
|             loginLog.setSuccess(Constants.ONE); | 
|             systemLoginLogService.create(loginLog); | 
|             if (e.getCause() instanceof BusinessException) { | 
|                 throw new BusinessException(ResponseStatus.ACCOUNT_INCORRECT.getCode(), e.getCause().getMessage()); | 
|             } | 
|             throw new BusinessException(ResponseStatus.ACCOUNT_INCORRECT.getCode(), "对不起,账号或密码错误!"); | 
|         } | 
|     } | 
|   | 
| } |