| | |
| | | @ApiOperation(value = "登录") |
| | | @PostMapping("/tideLogin") |
| | | public AjaxResult login(@RequestBody TidePojo tidePojo) { |
| | | String token = TideUtils.getToken(tidePojo.getCode()); |
| | | JSONObject userInfo = TideUtils.getUserInfo(token); |
| | | JSONObject tenant = JSONObject.parseObject(userInfo.get("tenant").toString()); |
| | | if (tidePojo == null || ObjectUtils.isEmpty(tidePojo.getCode())) { |
| | | return AjaxResult.error("缺少浪潮授权码(code),请从浪潮门户重新进入系统"); |
| | | } |
| | | // 浪潮门户下发的授权码可能带固定后缀,做一次兼容处理 |
| | | String code = String.valueOf(tidePojo.getCode()).trim(); |
| | | if (code.endsWith("suanwangcode")) { |
| | | code = code.substring(0, code.length() - "suanwangcode".length()); |
| | | } |
| | | String token; |
| | | try { |
| | | token = TideUtils.getToken(code); |
| | | } catch (Exception e) { |
| | | log.error("[浪潮SSO] 换取token异常, code={}", code, e); |
| | | return AjaxResult.error("浪潮授权失败:换取令牌异常,请从门户重新进入系统"); |
| | | } |
| | | if (ObjectUtils.isEmpty(token)) { |
| | | return AjaxResult.error("浪潮授权失败:授权码校验未通过(可能已过期或已被使用),请从门户重新进入系统"); |
| | | } |
| | | JSONObject userInfo; |
| | | try { |
| | | userInfo = TideUtils.getUserInfo(token); |
| | | } catch (Exception e) { |
| | | log.error("[浪潮SSO] 获取用户信息异常, token={}", token, e); |
| | | return AjaxResult.error("浪潮授权失败:获取用户信息异常,请联系管理员"); |
| | | } |
| | | log.info("[浪潮SSO] 用户信息: {}", userInfo); |
| | | if (userInfo == null) { |
| | | return AjaxResult.error("浪潮未返回用户信息,请联系平台管理员"); |
| | | } |
| | | // 兼容浪潮不同版本/不同平台的租户字段命名 |
| | | JSONObject tenant = null; |
| | | for (String key : new String[]{"tenant", "tenantInfo", "enterprise", "company", "org"}) { |
| | | Object v = userInfo.get(key); |
| | | if (v instanceof JSONObject) { |
| | | tenant = (JSONObject) v; |
| | | break; |
| | | } |
| | | if (v instanceof String && ((String) v).trim().startsWith("{")) { |
| | | try { |
| | | tenant = JSONObject.parseObject((String) v); |
| | | break; |
| | | } catch (Exception ignored) { |
| | | } |
| | | } |
| | | } |
| | | String account = tenant == null ? null : firstText(tenant, "tenantAccount", "account", "userName", "loginName", "mobile", "phone", "name"); |
| | | String password = tenant == null ? null : firstText(tenant, "tenantPassword", "password", "pwd", "passWord"); |
| | | if (ObjectUtils.isEmpty(account)) { |
| | | account = firstText(userInfo, "tenantAccount", "account", "userName", "loginName", "mobile", "phone"); |
| | | } |
| | | if (ObjectUtils.isEmpty(password)) { |
| | | password = firstText(userInfo, "tenantPassword", "password", "pwd"); |
| | | } |
| | | if (ObjectUtils.isEmpty(account) || ObjectUtils.isEmpty(password)) { |
| | | log.error("[浪潮SSO] 未取到租户账号/密码, userInfo={}", userInfo); |
| | | return AjaxResult.error("浪潮未返回企业租户账号信息,请联系平台管理员检查该账号的企业绑定"); |
| | | } |
| | | try { |
| | | String generateToken = loginService.login(account, password, null, IdWorker.get32UUID()); |
| | | AjaxResult ajax = AjaxResult.success(); |
| | | // 生成令牌 |
| | | String generateToken = loginService.login(tenant.get("tenantAccount").toString(), tenant.get("tenantPassword").toString(), null, |
| | | IdWorker.get32UUID()); |
| | | ajax.put(Constants.TOKEN, generateToken); |
| | | return ajax; |
| | | } catch (Exception e) { |
| | | log.error("[浪潮SSO] 租户账号登录失败, account={}", account, e); |
| | | return AjaxResult.error("浪潮单点登录失败:租户账号[" + account + "]在系统中登录失败(" + e.getMessage() + ")"); |
| | | } |
| | | } |
| | | |
| | | /** 依次尝试多个键,返回第一个非空字符串值 */ |
| | | private String firstText(JSONObject obj, String... keys) { |
| | | if (obj == null || keys == null) { |
| | | return null; |
| | | } |
| | | for (String key : keys) { |
| | | Object v = obj.get(key); |
| | | if (v == null) { |
| | | continue; |
| | | } |
| | | String s = String.valueOf(v).trim(); |
| | | if (!s.isEmpty() && !"null".equalsIgnoreCase(s)) { |
| | | return s; |
| | | } |
| | | } |
| | | return null; |
| | | } |
| | | |
| | | @PostMapping("/applicationOrdering") |