LAPTOP-SB56SG4Q\86185
3 years ago
10 changed files with 538 additions and 1 deletions
Binary file not shown.
@ -1,3 +1,6 @@
|
||||
# open-JSD-9243 |
||||
|
||||
JSD-9243 token单点集成 |
||||
JSD-9243 token单点集成\ |
||||
免责说明:该源码为第三方爱好者提供,不保证源码和方案的可靠性,也不提供任何形式的源码教学指导和协助!\ |
||||
仅作为开发者学习参考使用!禁止用于任何商业用途!\ |
||||
为保护开发者隐私,开发者信息已隐去!若原开发者希望公开自己的信息,可联系hugh处理。 |
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -0,0 +1,17 @@
|
||||
<?xml version="1.0" encoding="UTF-8" standalone="no"?> |
||||
<plugin> |
||||
<id>com.fr.plugin.third.party.ksdbaag</id> |
||||
<name><![CDATA[登录集成]]></name> |
||||
<active>yes</active> |
||||
<version>0.1</version> |
||||
<env-version>10.0</env-version> |
||||
<jartime>2019-01-01</jartime> |
||||
<vendor>fr.open</vendor> |
||||
<description><![CDATA[]]></description> |
||||
<change-notes><![CDATA[]]></change-notes> |
||||
<extra-decision> |
||||
<GlobalRequestFilterProvider class="com.fr.plugin.third.party.ksdbaag.http.SessionGlobalRequestFilterProvider"/> |
||||
</extra-decision> |
||||
<function-recorder class="com.fr.plugin.third.party.ksdbaag.config.DataConfigInitializeMonitor"/> |
||||
<lifecycle-monitor class="com.fr.plugin.third.party.ksdbaag.config.DataConfigInitializeMonitor"/> |
||||
</plugin> |
@ -0,0 +1,156 @@
|
||||
package com.fr.plugin.third.party.ksdbaag; |
||||
|
||||
import com.fanruan.api.log.LogKit; |
||||
import com.fanruan.api.util.StringKit; |
||||
import com.fr.general.IOUtils; |
||||
import com.fr.third.org.apache.http.HttpEntity; |
||||
import com.fr.third.org.apache.http.HttpStatus; |
||||
import com.fr.third.org.apache.http.client.config.RequestConfig; |
||||
import com.fr.third.org.apache.http.client.methods.CloseableHttpResponse; |
||||
import com.fr.third.org.apache.http.client.methods.HttpGet; |
||||
import com.fr.third.org.apache.http.client.methods.HttpPost; |
||||
import com.fr.third.org.apache.http.conn.ssl.NoopHostnameVerifier; |
||||
import com.fr.third.org.apache.http.conn.ssl.SSLConnectionSocketFactory; |
||||
import com.fr.third.org.apache.http.entity.StringEntity; |
||||
import com.fr.third.org.apache.http.impl.client.CloseableHttpClient; |
||||
import com.fr.third.org.apache.http.impl.client.HttpClients; |
||||
import com.fr.third.org.apache.http.ssl.SSLContextBuilder; |
||||
import com.fr.third.org.apache.http.ssl.TrustStrategy; |
||||
import com.fr.third.org.apache.http.util.EntityUtils; |
||||
|
||||
import javax.net.ssl.HostnameVerifier; |
||||
import javax.net.ssl.SSLContext; |
||||
import javax.servlet.ServletInputStream; |
||||
import javax.servlet.http.HttpServletRequest; |
||||
import java.io.IOException; |
||||
import java.security.cert.CertificateException; |
||||
import java.security.cert.X509Certificate; |
||||
|
||||
public class Utils { |
||||
public static String DEFAULT_USER_AGENT = "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.61 Safari/537.36"; |
||||
public static RequestConfig REQUEST_CONFIG = RequestConfig.custom() |
||||
.setConnectionRequestTimeout(30000) |
||||
.setSocketTimeout(30000) // 服务端相应超时
|
||||
.setConnectTimeout(30000) // 建立socket链接超时时间
|
||||
.build(); |
||||
|
||||
public static CloseableHttpClient createSSLClientDefault() { |
||||
try { |
||||
SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() { |
||||
|
||||
@Override |
||||
public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException { |
||||
return true; |
||||
} |
||||
}).build(); |
||||
HostnameVerifier hostnameVerifier = NoopHostnameVerifier.INSTANCE; |
||||
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext, hostnameVerifier); |
||||
return HttpClients.custom().setSSLSocketFactory(sslsf).build(); |
||||
} catch (Exception e) { |
||||
LogKit.error(e.getMessage(), e); |
||||
} |
||||
return HttpClients.createDefault(); |
||||
} |
||||
|
||||
public static synchronized CloseableHttpClient createHttpClient(String url) { |
||||
CloseableHttpClient httpClient = null; |
||||
if (StringKit.isEmpty(url)) { |
||||
httpClient = HttpClients.createDefault(); |
||||
return httpClient; |
||||
} |
||||
|
||||
if (url.startsWith("https://")) { |
||||
httpClient = createSSLClientDefault(); |
||||
return httpClient; |
||||
} |
||||
httpClient = HttpClients.createDefault(); |
||||
return httpClient; |
||||
} |
||||
|
||||
public static synchronized String createHttpGetContent(CloseableHttpClient httpClient, String url) throws IOException { |
||||
if ((httpClient == null) || (StringKit.isEmpty(url))) { |
||||
return ""; |
||||
} |
||||
|
||||
HttpGet httpGet = new HttpGet(url); |
||||
httpGet.addHeader("User-Agent", Utils.DEFAULT_USER_AGENT); |
||||
httpGet.setConfig(Utils.REQUEST_CONFIG); |
||||
CloseableHttpResponse response = httpClient.execute(httpGet); |
||||
int statusCode = response.getStatusLine().getStatusCode(); |
||||
if (statusCode != HttpStatus.SC_OK) { |
||||
response.close(); |
||||
LogKit.info("http请求出错,http status:" + statusCode); |
||||
return ""; |
||||
} |
||||
|
||||
HttpEntity httpEntity = response.getEntity(); |
||||
if (httpEntity == null) { |
||||
response.close(); |
||||
LogKit.info("http请求出错,http响应内容为空"); |
||||
return ""; |
||||
} |
||||
String responseContent = EntityUtils.toString(httpEntity, "UTF-8"); |
||||
response.close(); |
||||
if (StringKit.isEmpty(responseContent)) { |
||||
LogKit.info("http请求出错,http响应内容为空1"); |
||||
return ""; |
||||
} |
||||
return responseContent; |
||||
} |
||||
|
||||
public static synchronized String createHttpPostContent(CloseableHttpClient httpClient, String url, String bodyContent) throws IOException { |
||||
if ((httpClient == null) || (StringKit.isEmpty(url)) || (StringKit.isEmpty(bodyContent))) { |
||||
return ""; |
||||
} |
||||
|
||||
HttpPost httpPost = new HttpPost(url); |
||||
httpPost.addHeader("User-Agent", Utils.DEFAULT_USER_AGENT); |
||||
httpPost.setConfig(Utils.REQUEST_CONFIG); |
||||
StringEntity bodyEntity = new StringEntity(bodyContent, "UTF-8"); |
||||
httpPost.setEntity(bodyEntity); |
||||
CloseableHttpResponse response = httpClient.execute(httpPost); |
||||
int statusCode = response.getStatusLine().getStatusCode(); |
||||
if (statusCode != HttpStatus.SC_OK) { |
||||
response.close(); |
||||
LogKit.info("http请求出错,http status:" + statusCode); |
||||
return ""; |
||||
} |
||||
|
||||
HttpEntity httpEntity = response.getEntity(); |
||||
if (httpEntity == null) { |
||||
response.close(); |
||||
LogKit.info("http请求出错,http响应内容为空"); |
||||
return ""; |
||||
} |
||||
String responseContent = EntityUtils.toString(httpEntity, "UTF-8"); |
||||
response.close(); |
||||
if (StringKit.isEmpty(responseContent)) { |
||||
LogKit.info("http请求出错,http响应内容为空1"); |
||||
return ""; |
||||
} |
||||
return responseContent; |
||||
} |
||||
|
||||
/** |
||||
* 获取请求主体内容 |
||||
* @param req |
||||
* @return |
||||
* @throws IOException |
||||
*/ |
||||
public static String getHttpRequestBody(HttpServletRequest req) throws IOException { |
||||
if (req == null) { |
||||
return ""; |
||||
} |
||||
ServletInputStream inputStream = req.getInputStream(); |
||||
if (inputStream == null) { |
||||
return ""; |
||||
} |
||||
String content = IOUtils.inputStream2String(inputStream); |
||||
if (StringKit.isEmpty(content)) { |
||||
return ""; |
||||
} |
||||
return content; |
||||
} |
||||
|
||||
|
||||
} |
@ -0,0 +1,59 @@
|
||||
package com.fr.plugin.third.party.ksdbaag.config; |
||||
|
||||
|
||||
import com.fr.config.*; |
||||
import com.fr.config.holder.Conf; |
||||
import com.fr.config.holder.factory.Holders; |
||||
|
||||
/** |
||||
* 配置数据保存 |
||||
*/ |
||||
@Visualization(category = "登录集成配置") |
||||
public class CustomDataConfig extends DefaultConfiguration { |
||||
public String getNameSpace() { |
||||
return this.getClass().getName(); |
||||
} |
||||
|
||||
private static volatile CustomDataConfig config = null; |
||||
|
||||
public static CustomDataConfig getInstance() { |
||||
if (config == null) { |
||||
config = ConfigContext.getConfigInstance(CustomDataConfig.class); |
||||
} |
||||
return config; |
||||
} |
||||
|
||||
|
||||
@Identifier(value = "accessTokenUrl", name = "获取用户信息地址", description = "", status = Status.SHOW) |
||||
private Conf<String> accessTokenUrl = Holders.simple(""); |
||||
|
||||
@Identifier(value = "frUrl", name = "报表地址", description = "", status = Status.SHOW) |
||||
private Conf<String> frUrl = Holders.simple(""); |
||||
|
||||
|
||||
public String getAccessTokenUrl() { |
||||
return accessTokenUrl.get(); |
||||
} |
||||
|
||||
public void setAccessTokenUrl(String accessTokenUrl) { |
||||
this.accessTokenUrl.set(accessTokenUrl); |
||||
} |
||||
|
||||
public String getFrUrl() { |
||||
return frUrl.get(); |
||||
} |
||||
|
||||
public void setFrUrl(String frUrl) { |
||||
this.frUrl.set(frUrl); |
||||
} |
||||
|
||||
|
||||
@Override |
||||
public Object clone() throws CloneNotSupportedException { |
||||
CustomDataConfig cloned = (CustomDataConfig) super.clone(); |
||||
cloned.frUrl = (Conf<String>) frUrl.clone(); |
||||
cloned.accessTokenUrl = (Conf<String>) accessTokenUrl.clone(); |
||||
|
||||
return cloned; |
||||
} |
||||
} |
@ -0,0 +1,24 @@
|
||||
package com.fr.plugin.third.party.ksdbaag.config; |
||||
|
||||
import com.fr.intelli.record.Focus; |
||||
import com.fr.intelli.record.Original; |
||||
import com.fr.plugin.context.PluginContext; |
||||
import com.fr.plugin.observer.inner.AbstractPluginLifecycleMonitor; |
||||
import com.fr.record.analyzer.EnableMetrics; |
||||
|
||||
/** |
||||
* 配置信息初始化 |
||||
*/ |
||||
@EnableMetrics |
||||
public class DataConfigInitializeMonitor extends AbstractPluginLifecycleMonitor { |
||||
@Override |
||||
@Focus(id = "com.fr.plugin.third.party.ksdbaag", text = "plugin-ksdbaag", source = Original.PLUGIN) |
||||
public void afterRun(PluginContext pluginContext) { |
||||
CustomDataConfig.getInstance(); |
||||
} |
||||
|
||||
@Override |
||||
public void beforeStop(PluginContext pluginContext) { |
||||
|
||||
} |
||||
} |
@ -0,0 +1,278 @@
|
||||
package com.fr.plugin.third.party.ksdbaag.http; |
||||
|
||||
import com.fanruan.api.log.LogKit; |
||||
import com.fanruan.api.util.StringKit; |
||||
import com.fr.decision.authority.data.User; |
||||
import com.fr.decision.fun.impl.AbstractGlobalRequestFilterProvider; |
||||
import com.fr.decision.webservice.v10.login.LoginService; |
||||
import com.fr.decision.webservice.v10.user.UserService; |
||||
import com.fr.json.JSONObject; |
||||
import com.fr.plugin.third.party.ksdbaag.Utils; |
||||
import com.fr.plugin.third.party.ksdbaag.config.CustomDataConfig; |
||||
import com.fr.third.org.apache.http.HttpEntity; |
||||
import com.fr.third.org.apache.http.HttpStatus; |
||||
import com.fr.third.org.apache.http.client.config.RequestConfig; |
||||
import com.fr.third.org.apache.http.client.methods.CloseableHttpResponse; |
||||
import com.fr.third.org.apache.http.client.methods.HttpPost; |
||||
import com.fr.third.org.apache.http.impl.client.CloseableHttpClient; |
||||
import com.fr.third.org.apache.http.util.EntityUtils; |
||||
import com.fr.web.utils.WebUtils; |
||||
|
||||
import javax.servlet.FilterChain; |
||||
import javax.servlet.http.HttpServletRequest; |
||||
import javax.servlet.http.HttpServletResponse; |
||||
import java.text.SimpleDateFormat; |
||||
import java.util.Date; |
||||
|
||||
|
||||
public class SessionGlobalRequestFilterProvider extends AbstractGlobalRequestFilterProvider { |
||||
//private static CloseableHttpClient httpClient = HttpClients.createDefault();
|
||||
private static String DEFAULT_USER_AGENT = "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.61 Safari/537.36"; |
||||
|
||||
@Override |
||||
public String filterName() { |
||||
return "com.fr.plugin.third.party.jsdbaag"; |
||||
} |
||||
|
||||
@Override |
||||
public String[] urlPatterns() { |
||||
return new String[]{"/decision", "/decision/*"}; |
||||
} |
||||
|
||||
@Override |
||||
public void doFilter(HttpServletRequest req, HttpServletResponse res, FilterChain filterChain) { |
||||
try { |
||||
String fullUrl = req.getRequestURL().toString(); |
||||
String queryUrl = req.getQueryString(); |
||||
if ((queryUrl == null) || "null".equalsIgnoreCase(queryUrl)) { |
||||
queryUrl = ""; |
||||
} else { |
||||
queryUrl = "?" + queryUrl; |
||||
} |
||||
|
||||
String fullUrl1 = fullUrl + queryUrl; |
||||
String method = req.getMethod(); |
||||
LogKit.info("登录集成,记录访问地址:" + method + " " + fullUrl1); |
||||
|
||||
if (fullUrl.indexOf("/remote/") >= 0) { |
||||
filterChain.doFilter(req, res); |
||||
return; |
||||
} |
||||
|
||||
if (fullUrl.indexOf("terminal=H5") >= 0) { |
||||
filterChain.doFilter(req, res); |
||||
return; |
||||
} |
||||
|
||||
if (fullUrl.indexOf("__device__=") >= 0) { |
||||
filterChain.doFilter(req, res); |
||||
return; |
||||
} |
||||
|
||||
if (fullUrl.indexOf("/weixin/") >= 0) { |
||||
filterChain.doFilter(req, res); |
||||
return; |
||||
} |
||||
|
||||
|
||||
if (fullUrl.indexOf("/dingtalk/") >= 0) { |
||||
filterChain.doFilter(req, res); |
||||
return; |
||||
} |
||||
|
||||
if (!"GET".equalsIgnoreCase(method)) { |
||||
filterChain.doFilter(req, res); |
||||
return; |
||||
} |
||||
|
||||
// boolean option = isLogged(req);
|
||||
//if (option) {
|
||||
// filterChain.doFilter(req, res);
|
||||
// return;
|
||||
//}
|
||||
String loginUsername = getOauthLoginUsername(req); |
||||
if (StringKit.isEmpty(loginUsername)) { |
||||
filterChain.doFilter(req, res); |
||||
return; |
||||
} |
||||
|
||||
LogKit.info("登录集成,用户名:" + loginUsername); |
||||
User user = UserService.getInstance().getUserByUserName(loginUsername); |
||||
boolean tipsOption = false; |
||||
String tipsContent = ""; |
||||
if (user == null) { |
||||
tipsOption = true; |
||||
LogKit.info("登录集成,用户名:" + loginUsername + "在报表平台不存在"); |
||||
tipsContent = "在报表服务器上不存在"; |
||||
} else if (!user.isEnable()) { |
||||
tipsOption = true; |
||||
LogKit.info("登录集成,用户名:" + loginUsername + "在报表平台上被禁用"); |
||||
tipsContent = "在报表平台上被禁用"; |
||||
} |
||||
|
||||
if (tipsOption) { |
||||
String jumpContent = "<!doctype html>\n" + |
||||
"<head>\n" + |
||||
" <meta charset=\"utf-8\" />\n" + |
||||
" <title>提示</title>\n" + |
||||
"</head>\n" + |
||||
"<body>\n" + |
||||
" <script>\n" + |
||||
" var t = 20;\n" + |
||||
" var referI = setInterval(\"refer()\", 1000);\n" + |
||||
" function refer() {\n" + |
||||
" document.getElementById('show').innerHTML = \"用户:" + loginUsername + tipsContent + ",请联系管理员!<br>\" + t + \"秒后跳转到报表首页\"; \n" + |
||||
" t--;\n" + |
||||
" if (t <= 0) {\n" + |
||||
" clearInterval(referI);\n" + |
||||
" window.location = \"" + CustomDataConfig.getInstance().getFrUrl() + "\";\n" + |
||||
" }\n" + |
||||
" }\n" + |
||||
" </script>\n" + |
||||
" <div style=\"width: 100%;height:200px; line-height: 200px;font-size:30px;vertical-align:middle;text-align:center\">\n" + |
||||
" <span id=\"show\"></span>\n" + |
||||
" </div>\n" + |
||||
"</body>\n" + |
||||
"</html>"; |
||||
res.setContentType("text/html;charset=UTF-8"); |
||||
WebUtils.printAsString(res, jumpContent); |
||||
res.setStatus(200); |
||||
return; |
||||
} |
||||
|
||||
loginUsername = user.getUserName(); |
||||
LogKit.info("登录集成,报表平台用户名:" + loginUsername); |
||||
|
||||
String loginToken = LoginService.getInstance().login(req, res, loginUsername); |
||||
req.setAttribute("fine_auth_token", loginToken); |
||||
filterChain.doFilter(req, res); |
||||
} catch (Exception e) { |
||||
LogKit.error("登录集成出错," + e.getMessage(), e); |
||||
} |
||||
} |
||||
|
||||
private String getOauthLoginUsername(HttpServletRequest req) { |
||||
try { |
||||
if (req == null) { |
||||
return ""; |
||||
} |
||||
String oAuthCode = WebUtils.getHTTPRequestParameter(req, "token"); |
||||
if (StringKit.isEmpty(oAuthCode)) { |
||||
return ""; |
||||
} |
||||
LogKit.info("登录集成,token:" + oAuthCode); |
||||
|
||||
RequestConfig requestConfig = RequestConfig.custom() |
||||
.setConnectionRequestTimeout(10000) |
||||
.setSocketTimeout(10000) // 服务端相应超时
|
||||
.setConnectTimeout(10000) // 建立socket链接超时时间
|
||||
.build(); |
||||
|
||||
//获取授权Token
|
||||
//https://ssotest.wm-motor.com/profile/oauth2/accessToken?client_id=YOUR_CLIENT_ID&client_secret=YOUR_SECRET&redirect_uri=YOUR_REGISTERED_REDIRECT_URI&code=CODE
|
||||
String accessTokenUrl = CustomDataConfig.getInstance().getAccessTokenUrl(); |
||||
LogKit.info("登录集成,获取用户信息地址:" + accessTokenUrl); |
||||
HttpPost httpPost = new HttpPost(accessTokenUrl); |
||||
httpPost.addHeader("User-Agent", DEFAULT_USER_AGENT); |
||||
String authValue = "bearer " + oAuthCode; |
||||
httpPost.addHeader("auth", authValue); |
||||
|
||||
httpPost.setConfig(requestConfig); |
||||
CloseableHttpClient httpClient = Utils.createHttpClient(accessTokenUrl); |
||||
CloseableHttpResponse response = httpClient.execute(httpPost); |
||||
int statusCode = response.getStatusLine().getStatusCode(); |
||||
if (statusCode != HttpStatus.SC_OK) { |
||||
response.close(); |
||||
httpClient.close(); |
||||
LogKit.info("登录集成,获取用户信息请求出错,http status:" + statusCode); |
||||
return ""; |
||||
} |
||||
|
||||
HttpEntity httpEntity = response.getEntity(); |
||||
if (httpEntity == null) { |
||||
response.close(); |
||||
httpClient.close(); |
||||
LogKit.info("登录集成,获取用户信息请求出错,http响应内容为空"); |
||||
return ""; |
||||
} |
||||
String responseContent = EntityUtils.toString(httpEntity, "UTF-8"); |
||||
response.close(); |
||||
if (StringKit.isEmpty(responseContent)) { |
||||
httpClient.close(); |
||||
LogKit.info("登录集成,获取用户信息请求出错,http响应内容为空1"); |
||||
return ""; |
||||
} |
||||
LogKit.info("登录集成,获取用户信息请求,http响应内容\n" + responseContent); |
||||
|
||||
String uid = getUserId(responseContent); |
||||
if (StringKit.isEmpty(uid)) { |
||||
LogKit.info("登录集成,,获取用户信息请求出错,uid为空"); |
||||
return ""; |
||||
} |
||||
LogKit.info("登录集成,用户名:" + uid); |
||||
return uid; |
||||
} catch (Exception e) { |
||||
LogKit.error("登录集成,获取用户名出错," + e.getMessage(), e); |
||||
} |
||||
return ""; |
||||
} |
||||
|
||||
private String getUserId(String content) { |
||||
if (StringKit.isEmpty(content)) { |
||||
return ""; |
||||
} |
||||
|
||||
String loginName; |
||||
JSONObject contentJson = new JSONObject(content); |
||||
int code = contentJson.getInt("code"); |
||||
if (code != 200) { |
||||
return ""; |
||||
} |
||||
JSONObject dataJson = contentJson.getJSONObject("data"); |
||||
if (dataJson == null) { |
||||
return ""; |
||||
} |
||||
loginName = dataJson.getString("userName"); |
||||
return loginName; |
||||
} |
||||
|
||||
|
||||
public synchronized static String getSysTime() { |
||||
SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd"); |
||||
Date date = new Date(); |
||||
String nowData = format.format(date); |
||||
return nowData; |
||||
} |
||||
|
||||
private void sendRedirect(HttpServletResponse res, String url) { |
||||
res.setStatus(HttpServletResponse.SC_MOVED_TEMPORARILY); |
||||
res.setHeader("Location", url); |
||||
} |
||||
|
||||
|
||||
|
||||
private String addParaToQuery(String query, String paraName, String[] paraValues) { |
||||
if (StringKit.isEmpty(paraName)) { |
||||
return query; |
||||
} |
||||
String fullQuery = query; |
||||
if ((paraValues == null) || (paraValues.length <= 0)) { |
||||
if (StringKit.isNotEmpty(fullQuery)) { |
||||
fullQuery = fullQuery + "&"; |
||||
} |
||||
fullQuery = paraName + "="; |
||||
return fullQuery; |
||||
} |
||||
for (int i = 0, max = paraValues.length - 1; i <= max; i++) { |
||||
if (StringKit.isNotEmpty(fullQuery)) { |
||||
fullQuery = fullQuery + "&"; |
||||
} |
||||
fullQuery = fullQuery + paraName + "=" + paraValues[i]; |
||||
} |
||||
return fullQuery; |
||||
} |
||||
|
||||
|
||||
|
||||
|
||||
} |
Loading…
Reference in new issue