| | |
| | | |
| | | 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 com.ruoyi.system.service.ISysConfigService; |
| | | import com.ruoyi.system.service.ISysUserService; |
| | | |
| | | import java.util.HashMap; |
| | | import java.util.Map; |
| | | |
| | | /** |
| | | * 登录校验方法 |
| | | * |
| | |
| | | @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 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) { |
| | |
| | | // 用户验证 |
| | | 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); |
| | |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 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); |