Browse Source

open

master
pioneer 2 years ago
commit
4ac24005ff
  1. 7
      README.md
  2. BIN
      doc/JSD-9844配置使用文档.docx
  3. 19
      plugin.xml
  4. 9
      src/main/java/com/fr/plugin/xxx/zojj/sso/Constants.java
  5. 16
      src/main/java/com/fr/plugin/xxx/zojj/sso/CustomLogInOutEventProvider.java
  6. 28
      src/main/java/com/fr/plugin/xxx/zojj/sso/LRGT.java
  7. 231
      src/main/java/com/fr/plugin/xxx/zojj/sso/LoginFilter.java
  8. 83
      src/main/java/com/fr/plugin/xxx/zojj/sso/conf/SsoConfig.java
  9. 238
      src/main/java/com/fr/plugin/xxx/zojj/sso/util/HttpClient.java
  10. 121
      src/main/java/com/fr/plugin/xxx/zojj/sso/util/LogUtils.java

7
README.md

@ -0,0 +1,7 @@
# open-JSD-9844
JSD-9844 和统一身份平台的单点登录和集成\
免责说明:该源码为第三方爱好者提供,不保证源码和方案的可靠性,也不提供任何形式的源码教学指导和协助!\
仅作为开发者学习参考使用!禁止用于任何商业用途!\
为保护开发者隐私,开发者信息已隐去!若原开发者希望公开自己的信息,可联系【pioneer】处理。

BIN
doc/JSD-9844配置使用文档.docx

Binary file not shown.

19
plugin.xml

@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?><plugin>
<id>com.fr.plugin.xxx.zojj.sso</id>
<name><![CDATA[xxxsso]]></name>
<active>yes</active>
<version>1.0</version>
<env-version>10.0</env-version>
<jartime>2018-07-31</jartime>
<vendor>fr.open</vendor>
<description><![CDATA[xxxsso]]></description>
<change-notes><![CDATA[
[2022-05-11]【1.0】初始化插件<br/>
]]></change-notes>
<lifecycle-monitor class="com.fr.plugin.xxx.zojj.sso.LRGT"/>
<extra-decision>
<LogInOutEventProvider class="com.fr.plugin.xxx.zojj.sso.CustomLogInOutEventProvider"/>
<GlobalRequestFilterProvider class="com.fr.plugin.xxx.zojj.sso.LoginFilter"/>
</extra-decision>
<function-recorder class="com.fr.plugin.xxx.zojj.sso.LoginFilter"/>
</plugin>

9
src/main/java/com/fr/plugin/xxx/zojj/sso/Constants.java

@ -0,0 +1,9 @@
package com.fr.plugin.xxx.zojj.sso;
/**
* @author xxx
* @date 2020/11/11
*/
public class Constants {
public static final String PLUGIN_ID = "com.fr.plugin.xxx.zojj.sso";
}

16
src/main/java/com/fr/plugin/xxx/zojj/sso/CustomLogInOutEventProvider.java

@ -0,0 +1,16 @@
package com.fr.plugin.xxx.zojj.sso;
import com.fr.decision.fun.impl.AbstractLogInOutEventProvider;
import com.fr.decision.webservice.login.LogInOutResultInfo;
import com.fr.plugin.xxx.zojj.sso.conf.SsoConfig;
/**
* @author xxx
* @date 2020/11/11
*/
public class CustomLogInOutEventProvider extends AbstractLogInOutEventProvider {
@Override
public String logoutAction(LogInOutResultInfo result) {
return SsoConfig.getInstance().getLogout();
}
}

28
src/main/java/com/fr/plugin/xxx/zojj/sso/LRGT.java

@ -0,0 +1,28 @@
package com.fr.plugin.xxx.zojj.sso;
import com.fr.plugin.context.PluginContext;
import com.fr.plugin.xxx.zojj.sso.conf.SsoConfig;
import com.fr.plugin.observer.inner.AbstractPluginLifecycleMonitor;
/**
* 配置信息初始化
*/
public class LRGT extends AbstractPluginLifecycleMonitor {
@Override
public void afterRun(PluginContext pluginContext) {
SsoConfig.getInstance();
}
@Override
public void beforeStop(PluginContext pluginContext) {
}
@Override
public void beforeUninstall(PluginContext pluginContext) {
}
@Override
public void afterInstall(PluginContext var1) {
}
}

231
src/main/java/com/fr/plugin/xxx/zojj/sso/LoginFilter.java

@ -0,0 +1,231 @@
package com.fr.plugin.xxx.zojj.sso;/*
* The contents of this file are subject to the Mozilla Public
* License Version 1.1 (the "License"); you may not use this
* file except in compliance with the License. You may obtain
* a copy of the License at http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an
* "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express
* or implied. See the License for the specific language governing
* rights and limitations under the License.
*
*
* The Original Code is OIOSAML Java Service Provider.
*
* The Initial Developer of the Original Code is Trifork A/S. Portions
* created by Trifork A/S are Copyright (C) 2008 Danish National IT
* and Telecom Agency (http://www.itst.dk). All Rights Reserved.
*
* Contributor(s):
* Joakim Recht <jre@trifork.com>
* Rolf Njor Jensen <rolf@trifork.com>
* Aage Nielsen <ani@openminds.dk>
*
*/
import com.fr.data.NetworkHelper;
import com.fr.decision.authority.data.User;
import com.fr.decision.fun.impl.AbstractGlobalRequestFilterProvider;
import com.fr.decision.mobile.terminal.TerminalHandler;
import com.fr.decision.webservice.utils.DecisionServiceConstants;
import com.fr.decision.webservice.utils.WebServiceUtils;
import com.fr.decision.webservice.v10.config.ConfigService;
import com.fr.decision.webservice.v10.login.LoginService;
import com.fr.decision.webservice.v10.login.TokenResource;
import com.fr.decision.webservice.v10.user.UserService;
import com.fr.intelli.record.Focus;
import com.fr.intelli.record.Original;
import com.fr.json.JSONObject;
import com.fr.locale.InterProviderFactory;
import com.fr.plugin.context.PluginContexts;
import com.fr.plugin.xxx.zojj.sso.conf.SsoConfig;
import com.fr.plugin.xxx.zojj.sso.util.HttpClient;
import com.fr.plugin.xxx.zojj.sso.util.LogUtils;
import com.fr.plugin.transform.FunctionRecorder;
import com.fr.record.analyzer.EnableMetrics;
import com.fr.stable.StringUtils;
import com.fr.stable.fun.Authorize;
import com.fr.stable.web.Device;
import com.fr.web.utils.WebUtils;
import javax.servlet.FilterChain;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;
/**
* @author xxx
* @date 2020/11/11
*/
@FunctionRecorder
@Authorize(callSignKey = Constants.PLUGIN_ID)
@EnableMetrics
public class LoginFilter extends AbstractGlobalRequestFilterProvider {
@Override
public String filterName() {
return "zojj";
}
@Override
@Focus(id = Constants.PLUGIN_ID, text = "域单点登录", source = Original.PLUGIN)
public String[] urlPatterns() {
if (PluginContexts.currentContext().isAvailable()) {
String servletPathName = "decision";
try {
servletPathName = ConfigService.getInstance().getBasicParam().getServletPathName();
} catch (Exception e) {
LogUtils.error(e.getMessage(), e);
}
return new String[]{
"/" + servletPathName,
"/" + servletPathName + "/view/report",
"/" + servletPathName + "/view/form",
};
} else {
return new String[]{"/neverbeused"};
}
}
@Override
public void doFilter(HttpServletRequest req, HttpServletResponse res, FilterChain filterChain) {
String ticket = req.getParameter("jwt_ticket");
SsoConfig config = SsoConfig.getInstance();
try {
if (isAccept(req) || isLogin(req)) {
next(req, res, filterChain);
return;
}
if (StringUtils.isNotBlank(ticket)) {
if (!config.isAccept()) {
setError(res, String.format("单点登录配置未完成"));
return;
}
String user = StringUtils.EMPTY;
Map<String, String> queryParas = new HashMap<>();
queryParas.put("jwt_ticket", ticket);
String json = HttpClient.get(config.getValidUrl(), queryParas, new HashMap<>());
LogUtils.debug4plugin("vlalid token url is {} token is {} res is {}", config.getValidUrl(), ticket, json);
JSONObject object = new JSONObject(json);
boolean valid = object.getBoolean("isvalid");
user = object.getString("user");
user = user.substring(user.lastIndexOf("\\") + 1);
if (!valid || StringUtils.isBlank(user)) {
setError(res, "单点验证不通过");
return;
}
if (!checkUser(user)) {
setError(res, "user: 【" + user + "】 not exist !");
return;
}
loginFromToken(req, res, user);
}
} catch (Exception e) {
LogUtils.error(e.getMessage(), e);
}
String redirect = getUrl(req);
LogUtils.debug4plugin("current request {} not login send redirect login", redirect);
String sso = config.getSsoUrl();
try {
res.sendRedirect(sso + "?redirecturl=" + URLEncoder.encode(redirect, "UTF-8"));
} catch (IOException e) {
LogUtils.error(e.getMessage(), e);
}
}
private String getUrl(HttpServletRequest req) {
String requestURL = req.getRequestURL().toString();
String queryString = req.getQueryString();
if (StringUtils.isNotBlank(queryString)) {
requestURL += "?" + queryString;
}
return requestURL;
}
private boolean isLogin(HttpServletRequest request) throws Exception {
String oldToken = TokenResource.COOKIE.getToken(request);
return oldToken != null && checkTokenValid(request, (String) oldToken);
}
private boolean checkTokenValid(HttpServletRequest req, String token) {
try {
Device device = NetworkHelper.getDevice(req);
LoginService.getInstance().loginStatusValid(token, TerminalHandler.getTerminal(req, device));
return true;
} catch (Exception ignore) {
}
return false;
}
private boolean isAccept(HttpServletRequest req) {
if (req.getRequestURI().endsWith("/view/form") || req.getRequestURI().endsWith("/view/report")) {
return StringUtils.isBlank(WebUtils.getHTTPRequestParameter(req, "viewlet")) && StringUtils.isNotBlank(req.getParameter("samlToken"));
}
return false;
}
private void next(HttpServletRequest request, HttpServletResponse response, FilterChain chain) {
try {
chain.doFilter(request, response);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
private boolean checkUser(String username) {
User user = null;
try {
user = UserService.getInstance().getUserByUserName(username);
LogUtils.debug4plugin("get user:" + user);
if (user != null) {
return true;
}
} catch (Exception e) {
LogUtils.error(e.getMessage(), e);
}
return false;
}
private boolean loginFromToken(HttpServletRequest req, HttpServletResponse res, String username) throws Exception {
try {
if (StringUtils.isNotEmpty(username)) {
LogUtils.debug4plugin("current username:" + username);
String token = LoginService.getInstance().login(req, res, username);
LogUtils.debug4plugin("get login token:" + token);
req.setAttribute(DecisionServiceConstants.FINE_AUTH_TOKEN_NAME, token);
LogUtils.debug4plugin("username:" + username + "login success");
return true;
} else {
LogUtils.warn("username is null!");
return false;
}
} catch (Exception e) {
LogUtils.error(e.getMessage(), e);
}
return false;
}
private void setError(HttpServletResponse res, String reason) {
try {
PrintWriter printWriter = WebUtils.createPrintWriter(res);
Map<String, Object> map = new HashMap<>();
map.put("result", InterProviderFactory.getProvider().getLocText("Fine-Engine_Error_Page_Result"));
map.put("reason", reason);
map.put("solution", InterProviderFactory.getProvider().getLocText("Fine-Engine_Please_Contact_Platform_Admin"));
String page = WebServiceUtils.parseWebPageResourceSafe("com/fr/web/controller/decision/entrance/resources/unavailable.html", map);
printWriter.write(page);
printWriter.flush();
printWriter.close();
} catch (Exception e) {
LogUtils.error(e.getMessage(), e);
}
}
}

83
src/main/java/com/fr/plugin/xxx/zojj/sso/conf/SsoConfig.java

@ -0,0 +1,83 @@
package com.fr.plugin.xxx.zojj.sso.conf;
import com.fr.config.*;
import com.fr.config.holder.Conf;
import com.fr.config.holder.factory.Holders;
import com.fr.stable.StringUtils;
/**
* @author xxx
* @since 2021/12/04
*/
@Visualization(category = "域认证单点登录配置")
public class SsoConfig extends DefaultConfiguration {
private static volatile SsoConfig config = null;
public static SsoConfig getInstance() {
if (config == null) {
config = ConfigContext.getConfigInstance(SsoConfig.class);
}
return config;
}
@Identifier(value = "debugSwitch", name = "插件调试开关", description = "日志调试模式", status = Status.SHOW)
private Conf<Boolean> debugSwitch = Holders.simple(true);
public Boolean getDebugSwitch() {
return debugSwitch.get();
}
public void setDebugSwitch(Boolean debugSwitch) {
this.debugSwitch.set(debugSwitch);
}
@Identifier(value = "ssoUrl", name = "sso服务器登录地址", description = "sso服务器", status = Status.SHOW)
private Conf<String> ssoUrl = Holders.simple(StringUtils.EMPTY);
public String getSsoUrl() {
return this.ssoUrl.get();
}
public void setSsoUrl(String url) {
this.ssoUrl.set(url);
}
@Identifier(value = "validUrl", name = "验证jwt地址", description = "验证jwt地址", status = Status.SHOW)
private Conf<String> validUrl = Holders.simple(StringUtils.EMPTY);
public String getValidUrl() {
return validUrl.get();
}
public void setValidUrl(String key) {
validUrl.set(key);
}
@Identifier(value = "logout", name = "logout", description = "登出地址", status = Status.SHOW)
private Conf<String> logout = Holders.simple(StringUtils.EMPTY);
public String getLogout() {
return logout.get();
}
public void setLogout(String key) {
logout.set(key);
}
@Override
public Object clone() throws CloneNotSupportedException {
SsoConfig cloned = (SsoConfig) super.clone();
cloned.debugSwitch = (Conf<Boolean>) debugSwitch.clone();
cloned.logout = (Conf<String>) logout.clone();
cloned.ssoUrl = (Conf<String>) ssoUrl.clone();
cloned.validUrl = (Conf<String>) validUrl.clone();
return cloned;
}
public boolean isAccept() {
return StringUtils.isNotBlank(getSsoUrl()) && StringUtils.isNotBlank(getValidUrl());
}
}

238
src/main/java/com/fr/plugin/xxx/zojj/sso/util/HttpClient.java

@ -0,0 +1,238 @@
package com.fr.plugin.xxx.zojj.sso.util;
import javax.net.ssl.*;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.Map;
import java.util.Map.Entry;
/**
*
* @version V1.0
* @ClassName: HttpClient
* @Description: HTTP
*/
public class HttpClient {
/**
* https 域名校验
*/
private class TrustAnyHostnameVerifier implements HostnameVerifier {
public boolean verify(String hostname, SSLSession session) {
return true;
}
}
/**
* https 证书管理
*/
private class TrustAnyTrustManager implements X509TrustManager {
public X509Certificate[] getAcceptedIssuers() {
return null;
}
public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
}
public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
}
}
private static final String GET = "GET";
private static final String POST = "POST";
// private static final String CHARSET = "GBK";
private static final String CHARSET = "UTF-8";
private static final SSLSocketFactory sslSocketFactory = initSSLSocketFactory();
private static final TrustAnyHostnameVerifier trustAnyHostnameVerifier = new HttpClient().new TrustAnyHostnameVerifier();
private static SSLSocketFactory initSSLSocketFactory() {
try {
TrustManager[] tm = { new HttpClient().new TrustAnyTrustManager() };
SSLContext sslContext = SSLContext.getInstance("TLS", "SunJSSE");
sslContext.init(null, tm, new java.security.SecureRandom());
return sslContext.getSocketFactory();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
private static HttpURLConnection getHttpConnection(String url, String method, Map<String, String> headers)
throws IOException, NoSuchAlgorithmException, NoSuchProviderException, KeyManagementException {
URL _url = new URL(url);
HttpURLConnection conn = (HttpURLConnection) _url.openConnection();
if (conn instanceof HttpsURLConnection) {
((HttpsURLConnection) conn).setSSLSocketFactory(sslSocketFactory);
((HttpsURLConnection) conn).setHostnameVerifier(trustAnyHostnameVerifier);
}
conn.setRequestMethod(method);
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setConnectTimeout(19000);
conn.setReadTimeout(19000);
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.146 Safari/537.36");
if (headers != null && !headers.isEmpty())
for (Entry<String, String> entry : headers.entrySet())
conn.setRequestProperty(entry.getKey(), entry.getValue());
return conn;
}
/**
* Send GET request
*/
public static String get(String url, Map<String, String> queryParas, Map<String, String> headers) {
HttpURLConnection conn = null;
try {
conn = getHttpConnection(buildUrlWithQueryString(url, queryParas), GET, headers);
conn.connect();
return readResponseString(conn);
} catch (Exception e) {
e.printStackTrace();
System.out.println(e.getMessage());
throw new RuntimeException(e);
} finally {
if (conn != null) {
conn.disconnect();
}
}
}
public static String get(String url, Map<String, String> queryParas) {
return get(url, queryParas, null);
}
public static String get(String url) {
return get(url, null, null);
}
/**
* Send POST request
*/
public static String post(String url, Map<String, String> queryParas, String data, Map<String, String> headers) {
HttpURLConnection conn = null;
try {
conn = getHttpConnection(buildUrlWithQueryString(url, queryParas), POST, headers);
conn.connect();
OutputStream out = conn.getOutputStream();
out.write(data.getBytes(CHARSET));
out.flush();
out.close();
return readResponseString(conn);
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
if (conn != null) {
conn.disconnect();
}
}
}
public static String post(String url, Map<String, String> queryParas, String data) {
return post(url, queryParas, data, null);
}
public static String post(String url, Map<String, String> data) {
return post(url, data, "", null);
}
public static String post(String url, String data, Map<String, String> headers) {
return post(url, null, data, headers);
}
public static String post(String url, String data) {
return post(url, null, data, null);
}
private static String readResponseString(HttpURLConnection conn) {
StringBuilder sb = new StringBuilder();
InputStream inputStream = null;
try {
inputStream = conn.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, CHARSET));
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line);
}
return sb.toString();
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**
* Build queryString of the url
*/
private static String buildUrlWithQueryString(String url, Map<String, String> queryParas) {
if (queryParas == null || queryParas.isEmpty())
return url;
StringBuilder sb = new StringBuilder(url);
boolean isFirst;
if (url.indexOf("?") == -1) {
isFirst = true;
sb.append("?");
} else {
isFirst = false;
}
for (Entry<String, String> entry : queryParas.entrySet()) {
if (isFirst)
isFirst = false;
else
sb.append("&");
String key = entry.getKey();
String value = entry.getValue();
if (value != null && !"".equals(value))
try {
value = URLEncoder.encode(value, CHARSET);
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
sb.append(key).append("=").append(value);
}
return sb.toString();
}
public enum HttpContentType {
JSON("Content-Type", "application/json; charset=UTF-8"), TEXT("Content-Type", "application/text; charset=UTF-8"), XML("Content-Type", "text/xml; charset=UTF-8");
private String type;
private String context;
public String getType() {
return this.type;
}
public String getContext() {
return this.context;
}
private HttpContentType(String type, String context) {
this.type = type;
this.context = context;
}
}
}

121
src/main/java/com/fr/plugin/xxx/zojj/sso/util/LogUtils.java

@ -0,0 +1,121 @@
package com.fr.plugin.xxx.zojj.sso.util;
import com.fr.log.FineLoggerFactory;
import com.fr.log.FineLoggerProvider;
import com.fr.plugin.context.PluginContexts;
import com.fr.plugin.xxx.zojj.sso.conf.SsoConfig;
import com.fr.stable.StringUtils;
/**
* @author xxx
* @since 2021/12/04
*/
public final class LogUtils {
private static final String DEBUG_PREFIX = "[插件调试] ";
private static String LOG_PREFIX = "[单点登录] ";
private static final String PLUGIN_VERSION;
private static final FineLoggerProvider LOGGER = FineLoggerFactory.getLogger();
static {
String version = PluginContexts.currentContext().getMarker().getVersion();
if (StringUtils.isNotBlank(version)) {
PLUGIN_VERSION = "[v" + version + "] ";
} else {
PLUGIN_VERSION = "[unknown version] ";
}
LOG_PREFIX = LOG_PREFIX + PLUGIN_VERSION;
}
public static void setPrefix(String prefix) {
if (prefix != null) {
LOG_PREFIX = prefix;
}
}
public static boolean isDebugEnabled() {
return LOGGER.isDebugEnabled();
}
public static void debug(String s) {
LOGGER.debug(LOG_PREFIX + s);
}
public static void debug(String s, Object... objects) {
LOGGER.debug(LOG_PREFIX + s, objects);
}
public static void debug(String s, Throwable throwable) {
LOGGER.debug(LOG_PREFIX + s, throwable);
}
public static void debug4plugin(String s) {
if (SsoConfig.getInstance().getDebugSwitch()) {
LOGGER.error(DEBUG_PREFIX + LOG_PREFIX + s);
} else {
LOGGER.debug(LOG_PREFIX + s);
}
}
public static void debug4plugin(String s, Object... objects) {
if (SsoConfig.getInstance().getDebugSwitch()) {
LOGGER.error(DEBUG_PREFIX + LOG_PREFIX + s, objects);
} else {
LOGGER.debug(LOG_PREFIX + s, objects);
}
}
public static void debug4plugin(String s, Throwable throwable) {
if (SsoConfig.getInstance().getDebugSwitch()) {
LOGGER.error(DEBUG_PREFIX + LOG_PREFIX + s, throwable);
} else {
LOGGER.debug(LOG_PREFIX + s, throwable);
}
}
public static boolean isInfoEnabled() {
return LOGGER.isInfoEnabled();
}
public static void info(String s) {
LOGGER.info(LOG_PREFIX + s);
}
public static void info(String s, Object... objects) {
LOGGER.info(LOG_PREFIX + s, objects);
}
public static void warn(String s) {
LOGGER.warn(LOG_PREFIX + s);
}
public static void warn(String s, Object... objects) {
LOGGER.warn(LOG_PREFIX + s, objects);
}
public static void warn(String s, Throwable throwable) {
LOGGER.warn(LOG_PREFIX + s, throwable);
}
public static void warn(Throwable throwable, String s, Object... objects) {
LOGGER.warn(throwable, LOG_PREFIX + s, objects);
}
public static void error(String s) {
LOGGER.error(LOG_PREFIX + s);
}
public static void error(String s, Object... objects) {
LOGGER.error(LOG_PREFIX + s, objects);
}
public static void error(String s, Throwable throwable) {
LOGGER.error(LOG_PREFIX + s, throwable);
}
public static void error(Throwable throwable, String s, Object... objects) {
LOGGER.error(throwable, LOG_PREFIX + s, objects);
}
}
Loading…
Cancel
Save