liusheng
昨天 459aa78c84cf552ebea6ef056d978c2531d71ac8
ruoyi-framework/src/main/java/com/ruoyi/framework/web/service/SysLoginService.java
@@ -2,7 +2,14 @@
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;
@@ -28,11 +35,15 @@
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
@@ -50,6 +61,21 @@
    @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;
    /**
     * 登录验证
     *
@@ -59,7 +85,7 @@
     * @param uuid     唯一标识
     * @return 结果
     */
    public String login(String username, String password, String code, String uuid, String orgid) {
    public String login(String username, String password, String code, String uuid, String orgid, String campusid) {
        boolean captchaEnabled = configService.selectCaptchaEnabled();
        // 验证码开关
        if (captchaEnabled) {
@@ -68,7 +94,8 @@
        // 用户验证
        Authentication authentication = null;
        try {
            UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(username + "&" + orgid, password);
            if (StringUtils.isEmpty(campusid)) campusid = "1";
            UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(username + "&" + orgid + "&" + campusid, password);
            AuthenticationContextHolder.setContext(authenticationToken);
            // 该方法会去调用UserDetailsServiceImpl.loadUserByUsername
            authentication = authenticationManager.authenticate(authenticationToken);
@@ -90,6 +117,92 @@
        return tokenService.createToken(loginUser);
    }
    /**
     * SSO 单点登录业务处理
     * <p>
     * 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<String, String> headers = new HashMap<>();
        headers.put("app-key", ServiceSLTDHealthcareRecordServiceImpl.APP_KEY);
        Map<String, String> requestParams = new HashMap<>();
        requestParams.put("token", token);
        String reqData = HttpUtil.postFormRequest(sltdPubPath + "/checkSsoTokenId", requestParams, headers, null);
        log.info("【SLTD token 验证】响应结果:{}", reqData);
        if (StringUtils.isEmpty(reqData)) {
            log.error("【SLTD token 验证】响应为空,验证失败");
            return null;
        }
        Map<String, Object> map = JSONObject.parseObject(reqData, Map.class);
        if (ObjectUtils.isEmpty(map) || (Integer) map.get("code") != 200) {
            log.error("【SLTD token 验证】响应码异常,验证失败");
            return null;
        }
        Map<String, Object> data = (Map<String, Object>) map.get("data");
        return (String) data.get("accountNo");
    }
    public String loginByUserName(String userName) {
        SysUser sysUser = userService.selectUserByUserNameAndDeptId(userName);
        log.info("---------sysUser的值为:{}", sysUser);
        if (ObjectUtils.isNotEmpty(sysUser)) {
            // 构建登录用户对象
            LoginUser loginUser = new LoginUser();
            loginUser.setUser(sysUser);
            loginUser.setUserId(sysUser.getUserId());
            // 创建 token
            String token = tokenService.createToken(loginUser);
            // 生成token
            return token;
        }
        return null;
    }
    /**
     * 校验验证码
     *