package com.ruoyi.framework.web.service; import javax.annotation.Resource; import com.alibaba.fastjson2.JSONObject; import com.ruoyi.common.utils.HttpUtil; import com.ruoyi.common.utils.RSAPublicKeyExample; import com.smartor.service.impl.ServiceSLTDHealthcareRecordServiceImpl; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.ObjectUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; 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 com.ruoyi.common.constant.CacheConstants; import com.ruoyi.common.constant.Constants; import com.ruoyi.common.core.domain.entity.SysUser; import com.ruoyi.common.core.domain.model.LoginUser; import com.ruoyi.common.core.redis.RedisCache; import com.ruoyi.common.exception.ServiceException; import com.ruoyi.common.exception.user.CaptchaException; import com.ruoyi.common.exception.user.CaptchaExpireException; import com.ruoyi.common.exception.user.UserPasswordNotMatchException; import com.ruoyi.common.utils.DateUtils; import com.ruoyi.common.utils.MessageUtils; import com.ruoyi.common.utils.ServletUtils; import com.ruoyi.common.utils.StringUtils; import com.ruoyi.common.utils.ip.IpUtils; import com.ruoyi.framework.manager.AsyncManager; import com.ruoyi.framework.manager.factory.AsyncFactory; import com.ruoyi.framework.security.context.AuthenticationContextHolder; import com.ruoyi.system.service.ISysConfigService; import com.ruoyi.system.service.ISysUserService; import java.util.HashMap; import java.util.Map; /** * 登录校验方法 * * @author ruoyi */ @Slf4j @Component public class SysLoginService { @Autowired private TokenService tokenService; @Resource private AuthenticationManager authenticationManager; @Autowired private RedisCache redisCache; @Autowired private ISysUserService userService; @Autowired private ISysConfigService configService; @Autowired private RSAPublicKeyExample rsaPublicKeyExample; @Value("${pri_key}") private String priKey; @Value("${isEncryp}") private Integer isEncryp; @Value("${sltd_pub_path}") private String sltdPubPath; @Value("${spring.profiles.active}") private String active; /** * 登录验证 * * @param username 用户名 * @param password 密码 * @param code 验证码 * @param uuid 唯一标识 * @return 结果 */ public String login(String username, String password, String code, String uuid, String orgid, String campusid) { boolean captchaEnabled = configService.selectCaptchaEnabled(); // 验证码开关 if (captchaEnabled) { validateCaptcha(username, code, uuid); } // 用户验证 Authentication authentication = null; try { if (StringUtils.isEmpty(campusid)) campusid = "1"; UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(username + "&" + orgid + "&" + campusid, 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(); } 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); } /** * SSO 单点登录业务处理 *
* 1. 如果是 sltd 环境,先通过 SSO token 换取员工账号
* 2. RSA 解密 userName(若开启加密)
* 3. 根据 userName + orgid + deptId + campusid 生成登录 token
*
* @param userName 用户名(可能为空,如果 sltd 模式则从 token 中获取)
* @param orgid 组织机构ID
* @param deptId 部门ID
* @param campusid 校区 ID
* @param token SLTD SSO token(仅 sltd 环境下使用)
* @return 登录成功后的 JWT token,失败返回 null
*/
public String ssoLogin(String userName, String orgid, String deptId, String campusid, String token) {
// sltd 环境:通过 SSO token 获取员工账号
if ("sltd".equals(active)) {
userName = resolveUserNameBySltdToken(token);
if (userName == null) {
return null;
}
}
log.info("【SSO登录】userName={}", userName);
if (StringUtils.isEmpty(userName) || StringUtils.isEmpty(orgid)) {
log.error("【SSO登录】用户名或组织机构不能为空");
return null;
}
// RSA 解密用户名
if (isEncryp != null && isEncryp == 1) {
userName = rsaPublicKeyExample.decryptedData(userName, priKey);
}
if (StringUtils.isEmpty(deptId)) deptId = "null";
if (StringUtils.isEmpty(campusid)) campusid = "null";
return loginByUserName(userName + "&" + orgid + "&" + deptId + "&" + campusid);
}
/**
* 调用省立同德接口,通过 SSO token 获取员工账号
*
* @param token SLTD SSO token
* @return 员工账号,验证失败返回 null
*/
private String resolveUserNameBySltdToken(String token) {
Map