LAPTOP-SB56SG4Q\86185
3 years ago
10 changed files with 427 additions and 1 deletions
Binary file not shown.
Binary file not shown.
@ -1,3 +1,6 @@
|
||||
# open-JSD-7803 |
||||
|
||||
JSD-7803 开源任务材料 |
||||
jsd-7803 开源材料\ |
||||
免责说明:该源码为第三方爱好者提供,不保证源码和方案的可靠性,也不提供任何形式的源码教学指导和协助!\ |
||||
仅作为开发者学习参考使用!禁止用于任何商业用途!\ |
||||
为保护开发者隐私,开发者信息已隐去!若原开发者希望公开自己的信息,可联系hugh处理。 |
@ -0,0 +1,24 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?> |
||||
<plugin> |
||||
<id>com.fr.plugin.j7803.sso.auth</id> |
||||
<name><![CDATA[jsd7803-单点登陆]]></name> |
||||
<active>yes</active> |
||||
<version>1.1</version> |
||||
<env-version>10.0</env-version> |
||||
<jartime>2018-07-31</jartime> |
||||
<vendor>mqh</vendor> |
||||
<description><![CDATA[单点登陆jsd7803]]></description> |
||||
<change-notes><![CDATA[单点登陆jsd7803]]></change-notes> |
||||
<main-package>com.fr.plugin.j7803.sso</main-package> |
||||
<prefer-packages> |
||||
<prefer-package>com.fanruan.api</prefer-package> |
||||
</prefer-packages> |
||||
<lifecycle-monitor class="com.fr.plugin.j7803.sso.LifeCycleMonitorImpl"/> |
||||
<extra-core> |
||||
<LocaleFinder class="com.fr.plugin.j7803.sso.LocaleFinder"/> |
||||
</extra-core> |
||||
<extra-decision> |
||||
<GlobalRequestFilterProvider class="com.fr.plugin.j7803.sso.request.OAuth2Login"/> |
||||
</extra-decision> |
||||
<function-recorder class="com.fr.plugin.j7803.sso.LocaleFinder"/> |
||||
</plugin> |
@ -0,0 +1,34 @@
|
||||
/* |
||||
* Copyright (C), 2018-2021 |
||||
* Project: starter |
||||
* FileName: OneAccessLifeCycleMonitor |
||||
* Author: Louis |
||||
* Date: 2021/3/30 15:10 |
||||
*/ |
||||
package com.fr.plugin.j7803.sso; |
||||
|
||||
import com.fr.plugin.context.PluginContext; |
||||
import com.fr.plugin.observer.inner.AbstractPluginLifecycleMonitor; |
||||
import com.fr.plugin.j7803.sso.config.SsoConfig; |
||||
|
||||
/** |
||||
* <Function Description><br> |
||||
* <OneAccessLifeCycleMonitor> |
||||
* |
||||
* @author Louis |
||||
* @since 1.0.0 |
||||
*/ |
||||
public class LifeCycleMonitorImpl extends AbstractPluginLifecycleMonitor { |
||||
public LifeCycleMonitorImpl() { |
||||
} |
||||
|
||||
@Override |
||||
public void afterRun(PluginContext pluginContext) { |
||||
SsoConfig.getInstance(); |
||||
} |
||||
|
||||
@Override |
||||
public void beforeStop(PluginContext pluginContext) { |
||||
|
||||
} |
||||
} |
@ -0,0 +1,37 @@
|
||||
/* |
||||
* Copyright (C), 2018-2020 |
||||
* Project: starter |
||||
* FileName: LocaleFinder |
||||
* Author: Louis |
||||
* Date: 2020/8/31 22:19 |
||||
*/ |
||||
package com.fr.plugin.j7803.sso; |
||||
|
||||
import com.fr.intelli.record.Focus; |
||||
import com.fr.intelli.record.Original; |
||||
import com.fr.record.analyzer.EnableMetrics; |
||||
import com.fr.stable.fun.impl.AbstractLocaleFinder; |
||||
|
||||
import static com.fr.plugin.j7803.sso.config.SsoConfig.PLUGIN_ID; |
||||
|
||||
/** |
||||
* <Function Description><br> |
||||
* <LocaleFinder> |
||||
* |
||||
* @author Louis |
||||
* @since 1.0.0 |
||||
*/ |
||||
@EnableMetrics |
||||
public class LocaleFinder extends AbstractLocaleFinder { |
||||
|
||||
@Override |
||||
@Focus(id = PLUGIN_ID, text = "Plugin-J7803-Sso", source = Original.PLUGIN) |
||||
public String find() { |
||||
return "com/fr/plugin/j7803/sso/locale/lang"; |
||||
} |
||||
|
||||
@Override |
||||
public int currentAPILevel() { |
||||
return CURRENT_LEVEL; |
||||
} |
||||
} |
@ -0,0 +1,80 @@
|
||||
/* |
||||
* Copyright (C), 2018-2021 |
||||
* Project: starter |
||||
* FileName: OneAccessConfig |
||||
* Author: Louis |
||||
* Date: 2021/3/30 9:38 |
||||
*/ |
||||
package com.fr.plugin.j7803.sso.config; |
||||
|
||||
import com.fanruan.api.util.StringKit; |
||||
import com.fr.config.*; |
||||
import com.fr.config.holder.Conf; |
||||
import com.fr.config.holder.factory.Holders; |
||||
import com.fr.intelli.record.Focus; |
||||
import com.fr.intelli.record.Original; |
||||
|
||||
/** |
||||
* <Function Description><br> |
||||
* <SsoConfig> |
||||
* |
||||
* @author Louis |
||||
* @since 1.0.0 |
||||
*/ |
||||
@Visualization(category = "Plugin-J7803-Sso_Group") |
||||
public class SsoConfig extends DefaultConfiguration { |
||||
public static final String PLUGIN_ID = "com.fr.plugin.j7803.sso.auth"; |
||||
public static final String DOMAIN_NAME = "https://openapi.lanxin.cn/oauth2/authorize"; |
||||
public static final String APP_DOMAIN = "https://mcenteruat.huaqin.com/login/appLogin"; |
||||
|
||||
private static volatile SsoConfig config = null; |
||||
|
||||
@Focus(id = PLUGIN_ID, text = "Plugin-J7803-Sso", source = Original.PLUGIN) |
||||
public static SsoConfig getInstance() { |
||||
if (config == null) { |
||||
config = ConfigContext.getConfigInstance(SsoConfig.class); |
||||
} |
||||
return config; |
||||
} |
||||
|
||||
@Identifier(value = "appKey", name = "Plugin-J7803-Sso_Config_appKey", description = "Plugin-J7803-Sso_Config_appKey_Description", status = Status.SHOW) |
||||
private Conf<String> appKey = Holders.simple(StringKit.EMPTY); |
||||
// @Identifier(value = "appSecret", name = "Plugin-J7803-Sso_Config_appSecret", description = "Plugin-J7803-Sso_Config_appSecret_Description", status = Status.SHOW)
|
||||
// private Conf<String> appSecret = Holders.simple(StringKit.EMPTY);
|
||||
@Identifier(value = "domainName", name = "Plugin-J7803-Sso_Config_DomainName", description = "Plugin-J7803-Sso_Config_DomainName_Description", status = Status.SHOW) |
||||
private Conf<String> domainName = Holders.simple(DOMAIN_NAME); |
||||
@Identifier(value = "appDomain", name = "Plugin-J7803-Sso_Config_AppDomain", description = "Plugin-J7803-Sso_Config_AppDomain_Description", status = Status.SHOW) |
||||
private Conf<String> appDomain = Holders.simple(APP_DOMAIN); |
||||
|
||||
public String getAppKey() { |
||||
return appKey.get(); |
||||
} |
||||
|
||||
public void setAppKey(String appKey) { |
||||
this.appKey.set(appKey); |
||||
} |
||||
|
||||
// public String getAppSecret() {
|
||||
// return appSecret.get();
|
||||
// }
|
||||
//
|
||||
// public void setAppSecret(String appSecret) {
|
||||
// this.appSecret.set(appSecret);
|
||||
// }
|
||||
|
||||
public String getDomainName() { |
||||
return domainName.get(); |
||||
} |
||||
|
||||
public void setDomainName(String domainName) { |
||||
this.domainName.set(domainName); |
||||
} |
||||
|
||||
public String getAppDomain() { |
||||
return appDomain.get(); |
||||
} |
||||
|
||||
public void setAppDomain(String appDomain) { |
||||
this.appDomain.set(appDomain); |
||||
} |
||||
} |
@ -0,0 +1,228 @@
|
||||
/* |
||||
* Copyright (C), 2018-2021 |
||||
* Project: starter |
||||
* FileName: OAuth2Login |
||||
* Author: Louis |
||||
* Date: 2021/3/30 22:09 |
||||
*/ |
||||
package com.fr.plugin.j7803.sso.request; |
||||
|
||||
import com.fanruan.api.decision.login.LoginKit; |
||||
import com.fanruan.api.log.LogKit; |
||||
import com.fanruan.api.net.NetworkKit; |
||||
import com.fanruan.api.net.http.HttpKit; |
||||
import com.fanruan.api.util.StringKit; |
||||
import com.fr.decision.fun.impl.AbstractGlobalRequestFilterProvider; |
||||
import com.fr.decision.webservice.utils.DecisionServiceConstants; |
||||
import com.fr.decision.webservice.v10.login.LoginService; |
||||
import com.fr.general.ComparatorUtils; |
||||
import com.fr.json.JSONObject; |
||||
import com.fr.plugin.j7803.sso.config.SsoConfig; |
||||
import com.fr.third.org.apache.http.client.utils.URIBuilder; |
||||
import com.fr.web.utils.WebUtils; |
||||
|
||||
import javax.servlet.FilterChain; |
||||
import javax.servlet.FilterConfig; |
||||
import javax.servlet.http.HttpServletRequest; |
||||
import javax.servlet.http.HttpServletResponse; |
||||
import java.io.UnsupportedEncodingException; |
||||
import java.net.URISyntaxException; |
||||
import java.util.HashMap; |
||||
import java.util.Map; |
||||
|
||||
/** |
||||
* <Function Description><br> |
||||
* <OAuth2Login> |
||||
* |
||||
* @author Louis |
||||
* @since 1.0.0 |
||||
*/ |
||||
public class OAuth2Login extends AbstractGlobalRequestFilterProvider { |
||||
public static final String OP_H5 = "H5"; |
||||
public static final String MOBILE_PATH = "/url/mobile"; |
||||
public static final String REMOTE_DESIGN = "/remote/design"; |
||||
public static final String RESOURCES_PATH = "/resources"; |
||||
public static final String VIEW_FORM = "/view/form"; |
||||
public static final String VIEW_REPORT = "/view/report"; |
||||
public static final String FILE_PATH = "/file"; |
||||
public static final String SYSTEM_INFO = "/system/info"; |
||||
public static final String MATERIALS_MIN_JS_MAP = "/materials.min.js.map"; |
||||
public static final String LOGIN_PATH = "/login"; |
||||
public static final String LOGIN_OTHER = "/login/"; |
||||
public static final String LOGOUT_PATH = "/logout"; |
||||
public static final String USER_LANGUAGE = "/v10/user/language"; |
||||
|
||||
public static final String CODE = "code"; |
||||
|
||||
private SsoConfig config; |
||||
|
||||
/** |
||||
* 过滤器名称 |
||||
* |
||||
* @return |
||||
*/ |
||||
@Override |
||||
public String filterName() { |
||||
return "J7803Filter"; |
||||
} |
||||
|
||||
/** |
||||
* 过滤规则 |
||||
* |
||||
* @return |
||||
*/ |
||||
@Override |
||||
public String[] urlPatterns() { |
||||
return new String[]{"/*"}; |
||||
} |
||||
|
||||
/** |
||||
* 过滤器初始化 |
||||
* |
||||
* @param filterConfig |
||||
*/ |
||||
@Override |
||||
public void init(FilterConfig filterConfig) { |
||||
this.config = SsoConfig.getInstance(); |
||||
super.init(filterConfig); |
||||
} |
||||
|
||||
/** |
||||
* 过滤器处理 |
||||
* |
||||
* @param request |
||||
* @param response |
||||
* @param filterChain |
||||
*/ |
||||
@Override |
||||
public void doFilter(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) { |
||||
try { |
||||
if (operation(request, response)) { |
||||
filterChain.doFilter(request, response); |
||||
} |
||||
} catch (Exception e) { |
||||
LogKit.error(e.getMessage(), e); |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* 用户验证登陆操作 |
||||
* |
||||
* @param req |
||||
* @param res |
||||
* @throws Exception |
||||
*/ |
||||
private boolean operation(HttpServletRequest req, HttpServletResponse res) throws Exception { |
||||
String pathInfo = (req.getPathInfo() != null) ? req.getPathInfo() : StringKit.EMPTY; |
||||
LogKit.info("sso-OAuth2Login-operation-pathInfo:{}", pathInfo); |
||||
if (pathInfo.startsWith(REMOTE_DESIGN) || pathInfo.startsWith(LOGIN_OTHER) |
||||
|| StringKit.equals(LOGIN_PATH, pathInfo) |
||||
|| pathInfo.startsWith(RESOURCES_PATH) || pathInfo.startsWith(LOGOUT_PATH) |
||||
|| pathInfo.startsWith(SYSTEM_INFO) || pathInfo.startsWith(MATERIALS_MIN_JS_MAP) |
||||
|| pathInfo.startsWith(USER_LANGUAGE) || pathInfo.startsWith(FILE_PATH)) { |
||||
return true; |
||||
} |
||||
// 非移动端放行
|
||||
if (!validateMobile(req, pathInfo)) { |
||||
return true; |
||||
} |
||||
// 已登录
|
||||
if (LoginService.getInstance().isLogged(req)) { |
||||
return true; |
||||
} |
||||
String code = NetworkKit.getHTTPRequestParameter(req, CODE); |
||||
LogKit.info("sso-OAuth2Login-operation-code:{}", code); |
||||
if (StringKit.isEmpty(code)) { |
||||
loginPage(req, res); |
||||
return false; |
||||
} |
||||
String username = getUsername(code); |
||||
LogKit.info("sso-OAuth2Login-operation-username:{}", username); |
||||
if (StringKit.isEmpty(username)) { |
||||
return true; |
||||
} |
||||
String tokenFR = LoginKit.login(req, res, username); |
||||
req.setAttribute(DecisionServiceConstants.FINE_AUTH_TOKEN_NAME, tokenFR); |
||||
return true; |
||||
} |
||||
|
||||
/** |
||||
* 验证是否移动端请求 |
||||
* |
||||
* @param req |
||||
* @param pathInfo |
||||
* @return |
||||
*/ |
||||
private Boolean validateMobile(HttpServletRequest req, String pathInfo) { |
||||
String op = WebUtils.getHTTPRequestParameter(req, "op"); |
||||
return (StringKit.isNotEmpty(op) && StringKit.equals(OP_H5, op.toUpperCase())) |
||||
|| WebUtils.getDevice(req).isMobile() |
||||
|| ComparatorUtils.equalsIgnoreCase(MOBILE_PATH, pathInfo); |
||||
} |
||||
|
||||
/** |
||||
* 获取code页面 |
||||
* |
||||
* @param req |
||||
* @param res |
||||
* @return |
||||
* @throws Exception |
||||
*/ |
||||
private void loginPage(HttpServletRequest req, HttpServletResponse res) throws Exception { |
||||
Map<String, String> params = new HashMap<>(); |
||||
params.put("appid", this.config.getAppKey()); |
||||
params.put("response_type", "code"); |
||||
params.put("scope", "basic_userinfor"); |
||||
params.put("state", "sso"); |
||||
params.put("redirect_uri", req.getRequestURL().toString()); |
||||
String loginUrl = buildUrl(this.config.getDomainName(), params); |
||||
LogKit.info("sso-OAuth2Login-redirectToLoginPage-loginUrl:{}", loginUrl); |
||||
res.sendRedirect(loginUrl); |
||||
} |
||||
|
||||
/** |
||||
* 通过凭证获得username |
||||
* |
||||
* @param accessToken |
||||
* @return |
||||
*/ |
||||
private String getUsername(String accessToken) throws Exception { |
||||
Map<String, String> headers = new HashMap<>(); |
||||
headers.put("origin", "finebi.huaqin.com"); |
||||
Map<String, String> userInfoParams = new HashMap<>(); |
||||
userInfoParams.put("appId", this.config.getAppKey()); |
||||
userInfoParams.put("code", accessToken); |
||||
userInfoParams.put("env", "lx"); |
||||
LogKit.info("sso-OAuth2Login-getUsername-userInfoParams:{}, headers:{}", userInfoParams, headers); |
||||
String userRes = HttpKit.get(this.config.getAppDomain(), userInfoParams, headers); |
||||
LogKit.info("sso-OAuth2Login-getUsername-userRes:{}", userRes); |
||||
return new JSONObject(userRes).getJSONObject("data").getString("empNo"); |
||||
} |
||||
|
||||
/** |
||||
* 构建 Url |
||||
* |
||||
* @param url 请求地址 |
||||
* @param params 参数 |
||||
* @return 拼接之后的地址 |
||||
* @throws UnsupportedEncodingException 不支持的编码 |
||||
*/ |
||||
private String buildUrl(String url, Map<String, String> params) throws UnsupportedEncodingException { |
||||
if (params == null || params.isEmpty()) { |
||||
return url; |
||||
} |
||||
URIBuilder builder; |
||||
try { |
||||
builder = new URIBuilder(url); |
||||
for (Map.Entry<String, String> entry : params.entrySet()) { |
||||
String key = entry.getKey(); |
||||
String value = entry.getValue(); |
||||
builder.setParameter(key, value); |
||||
} |
||||
return builder.build().toString(); |
||||
} catch (URISyntaxException e) { |
||||
LogKit.debug("Error to build url, please check the arguments."); |
||||
} |
||||
return url; |
||||
} |
||||
} |
@ -0,0 +1,10 @@
|
||||
Plugin-J7803-Sso=Sso Plugin |
||||
Plugin-J7803-Sso_Group=Sso Plugin |
||||
Plugin-J7803-Sso_Config_appKey=App ID |
||||
Plugin-J7803-Sso_Config_appKey_Description=App ID |
||||
Plugin-J7803-Sso_Config_appSecret=App Secret |
||||
Plugin-J7803-Sso_Config_appSecret_Description=App Secret |
||||
Plugin-J7803-Sso_Config_DomainName=lx code url |
||||
Plugin-J7803-Sso_Config_DomainName_Description=lx code url |
||||
Plugin-J7803-Sso_Config_AppDomain=appLogin url |
||||
Plugin-J7803-Sso_Config_AppDomain_Description=appLogin url |
@ -0,0 +1,10 @@
|
||||
Plugin-J7803-Sso=\u5355\u70B9\u767B\u9646\u63D2\u4EF6 |
||||
Plugin-J7803-Sso_Group=\u5355\u70B9\u767B\u9646\u63D2\u4EF6 |
||||
Plugin-J7803-Sso_Config_appKey=App ID |
||||
Plugin-J7803-Sso_Config_appKey_Description=App ID |
||||
Plugin-J7803-Sso_Config_appSecret=App Secret |
||||
Plugin-J7803-Sso_Config_appSecret_Description=App Secret |
||||
Plugin-J7803-Sso_Config_DomainName=\u84DD\u4FE1\u6388\u6743\u63A5\u53E3 |
||||
Plugin-J7803-Sso_Config_DomainName_Description=\u84DD\u4FE1\u6388\u6743\u63A5\u53E3 |
||||
Plugin-J7803-Sso_Config_AppDomain=\u767B\u9646\u9274\u6743\u63A5\u53E3 |
||||
Plugin-J7803-Sso_Config_AppDomain_Description=\u767B\u9646\u9274\u6743\u63A5\u53E3 |
Loading…
Reference in new issue