| | |
| | | package com.ruoyi.framework.security.service;
|
| | |
|
| | | import javax.annotation.Resource;
|
| | |
|
| | | import com.ruoyi.framework.web.domain.AjaxResult;
|
| | | 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.security.core.context.SecurityContextHolder;
|
| | | import org.springframework.stereotype.Component;
|
| | | import com.ruoyi.common.constant.CacheConstants;
|
| | | import com.ruoyi.common.constant.Constants;
|
| | |
| | | import com.ruoyi.project.system.domain.SysUser;
|
| | | import com.ruoyi.project.system.service.ISysConfigService;
|
| | | import com.ruoyi.project.system.service.ISysUserService;
|
| | |
|
| | | import java.util.Collections;
|
| | | import java.util.HashMap;
|
| | | import java.util.Map;
|
| | |
|
| | | /**
|
| | | * 登录校验方法
|
| | |
| | | public String login(String username, String password, String code, String uuid)
|
| | | {
|
| | | // 验证码校验
|
| | | validateCaptcha(username, code, uuid);
|
| | | // validateCaptcha(username, code, uuid);
|
| | | // 登录前置校验
|
| | | loginPreCheck(username, password);
|
| | | // 用户验证
|
| | |
| | | sysUser.setLoginDate(DateUtils.getNowDate());
|
| | | userService.updateUserProfile(sysUser);
|
| | | }
|
| | |
|
| | | public Long loginCheck(String username, String password){
|
| | | loginPreCheck(username, password);
|
| | | // 用户验证
|
| | | Authentication authentication = null;
|
| | | try
|
| | | {
|
| | | UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(username, password);
|
| | | AuthenticationContextHolder.setContext(authenticationToken);
|
| | | // 该方法会去调用UserDetailsServiceImpl.loadUserByUsername
|
| | | authentication = authenticationManager.authenticate(authenticationToken);
|
| | | }
|
| | | 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 ServiceException(e.getMessage());
|
| | | }
|
| | | }
|
| | | finally
|
| | | {
|
| | | AuthenticationContextHolder.clearContext();
|
| | | }
|
| | | LoginUser loginUser = (LoginUser) authentication.getPrincipal();
|
| | | return loginUser.getUserId();
|
| | | }
|
| | |
|
| | | /**
|
| | | * 登录验证
|
| | | *
|
| | | * @param username 用户名
|
| | | * @param password 密码
|
| | | * @param factoryId 公司ID
|
| | | * @return 结果
|
| | | */
|
| | | public String loginCheckFactory(String username, String password, Long factoryId)
|
| | | {
|
| | | // 登录前置校验
|
| | | loginPreCheck(username, password);
|
| | | // 用户验证
|
| | | Authentication authentication = null;
|
| | | try
|
| | | {
|
| | | UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(username, password);
|
| | | // 扩展细节:将 companyId 放入 details(可选方式)
|
| | | // 使用 Map 存储额外信息
|
| | | Map<String, Object> details = new HashMap<>();
|
| | | details.put("factoryId", factoryId);
|
| | | authenticationToken.setDetails(details);
|
| | | SecurityContextHolder.getContext().setAuthentication(authenticationToken);
|
| | | AuthenticationContextHolder.setContext(authenticationToken);
|
| | | // 该方法会去调用UserDetailsServiceImpl.loadUserByUsername
|
| | | authentication = authenticationManager.authenticate(authenticationToken);
|
| | | }
|
| | | 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 ServiceException(e.getMessage());
|
| | | }
|
| | | }
|
| | | finally
|
| | | {
|
| | | AuthenticationContextHolder.clearContext();
|
| | | }
|
| | | AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_SUCCESS, MessageUtils.message("user.login.success")));
|
| | | LoginUser loginUser = (LoginUser) authentication.getPrincipal();
|
| | | recordLoginInfo(loginUser.getUserId());
|
| | | // 生成token
|
| | | return tokenService.createToken(loginUser);
|
| | | }
|
| | |
|
| | | }
|