123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146 |
- package com.ydd.system.service;
- import com.ydd.common.config.EnvConfig;
- import com.ydd.common.constant.Constants;
- import com.ydd.common.core.domain.entity.SysUser;
- import com.ydd.common.core.domain.model.LoginUser;
- import com.ydd.common.core.redis.RedisCache;
- import com.ydd.common.exception.CustomException;
- import com.ydd.common.exception.user.CaptchaException;
- import com.ydd.common.exception.user.CaptchaExpireException;
- import com.ydd.common.exception.user.UserNameDisableException;
- import com.ydd.common.exception.user.UserPasswordNotMatchException;
- import com.ydd.common.utils.MessageUtils;
- import com.ydd.ecloud.core.enums.StatusEnum;
- import com.ydd.ecloud.core.utils.HttpContextUtils;
- import com.ydd.framework.web.service.TokenService;
- import com.ydd.system.manager.AsyncManager;
- import com.ydd.system.manager.factory.AsyncFactory;
- import org.apache.commons.collections4.CollectionUtils;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.security.authentication.AuthenticationManager;
- import org.springframework.security.authentication.BadCredentialsException;
- import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
- import org.springframework.security.core.Authentication;
- import org.springframework.stereotype.Component;
- import javax.annotation.Resource;
- import java.util.List;
- /**
- * 登录校验方法
- *
- * @author douya
- */
- @Component
- public class SysLoginService
- {
- @Autowired
- private TokenService tokenService;
- @Resource
- private AuthenticationManager authenticationManager;
- @Autowired
- private RedisCache redisCache;
- @Resource
- private ISysUserService iSysUserService;
- @Resource
- private ISysRoleService iSysRoleService;
- @Resource
- private EnvConfig envConfig;
- /**
- * 登录验证
- *
- * @param username 用户名
- * @param password 密码
- * @param code 验证码
- * @param uuid 唯一标识
- * @return 结果
- */
- public String login(String username, String password, String code, String uuid)
- {
- String verifyKey = Constants.CAPTCHA_CODE_KEY + uuid;
- String captcha = redisCache.getCacheObject(verifyKey);
- redisCache.deleteObject(verifyKey);
- if (captcha == null)
- {
- AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("user.jcaptcha.expire")));
- throw new CaptchaExpireException();
- }
- if (!code.equalsIgnoreCase(captcha))
- {
- AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("user.jcaptcha.error")));
- throw new CaptchaException();
- }
- // 代理商禁用不能登录
- Integer status = iSysUserService.selectAgentByMoblie(username);
- if (status != null) {
- if (status.equals(StatusEnum.off.value)) {
- AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("user.username.disable")));
- throw new UserNameDisableException();
- }
- }
- // 登录域名校验
- this.checkDomain(username);
- // 用户验证
- Authentication authentication = null;
- try
- {
- // 该方法会去调用UserDetailsServiceImpl.loadUserByUsername
- authentication = authenticationManager
- .authenticate(new UsernamePasswordAuthenticationToken(username, password));
- }
- catch (Exception e)
- {
- if (e instanceof BadCredentialsException)
- {
- AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("user.password.not.match")));
- throw new UserPasswordNotMatchException();
- }
- else
- {
- AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL, e.getMessage()));
- throw new CustomException(e.getMessage());
- }
- }
- AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_SUCCESS, MessageUtils.message("user.login.success")));
- LoginUser loginUser = (LoginUser) authentication.getPrincipal();
- // 生成token
- return tokenService.createToken(loginUser);
- }
- private void checkDomain(String username) {
- String serverName = HttpContextUtils.getHttpServletRequest().getServerName();
- if (serverName != null && !serverName.contains("localhost")) {
- SysUser sysUser = iSysUserService.selectUserByUserName(username);
- if (sysUser != null) {
- List<Integer> roleIdList = iSysRoleService.selectRoleListByUserId(sysUser.getUserId());
- if (CollectionUtils.isNotEmpty(roleIdList)) {
- Long roleId = Long.parseLong(roleIdList.get(0).toString());
- // 外部角色
- if (Constants.EXTERNAL_ROLE_IDS.contains(roleId)) {
- if (!serverName.contains(Constants.getExternalDomain(envConfig.getProfiles()))) {
- AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL, "用户不存在!"));
- throw new CustomException("用户不存在!");
- }
- } else {
- // 内部角色
- if (!serverName.contains(Constants.getInSideDomain(envConfig.getProfiles()))) {
- AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL, "用户不存在!"));
- throw new CustomException("用户不存在!");
- }
- }
- }
- }
- }
- }
- }
|