| | |
| | | import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
|
| | | import org.springframework.security.core.Authentication;
|
| | | import org.springframework.security.core.context.SecurityContextHolder;
|
| | | import org.slf4j.Logger;
|
| | | import org.slf4j.LoggerFactory;
|
| | | import org.springframework.stereotype.Component;
|
| | | import com.ruoyi.common.constant.CacheConstants;
|
| | | import com.ruoyi.common.constant.Constants;
|
| | | import com.ruoyi.common.constant.UserConstants;
|
| | | import com.ruoyi.common.enums.UserStatus;
|
| | | import com.ruoyi.common.exception.ServiceException;
|
| | | import com.ruoyi.common.exception.user.BlackListException;
|
| | | import com.ruoyi.common.exception.user.CaptchaException;
|
| | |
| | | @Component
|
| | | public class SysLoginService
|
| | | {
|
| | | private static final Logger log = LoggerFactory.getLogger(SysLoginService.class);
|
| | |
|
| | | @Autowired
|
| | | private TokenService tokenService;
|
| | |
|
| | | @Autowired
|
| | | private UserDetailsServiceImpl userDetailsService;
|
| | |
|
| | | @Resource
|
| | | private AuthenticationManager authenticationManager;
|
| | |
| | | return tokenService.createToken(loginUser);
|
| | | }
|
| | |
|
| | | /**
|
| | | * 浪潮门户 SSO 免密登录
|
| | | * <p>
|
| | | * 背景:浪潮 OSS 通过 /tide/tideLogin 把 code 换成 token,再取到 tenant 信息后,
|
| | | * 原本调用 login(username, password, code, uuid) 用 tenantAccount + tenantPassword
|
| | | * 做本地登录。但 tenantPassword 是浪潮系统详情里单独配置的值,与本地 sys_user.password
|
| | | * 一旦不一致(浪潮侧改密码、本地重置密码、账号由 applicationOrdering 创建后改过密码等),
|
| | | * 就会报「租户账号[xxx]在系统中登录失败(用户不存在/密码错误)」,导致门户整条登录链路断掉。
|
| | | * <p>
|
| | | * 鉴权实际上在浪潮侧已经完成(能拿到有效 token 和 userInfo 就说明身份可信),
|
| | | * 本地无需再次校验密码。这里直接按用户名加载用户并签发本系统 token。
|
| | | * <p>
|
| | | * 仍然保留的校验:用户不存在、已删除、已停用 —— 这三种情况拒绝登录。
|
| | | *
|
| | | * @param username 浪潮返回的租户账号(tenantAccount)
|
| | | * @return 本系统 token
|
| | | */
|
| | | public String ssoLogin(String username)
|
| | | {
|
| | | SysUser user = userService.selectUserByUserName(username);
|
| | | if (StringUtils.isNull(user))
|
| | | {
|
| | | log.info("SSO 登录:用户 {} 不存在.", username);
|
| | | AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL,
|
| | | MessageUtils.message("user.not.exists")));
|
| | | throw new UserNotExistsException();
|
| | | }
|
| | | if (UserStatus.DELETED.getCode().equals(user.getDelFlag()))
|
| | | {
|
| | | log.info("SSO 登录:用户 {} 已被删除.", username);
|
| | | AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL,
|
| | | MessageUtils.message("user.password.delete")));
|
| | | throw new ServiceException(MessageUtils.message("user.password.delete"));
|
| | | }
|
| | | if (UserStatus.DISABLE.getCode().equals(user.getStatus()))
|
| | | {
|
| | | log.info("SSO 登录:用户 {} 已被停用.", username);
|
| | | AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL,
|
| | | MessageUtils.message("user.blocked")));
|
| | | throw new ServiceException(MessageUtils.message("user.blocked"));
|
| | | }
|
| | | // 免密:直接构造 LoginUser(不调用 passwordService.validate)
|
| | | LoginUser loginUser = (LoginUser) userDetailsService.createLoginUser(user);
|
| | | AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_SUCCESS,
|
| | | "浪潮门户 SSO 登录"));
|
| | | recordLoginInfo(user.getUserId());
|
| | | return tokenService.createToken(loginUser);
|
| | | }
|
| | |
|
| | | }
|