|
@@ -1,19 +1,9 @@
|
|
|
package com.kingdee.eas.custom.sso;
|
|
|
|
|
|
import com.alibaba.fastjson.JSON;
|
|
|
-import com.alibaba.fastjson.JSONObject;
|
|
|
-import com.kingdee.bos.BOSException;
|
|
|
-import com.kingdee.bos.Context;
|
|
|
-import com.kingdee.eas.cp.eip.sso.util.CASLoginConfigPropUtil;
|
|
|
-import com.kingdee.eas.cp.eip.sso.util.CloudParamUtil;
|
|
|
-import com.kingdee.eas.util.app.DbUtil;
|
|
|
import com.kingdee.eas.utils.ExpiringMapCache;
|
|
|
-import com.kingdee.jdbc.rowset.IRowSet;
|
|
|
import com.kingdee.shr.base.syssetting.exception.SHRWebException;
|
|
|
import com.kingdee.util.StringUtils;
|
|
|
-import okhttp3.OkHttpClient;
|
|
|
-import okhttp3.Request;
|
|
|
-import okhttp3.Response;
|
|
|
import org.apache.log4j.Logger;
|
|
|
|
|
|
import javax.servlet.ServletException;
|
|
@@ -22,7 +12,6 @@ import javax.servlet.http.HttpServletRequest;
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
import java.io.*;
|
|
|
import java.net.URLEncoder;
|
|
|
-import java.sql.SQLException;
|
|
|
import java.text.MessageFormat;
|
|
|
import java.util.HashMap;
|
|
|
import java.util.Map;
|
|
@@ -110,6 +99,10 @@ public class OAToMBos extends HttpServlet {
|
|
|
if (StringUtils.isEmpty(getAccessTokenPath)) {
|
|
|
throw new RuntimeException("getAccessTokenPath不能为空! 请检查配置文件: " + propPath);
|
|
|
}
|
|
|
+ String serverName = prop.getProperty("serverName");
|
|
|
+ if (StringUtils.isEmpty(serverName)) {
|
|
|
+ throw new RuntimeException("serverName不能为空! 请检查配置文件: " + propPath);
|
|
|
+ }
|
|
|
String client_secret = prop.getProperty("client_secret");
|
|
|
if (StringUtils.isEmpty(client_secret)) {
|
|
|
throw new RuntimeException("client_secret不能为空! 请检查配置文件: " + propPath);
|
|
@@ -136,7 +129,7 @@ public class OAToMBos extends HttpServlet {
|
|
|
params.put("redirect_uri", URLEncoder.encode(redirect_uri2, "UTF-8"));
|
|
|
try {
|
|
|
String redirectUrl = null;
|
|
|
- String token = getAccessToken(getAccessTokenPath, params);
|
|
|
+ String token = SsoUtil.getAccessToken(getAccessTokenPath, params);
|
|
|
logger.error(token);
|
|
|
if (StringUtils.isEmpty(redirectUrlKey)) {
|
|
|
String eid = prop.getProperty("eid");
|
|
@@ -149,7 +142,9 @@ public class OAToMBos extends HttpServlet {
|
|
|
}
|
|
|
Map toMbosparams = new HashMap();
|
|
|
toMbosparams.put("token", token);
|
|
|
- String urlString = appendUrl(redirectUrl, toMbosparams);
|
|
|
+ //校验流程生效状态
|
|
|
+ redirectUrl = SsoUtil.checkFlowEffectiveStateForMbos(redirectUrl, serverName, getLoginIdPath, token);
|
|
|
+ String urlString = SsoUtil.appendUrl(redirectUrl, toMbosparams);
|
|
|
resp.sendRedirect(urlString);
|
|
|
logger.error("redirectMbos url" + urlString);
|
|
|
} catch (Exception e) {
|
|
@@ -195,7 +190,7 @@ public class OAToMBos extends HttpServlet {
|
|
|
params.put("client_id", client_id);
|
|
|
params.put("response_type", response_type);
|
|
|
params.put("redirect_uri", URLEncoder.encode(redirect_uri1, "UTF-8"));
|
|
|
- String urlString = appendUrl(authorizePath, params);
|
|
|
+ String urlString = SsoUtil.appendUrl(authorizePath, params);
|
|
|
resp.sendRedirect(urlString);
|
|
|
logger.error("authorize url" + urlString);
|
|
|
}
|
|
@@ -224,7 +219,7 @@ public class OAToMBos extends HttpServlet {
|
|
|
Map result = new HashMap();
|
|
|
try {
|
|
|
//从人员对象,获取纷享用户userId
|
|
|
- String loginId = loginId2userId(getLoginIdPath, token);
|
|
|
+ String loginId = SsoUtil.loginId2userId(getLoginIdPath, token);
|
|
|
result.put("status", "0");
|
|
|
result.put("message", loginId);
|
|
|
resp.setStatus(200);
|
|
@@ -239,126 +234,4 @@ public class OAToMBos extends HttpServlet {
|
|
|
writer.write(JSON.toJSONString(result));
|
|
|
writer.close();
|
|
|
}
|
|
|
-
|
|
|
- /**
|
|
|
- * 获取泛微token方法
|
|
|
- *
|
|
|
- * @param getAccessTokenPath
|
|
|
- * @param params
|
|
|
- * @return
|
|
|
- * @throws IOException
|
|
|
- */
|
|
|
- private String getAccessToken(String getAccessTokenPath, Map<String, String> params)
|
|
|
- throws IOException {
|
|
|
- logger.error("getAccessToken方法参数: " + params);
|
|
|
- String url = appendUrl(getAccessTokenPath, params);
|
|
|
- logger.error("access_token url" + url);
|
|
|
- OkHttpClient client = new OkHttpClient();
|
|
|
- Request request = new Request.Builder()
|
|
|
- .url(url)
|
|
|
- .get()
|
|
|
- .addHeader("content-type", "multipart/form-data; boundary=---011000010111000001101001")
|
|
|
- .build();
|
|
|
- Response response = client.newCall(request).execute();
|
|
|
- if (response.isSuccessful()) {
|
|
|
- String string = response.body().string();
|
|
|
- JSONObject jsonObject = JSONObject.parseObject(string);
|
|
|
- String code = jsonObject.getString("code");
|
|
|
- if ("0".equals(code)) {
|
|
|
- String access_token = jsonObject.getString("access_token");
|
|
|
- logger.error("access_token " + access_token);
|
|
|
- return access_token;
|
|
|
- } else {
|
|
|
- throw new RuntimeException(jsonObject.getString("msg"));
|
|
|
- }
|
|
|
- } else {
|
|
|
- //网络超时
|
|
|
- throw new RuntimeException("获取token超时");
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 获取用户信息
|
|
|
- *
|
|
|
- * @param accessToken
|
|
|
- * @return
|
|
|
- * @throws IOException
|
|
|
- * @throws BOSException
|
|
|
- * @throws SQLException
|
|
|
- */
|
|
|
- private String loginId2userId(String getLoginIdPath, String accessToken)
|
|
|
- throws IOException, BOSException, SQLException {
|
|
|
- if (StringUtils.isEmpty(accessToken)) {
|
|
|
- throw new RuntimeException("accessToken不能为空! ");
|
|
|
- }
|
|
|
- //获取第三方用户信息
|
|
|
- Map params = new HashMap();
|
|
|
- params.put("access_token", accessToken);
|
|
|
- String url = appendUrl(getLoginIdPath, params);
|
|
|
- logger.error("loginId2userId url" + url);
|
|
|
- OkHttpClient client = new OkHttpClient();
|
|
|
- Request request = new Request.Builder()
|
|
|
- .url(url)
|
|
|
- .get()
|
|
|
- .addHeader("content-type", "multipart/form-data; boundary=---011000010111000001101001")
|
|
|
- .build();
|
|
|
- Response response = client.newCall(request).execute();
|
|
|
- if (response.isSuccessful()) {
|
|
|
- String string = response.body().string();
|
|
|
- JSONObject jsonObject = JSONObject.parseObject(string);
|
|
|
- String code = jsonObject.getString("code");
|
|
|
- if ("0".equals(code)) {
|
|
|
- JSONObject attributes = jsonObject.getJSONObject("attributes");
|
|
|
- //登录id
|
|
|
- String loginid = attributes.getString("loginid");
|
|
|
- String dataCenter = CASLoginConfigPropUtil.getDataCenter();
|
|
|
- String locale = CASLoginConfigPropUtil.getLocale();
|
|
|
- if (!StringUtils.isEmpty(dataCenter) && !StringUtils.isEmpty(locale)) {
|
|
|
- Context ctx = CloudParamUtil.getContext(dataCenter, locale, "administrator");
|
|
|
- String sql = "SELECT count(1) total FROM T_PM_USER WHERE fnumber=?";
|
|
|
- IRowSet rs = DbUtil.executeQuery(ctx, sql, new Object[]{loginid});
|
|
|
- int total = 0;
|
|
|
- if (rs.next()) {
|
|
|
- total = rs.getInt("total");
|
|
|
- }
|
|
|
- if (total <= 0) {
|
|
|
- logger.error("SHR找不到对应的用户, loginid:" + loginid);
|
|
|
- throw new RuntimeException("SHR找不到对应的用户, loginid: " + loginid);
|
|
|
- //("您无权限访问SHR系统,请联系管理员处理。")
|
|
|
- } else if (total > 1) {
|
|
|
- logger.error("SHR找到多个对应的用户, loginid:" + loginid);
|
|
|
- throw new RuntimeException("SHR找到多个对应的用户, loginid: " + loginid);
|
|
|
- //("您无权限访问SHR系统,请联系管理员处理。")
|
|
|
- } else {
|
|
|
- return loginid;
|
|
|
- }
|
|
|
- }
|
|
|
- logger.error("获取用户信息报错,数据中心没找到!");
|
|
|
- throw new RuntimeException("获取用户信息报错,数据中心没找到!");
|
|
|
- } else {
|
|
|
- logger.error(jsonObject.getString("msg"));
|
|
|
- throw new RuntimeException(jsonObject.getString("msg"));
|
|
|
- }
|
|
|
- } else {
|
|
|
- //网络超时
|
|
|
- logger.error("网络超时");
|
|
|
- throw new RuntimeException("网络超时");
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 拼接地址参数
|
|
|
- */
|
|
|
- private static String appendUrl(String url, Map<String, String> data) {
|
|
|
- logger.error("appendUrl_url: " + url);
|
|
|
- logger.error("appendUrl_data: " + data);
|
|
|
- StringBuilder paramStr = new StringBuilder();
|
|
|
- for (String key : data.keySet()) {
|
|
|
- paramStr.append(key).append("=").append(data.get(key)).append("&");
|
|
|
- }
|
|
|
- paramStr.deleteCharAt(paramStr.lastIndexOf("&"));
|
|
|
- String str = url.contains("?") ? (url + "&" + paramStr) : (url + "?" + paramStr);
|
|
|
- logger.error("拼接后的地址为:" + str);
|
|
|
- return str;
|
|
|
- }
|
|
|
}
|