commit
98647e4fa9
13 changed files with 947 additions and 0 deletions
@ -0,0 +1,6 @@ |
|||||||
|
# open-JSD-9584 |
||||||
|
|
||||||
|
JSD-9584 oauth2单点对接\ |
||||||
|
免责说明:该源码为第三方爱好者提供,不保证源码和方案的可靠性,也不提供任何形式的源码教学指导和协助!\ |
||||||
|
仅作为开发者学习参考使用!禁止用于任何商业用途!\ |
||||||
|
为保护开发者隐私,开发者信息已隐去!若原开发者希望公开自己的信息,可联系【pioneer】处理。 |
Binary file not shown.
@ -0,0 +1,20 @@ |
|||||||
|
package com.fr.plugin.xxx.xxx.sso; |
||||||
|
|
||||||
|
import com.fr.decision.fun.impl.AbstractLogInOutEventProvider; |
||||||
|
import com.fr.decision.webservice.login.LogInOutResultInfo; |
||||||
|
import com.fr.plugin.xxx.xxx.sso.util.LogUtils; |
||||||
|
|
||||||
|
/** |
||||||
|
* @Author hujian |
||||||
|
* @Date 2020/9/18 |
||||||
|
* @Description |
||||||
|
**/ |
||||||
|
public class CustomLogInOutEventProvider extends AbstractLogInOutEventProvider { |
||||||
|
|
||||||
|
@Override |
||||||
|
public String logoutAction(LogInOutResultInfo result) { |
||||||
|
String logout = JianfaConfig.getInstance().getLogout(); |
||||||
|
LogUtils.debug4plugin("logout url is {}", logout); |
||||||
|
return logout; |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,187 @@ |
|||||||
|
package com.fr.plugin.xxx.xxx.sso; |
||||||
|
|
||||||
|
import com.fr.config.*; |
||||||
|
import com.fr.config.holder.Conf; |
||||||
|
import com.fr.config.holder.factory.Holders; |
||||||
|
import com.fr.stable.StringUtils; |
||||||
|
|
||||||
|
/** |
||||||
|
* @Author xxx |
||||||
|
* @Date 2022/3/3 |
||||||
|
* @Description |
||||||
|
**/ |
||||||
|
@Visualization(category = "单点配置") |
||||||
|
public class xxxConfig extends DefaultConfiguration { |
||||||
|
|
||||||
|
private static volatile xxxConfig config = null; |
||||||
|
|
||||||
|
public static xxxConfig getInstance() { |
||||||
|
if (config == null) { |
||||||
|
config = ConfigContext.getConfigInstance(xxxConfig.class); |
||||||
|
} |
||||||
|
return config; |
||||||
|
} |
||||||
|
|
||||||
|
@Identifier(value = "debugSwitch", name = "插件调试开关", description = "日志调试模式", status = Status.SHOW) |
||||||
|
private Conf<Boolean> debugSwitch = Holders.simple(true); |
||||||
|
|
||||||
|
public Boolean getDebugSwitch() { |
||||||
|
return this.debugSwitch.get(); |
||||||
|
} |
||||||
|
|
||||||
|
public void setDebugSwitch(Boolean debugSwitch) { |
||||||
|
this.debugSwitch.set(debugSwitch); |
||||||
|
} |
||||||
|
|
||||||
|
@Identifier(value = "timeout", name = "授权超时时间", description = "授权超时时间,单位分钟", status = Status.SHOW) |
||||||
|
private Conf<Integer> timeout = Holders.simple(5); |
||||||
|
|
||||||
|
public Integer getTimeout() { |
||||||
|
return timeout.get(); |
||||||
|
} |
||||||
|
|
||||||
|
public void setTimeout(Integer timeout) { |
||||||
|
this.timeout.set(timeout); |
||||||
|
} |
||||||
|
@Identifier(value = "key", name = "解密秘钥", description = "解密秘钥", status = Status.SHOW) |
||||||
|
private Conf<String> key = Holders.simple(StringUtils.EMPTY); |
||||||
|
|
||||||
|
public String getKey() { |
||||||
|
return key.get(); |
||||||
|
} |
||||||
|
|
||||||
|
public void setKey(String key) { |
||||||
|
this.key.set(key); |
||||||
|
} |
||||||
|
|
||||||
|
@Identifier(value = "loginUrl", name = "跳转登录地址", description = "跳转登录地址", status = Status.SHOW) |
||||||
|
private Conf<String> loginUrl = Holders.simple(StringUtils.EMPTY); |
||||||
|
|
||||||
|
public String getLoginUrl() { |
||||||
|
return loginUrl.get(); |
||||||
|
} |
||||||
|
|
||||||
|
public void setLoginUrl(String loginUrl) { |
||||||
|
this.loginUrl.set(loginUrl); |
||||||
|
} |
||||||
|
|
||||||
|
@Identifier(value = "clientId", name = "clientId", description = "服务端预设的访问代码,即应用名称", status = Status.SHOW) |
||||||
|
private Conf<String> clientId = Holders.simple(StringUtils.EMPTY); |
||||||
|
|
||||||
|
public String getClientId() { |
||||||
|
return clientId.get(); |
||||||
|
} |
||||||
|
|
||||||
|
public void setClientId(String clientId) { |
||||||
|
this.clientId.set(clientId); |
||||||
|
} |
||||||
|
|
||||||
|
@Identifier(value = "clientSecret", name = "clientSecret", description = "服务端预设的访问密钥", status = Status.SHOW) |
||||||
|
private Conf<String> clientSecret = Holders.simple(StringUtils.EMPTY); |
||||||
|
|
||||||
|
public String getClientSecret() { |
||||||
|
return clientSecret.get(); |
||||||
|
} |
||||||
|
|
||||||
|
public void setClientSecret(String clientSecret) { |
||||||
|
this.clientSecret.set(clientSecret); |
||||||
|
} |
||||||
|
|
||||||
|
@Identifier(value = "grantType", name = "grant_type", description = "grant_type", status = Status.SHOW) |
||||||
|
private Conf<String> grantType = Holders.simple("authorization_cod"); |
||||||
|
|
||||||
|
public String getGrantType() { |
||||||
|
return grantType.get(); |
||||||
|
} |
||||||
|
|
||||||
|
public void setGrantType(String grantType) { |
||||||
|
this.grantType.set(grantType); |
||||||
|
} |
||||||
|
|
||||||
|
@Identifier(value = "responseType", name = "response_type", description = "response_type", status = Status.SHOW) |
||||||
|
private Conf<String> responseType = Holders.simple("code"); |
||||||
|
|
||||||
|
public String getResponseType() { |
||||||
|
return responseType.get(); |
||||||
|
} |
||||||
|
|
||||||
|
public void setResponseType(String responseType) { |
||||||
|
this.responseType.set(responseType); |
||||||
|
} |
||||||
|
|
||||||
|
@Identifier(value = "accessUrl", name = "accessUrl", description = "换取access_token的地址", status = Status.SHOW) |
||||||
|
private Conf<String> accessUrl = Holders.simple(StringUtils.EMPTY); |
||||||
|
|
||||||
|
public String getAccessUrl() { |
||||||
|
return accessUrl.get(); |
||||||
|
} |
||||||
|
|
||||||
|
public void setAccessUrl(String accessUrl) { |
||||||
|
this.accessUrl.set(accessUrl); |
||||||
|
} |
||||||
|
|
||||||
|
@Identifier(value = "profileUrl", name = "profileUrl", description = "换取用户信息的地址", status = Status.SHOW) |
||||||
|
private Conf<String> profileUrl = Holders.simple(StringUtils.EMPTY); |
||||||
|
|
||||||
|
public String getProfileUrl() { |
||||||
|
return profileUrl.get(); |
||||||
|
} |
||||||
|
|
||||||
|
public void setProfileUrl(String profileUrl) { |
||||||
|
this.profileUrl.set(profileUrl); |
||||||
|
} |
||||||
|
|
||||||
|
@Identifier(value = "redirectUrl", name = "回调地址", description = "回调地址,默认配置的回调地址", status = Status.SHOW) |
||||||
|
private Conf<String> redirectUrl = Holders.simple(StringUtils.EMPTY); |
||||||
|
|
||||||
|
public String getRedirectUrl() { |
||||||
|
return redirectUrl.get(); |
||||||
|
} |
||||||
|
|
||||||
|
public void setRedirectUrl(String redirectUrl) { |
||||||
|
this.redirectUrl.set(redirectUrl); |
||||||
|
} |
||||||
|
|
||||||
|
@Identifier(value = "logout", name = "登出地址", description = "登出地址", status = Status.SHOW) |
||||||
|
private Conf<String> logout = Holders.simple(StringUtils.EMPTY); |
||||||
|
|
||||||
|
public String getLogout() { |
||||||
|
return logout.get(); |
||||||
|
} |
||||||
|
|
||||||
|
public void setLogout(String logout) { |
||||||
|
this.logout.set(logout); |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@Override |
||||||
|
public Object clone() throws CloneNotSupportedException { |
||||||
|
xxxConfig cloned = (xxxConfig) super.clone(); |
||||||
|
cloned.debugSwitch = (Conf<Boolean>) debugSwitch.clone(); |
||||||
|
cloned.timeout = (Conf<Integer>) timeout.clone(); |
||||||
|
cloned.key = (Conf<String>) key.clone(); |
||||||
|
cloned.loginUrl = (Conf<String>) loginUrl.clone(); |
||||||
|
cloned.clientId = (Conf<String>) clientId.clone(); |
||||||
|
cloned.clientSecret = (Conf<String>) clientSecret.clone(); |
||||||
|
cloned.grantType = (Conf<String>) grantType.clone(); |
||||||
|
cloned.responseType = (Conf<String>) responseType.clone(); |
||||||
|
cloned.accessUrl = (Conf<String>) accessUrl.clone(); |
||||||
|
cloned.profileUrl = (Conf<String>) profileUrl.clone(); |
||||||
|
cloned.redirectUrl = (Conf<String>) redirectUrl.clone(); |
||||||
|
cloned.logout = (Conf<String>) logout.clone(); |
||||||
|
return cloned; |
||||||
|
} |
||||||
|
|
||||||
|
public boolean isTokenConfig(){ |
||||||
|
return StringUtils.isNotBlank(getKey()) && timeout != null; |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
public boolean isConfiged() { |
||||||
|
return StringUtils.isNotBlank(getLoginUrl()) && StringUtils.isNotBlank(getClientId()) |
||||||
|
&& StringUtils.isNotBlank(getClientSecret()) && StringUtils.isNotBlank(getGrantType()) |
||||||
|
&& StringUtils.isNotBlank(getResponseType()) && StringUtils.isNotBlank(getAccessUrl()) |
||||||
|
&& StringUtils.isNotBlank(getProfileUrl()) && StringUtils.isNotBlank(getRedirectUrl()); |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,27 @@ |
|||||||
|
package com.fr.plugin.xxx.xxx.sso; |
||||||
|
|
||||||
|
import com.fr.plugin.context.PluginContext; |
||||||
|
import com.fr.plugin.observer.inner.AbstractPluginLifecycleMonitor; |
||||||
|
|
||||||
|
/** |
||||||
|
* 配置信息初始化 |
||||||
|
*/ |
||||||
|
|
||||||
|
public class LRGT extends AbstractPluginLifecycleMonitor { |
||||||
|
@Override |
||||||
|
public void afterRun(PluginContext pluginContext) { |
||||||
|
xxxConfig.getInstance(); |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public void beforeStop(PluginContext pluginContext) { |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public void beforeUninstall(PluginContext pluginContext) { |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public void afterInstall(PluginContext var1) { |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,77 @@ |
|||||||
|
package com.fr.plugin.xxx.xxx.sso; |
||||||
|
|
||||||
|
import com.fr.decision.fun.impl.AbstractGlobalRequestFilterProvider; |
||||||
|
import com.fr.decision.webservice.bean.authentication.OriginUrlResponseBean; |
||||||
|
import com.fr.decision.webservice.utils.DecisionStatusService; |
||||||
|
import com.fr.plugin.xxx.xxx.sso.util.CommonUtils; |
||||||
|
import com.fr.plugin.xxx.xxx.sso.util.LogUtils; |
||||||
|
import com.fr.stable.StringUtils; |
||||||
|
import com.fr.web.utils.WebUtils; |
||||||
|
|
||||||
|
import javax.servlet.FilterChain; |
||||||
|
import javax.servlet.http.HttpServletRequest; |
||||||
|
import javax.servlet.http.HttpServletResponse; |
||||||
|
import java.io.UnsupportedEncodingException; |
||||||
|
import java.net.URLEncoder; |
||||||
|
|
||||||
|
import static com.fr.plugin.xxx.xxx.sso.util.CommonUtils.cacheParam; |
||||||
|
|
||||||
|
/** |
||||||
|
* @Author xxx |
||||||
|
* @Date 2022/3/3 |
||||||
|
* @Description |
||||||
|
**/ |
||||||
|
public class LoginFilter extends AbstractGlobalRequestFilterProvider { |
||||||
|
@Override |
||||||
|
public String filterName() { |
||||||
|
return "loginFilter"; |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public String[] urlPatterns() { |
||||||
|
return new String[]{"/decision/login"}; |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public void doFilter(HttpServletRequest req, HttpServletResponse res, FilterChain filterChain) { |
||||||
|
xxxConfig config = xxxConfig.getInstance(); |
||||||
|
if (CommonUtils.isLogin(req) || !config.isConfiged()) { |
||||||
|
CommonUtils.next(req, res, filterChain); |
||||||
|
return; |
||||||
|
} |
||||||
|
String origin = WebUtils.getHTTPRequestParameter(req, "origin"); |
||||||
|
if ("get".equalsIgnoreCase(req.getMethod())) { |
||||||
|
try { |
||||||
|
if (StringUtils.isNotBlank(origin)) { |
||||||
|
OriginUrlResponseBean path = DecisionStatusService.originUrlStatusService().get(origin); |
||||||
|
if(path != null){ |
||||||
|
String id = cacheParam(res,path.getOriginUrl()); |
||||||
|
LogUtils.debug4plugin("Redirect origin is {}", req.getRequestURL()); |
||||||
|
res.sendRedirect(buildAuthorizeUrl(req,config.getRedirectUrl(),id)); |
||||||
|
return; |
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
|
} catch (Exception e) { |
||||||
|
LogUtils.error(e.getMessage(), e); |
||||||
|
} |
||||||
|
} |
||||||
|
CommonUtils.next(req, res, filterChain); |
||||||
|
} |
||||||
|
|
||||||
|
protected String buildAuthorizeUrl(HttpServletRequest req, String origin, String id) { |
||||||
|
xxxConfig config = xxxConfig.getInstance(); |
||||||
|
String loginUrl = config.getLoginUrl(); |
||||||
|
String clientId = config.getClientId(); |
||||||
|
String responseType = config.getResponseType(); |
||||||
|
String url = null; |
||||||
|
try { |
||||||
|
url = String.format("%s?client_id=%s&response_type=%s&redirect_uri=%s", loginUrl, clientId, responseType, URLEncoder.encode(origin, "UTF-8")); |
||||||
|
} catch (UnsupportedEncodingException e) { |
||||||
|
LogUtils.error(e.getMessage(), e); |
||||||
|
} |
||||||
|
LogUtils.debug4plugin("login page url: {}", url); |
||||||
|
return url; |
||||||
|
} |
||||||
|
|
||||||
|
} |
@ -0,0 +1,13 @@ |
|||||||
|
package com.fr.plugin.xxx.xxx.sso; |
||||||
|
|
||||||
|
/** |
||||||
|
* @author xxx |
||||||
|
* @since 2021/12/04 |
||||||
|
*/ |
||||||
|
public class PluginConstants { |
||||||
|
|
||||||
|
public static final String PLUGIN_ID = "com.fr.plugin.xxx.xxx.sso"; |
||||||
|
|
||||||
|
public static final String PLUGIN_NAME = "单点登录11.0"; |
||||||
|
|
||||||
|
} |
@ -0,0 +1,73 @@ |
|||||||
|
package com.fr.plugin.xxx.xxx.sso; |
||||||
|
|
||||||
|
import com.fr.plugin.context.PluginContexts; |
||||||
|
import com.fr.plugin.xxx.xxx.sso.util.CommonUtils; |
||||||
|
import com.fr.plugin.xxx.xxx.sso.util.HttpUtil; |
||||||
|
import com.fr.plugin.xxx.xxx.sso.util.LogUtils; |
||||||
|
import com.fr.plugin.transform.FunctionRecorder; |
||||||
|
import com.fr.stable.StringUtils; |
||||||
|
import com.fr.stable.fun.Authorize; |
||||||
|
import com.fr.web.utils.WebUtils; |
||||||
|
|
||||||
|
import javax.servlet.FilterChain; |
||||||
|
import javax.servlet.http.HttpServletRequest; |
||||||
|
import javax.servlet.http.HttpServletResponse; |
||||||
|
|
||||||
|
/** |
||||||
|
* @Author xxx |
||||||
|
* @Date 2022/3/3 |
||||||
|
* @Description |
||||||
|
**/ |
||||||
|
@FunctionRecorder |
||||||
|
@Authorize(callSignKey = PluginConstants.PLUGIN_ID) |
||||||
|
public class SsoFilter extends LoginFilter { |
||||||
|
@Override |
||||||
|
public String filterName() { |
||||||
|
return "xxx"; |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public String[] urlPatterns() { |
||||||
|
if (PluginContexts.currentContext().isAvailable()) { |
||||||
|
return new String[]{ |
||||||
|
"/decision", |
||||||
|
"/decision/view/report", |
||||||
|
"/decision/view/form", |
||||||
|
}; |
||||||
|
} else { |
||||||
|
return new String[]{"/qqqqq"}; |
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public void doFilter(HttpServletRequest req, HttpServletResponse res, FilterChain filterChain) { |
||||||
|
xxxConfig config = xxxConfig.getInstance(); |
||||||
|
if (CommonUtils.isLogin(req) || !config.isConfiged()) { |
||||||
|
CommonUtils.next(req, res, filterChain); |
||||||
|
return; |
||||||
|
} |
||||||
|
String code = WebUtils.getHTTPRequestParameter(req, "code"); |
||||||
|
if (StringUtils.isNotBlank(code)) { |
||||||
|
String username = HttpUtil.getUsername(config.getAccessUrl(), config.getProfileUrl() |
||||||
|
, config.getClientId(), config.getClientSecret() |
||||||
|
, config.getGrantType(), config.getRedirectUrl(), code); |
||||||
|
LogUtils.debug4plugin("code {} get username is {}",username); |
||||||
|
try { |
||||||
|
CommonUtils.login(username,req,res); |
||||||
|
String origin = CommonUtils.getOrigin(req); |
||||||
|
if (StringUtils.isNotBlank(origin)) { |
||||||
|
res.sendRedirect(origin); |
||||||
|
return; |
||||||
|
} |
||||||
|
} catch (Exception e) { |
||||||
|
CommonUtils.setError(res,"用户不存在"); |
||||||
|
return; |
||||||
|
} |
||||||
|
} |
||||||
|
CommonUtils.next(req, res, filterChain); |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
} |
@ -0,0 +1,64 @@ |
|||||||
|
package com.fr.plugin.xxx.xxx.sso; |
||||||
|
|
||||||
|
import com.fr.decision.fun.impl.AbstractGlobalRequestFilterProvider; |
||||||
|
import com.fr.plugin.xxx.xxx.sso.util.CommonUtils; |
||||||
|
import com.fr.plugin.xxx.xxx.sso.util.LogUtils; |
||||||
|
import com.fr.plugin.xxx.xxx.sso.util.Sha256Util; |
||||||
|
import com.fr.stable.StringUtils; |
||||||
|
import com.fr.web.utils.WebUtils; |
||||||
|
|
||||||
|
import javax.servlet.FilterChain; |
||||||
|
import javax.servlet.http.HttpServletRequest; |
||||||
|
import javax.servlet.http.HttpServletResponse; |
||||||
|
import java.time.Instant; |
||||||
|
|
||||||
|
/** |
||||||
|
* @Author xxx |
||||||
|
* @Date 2022/3/3 |
||||||
|
* @Description |
||||||
|
**/ |
||||||
|
public class TokenFilter extends AbstractGlobalRequestFilterProvider { |
||||||
|
@Override |
||||||
|
public String filterName() { |
||||||
|
return "xxx"; |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public String[] urlPatterns() { |
||||||
|
return new String[]{"/decision/url/mobile"}; |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public void doFilter(HttpServletRequest req, HttpServletResponse res, FilterChain filterChain) { |
||||||
|
String token = WebUtils.getHTTPRequestParameter(req, "token"); |
||||||
|
if (StringUtils.isNotBlank(token) && xxxConfig.getInstance().isTokenConfig()) { |
||||||
|
String username = getUsername(token, xxxConfig.getInstance().getKey()); |
||||||
|
if (StringUtils.isBlank(username)) { |
||||||
|
CommonUtils.setError(res, "用户解析失败"); |
||||||
|
return; |
||||||
|
} |
||||||
|
try { |
||||||
|
CommonUtils.login(username, req, res); |
||||||
|
} catch (Exception e) { |
||||||
|
CommonUtils.setError(res, e.getMessage()); |
||||||
|
return; |
||||||
|
} |
||||||
|
} |
||||||
|
CommonUtils.next(req, res, filterChain); |
||||||
|
} |
||||||
|
|
||||||
|
private String getUsername(String token, String key) { |
||||||
|
Integer timeout = xxxConfig.getInstance().getTimeout(); |
||||||
|
String[] arr = token.split("_"); |
||||||
|
if (arr.length != 3) { |
||||||
|
return StringUtils.EMPTY; |
||||||
|
} |
||||||
|
String sign = Sha256Util.getSHA256(String.format("%s_%s_%s", arr[1], arr[2], key)); |
||||||
|
LogUtils.debug4plugin("calculate signature is {},current time is {}", sign, Instant.now().toEpochMilli()); |
||||||
|
if (StringUtils.equals(sign, arr[0]) && Instant.now().toEpochMilli() - Long.valueOf(arr[2]) < timeout * 60) { |
||||||
|
return arr[1]; |
||||||
|
} |
||||||
|
LogUtils.warn("current request time out or signature does not match!"); |
||||||
|
return StringUtils.EMPTY; |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,183 @@ |
|||||||
|
package com.fr.plugin.xxx.xxx.sso.util; |
||||||
|
|
||||||
|
import com.fr.data.NetworkHelper; |
||||||
|
import com.fr.decision.authority.data.User; |
||||||
|
import com.fr.decision.mobile.terminal.TerminalHandler; |
||||||
|
import com.fr.decision.webservice.bean.authentication.OriginUrlResponseBean; |
||||||
|
import com.fr.decision.webservice.utils.DecisionServiceConstants; |
||||||
|
import com.fr.decision.webservice.utils.DecisionStatusService; |
||||||
|
import com.fr.decision.webservice.utils.WebServiceUtils; |
||||||
|
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.locale.InterProviderFactory; |
||||||
|
import com.fr.log.FineLoggerFactory; |
||||||
|
import com.fr.stable.StringUtils; |
||||||
|
import com.fr.stable.web.Device; |
||||||
|
import com.fr.web.utils.WebUtils; |
||||||
|
|
||||||
|
import javax.servlet.FilterChain; |
||||||
|
import javax.servlet.http.Cookie; |
||||||
|
import javax.servlet.http.HttpServletRequest; |
||||||
|
import javax.servlet.http.HttpServletResponse; |
||||||
|
import java.io.PrintWriter; |
||||||
|
import java.util.HashMap; |
||||||
|
import java.util.Map; |
||||||
|
import java.util.Properties; |
||||||
|
import java.util.UUID; |
||||||
|
|
||||||
|
/** |
||||||
|
* @author xxxx |
||||||
|
* @since 2021/8/24 |
||||||
|
*/ |
||||||
|
public class CommonUtils { |
||||||
|
|
||||||
|
public static String getProperty(Properties props, String key, String defaultValue, boolean allowBlank) { |
||||||
|
String value = props.getProperty(key); |
||||||
|
if (StringUtils.isNotBlank(value)) { |
||||||
|
return value; |
||||||
|
} else { |
||||||
|
if (allowBlank) { |
||||||
|
LogUtils.warn("Property[" + key + "] value is blank."); |
||||||
|
return defaultValue; |
||||||
|
} else { |
||||||
|
throw new IllegalArgumentException("Property[" + key + "] cann't be blank."); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
public static String getProperty(Properties props, String key, boolean allowBlank) { |
||||||
|
return getProperty(props, key, null, allowBlank); |
||||||
|
} |
||||||
|
|
||||||
|
public static String getProperty(Properties props, String key) { |
||||||
|
return getProperty(props, key, null, true); |
||||||
|
} |
||||||
|
|
||||||
|
public static boolean isLogin(HttpServletRequest request) { |
||||||
|
String oldToken = TokenResource.COOKIE.getToken(request); |
||||||
|
return oldToken != null && checkTokenValid(request, (String) oldToken); |
||||||
|
} |
||||||
|
|
||||||
|
private static 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; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 跳转到过滤器链中的下一个过滤器 |
||||||
|
* |
||||||
|
* @param request |
||||||
|
* @param response |
||||||
|
* @param chain |
||||||
|
*/ |
||||||
|
public static void next(HttpServletRequest request, HttpServletResponse response, FilterChain chain) { |
||||||
|
try { |
||||||
|
chain.doFilter(request, response); |
||||||
|
} catch (Exception e) { |
||||||
|
FineLoggerFactory.getLogger().error(e.getMessage(), e); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
public static void login(String username, HttpServletRequest request, HttpServletResponse response) throws Exception { |
||||||
|
User user = UserService.getInstance().getUserByUserName(username); |
||||||
|
if (user == null) { |
||||||
|
throw new Exception(String.format("%s用户不存在", username)); |
||||||
|
} |
||||||
|
String token = LoginService.getInstance().login(request, response, user.getUserName()); |
||||||
|
request.setAttribute(DecisionServiceConstants.FINE_AUTH_TOKEN_NAME, token); |
||||||
|
} |
||||||
|
|
||||||
|
public static boolean isMobileDevice(HttpServletRequest request) { |
||||||
|
if (WebUtils.getDevice(request).isMobile()) { |
||||||
|
LogUtils.debug4plugin("current request is is mobile request ,url is {}", request.getRequestURI()); |
||||||
|
return true; |
||||||
|
} |
||||||
|
String requestHeader = request.getHeader("user-agent"); |
||||||
|
String[] deviceArray = new String[]{"android", "iphone", "ipad", "ios", "windows phone", "wechat"}; |
||||||
|
if (requestHeader == null) { |
||||||
|
return false; |
||||||
|
} |
||||||
|
requestHeader = requestHeader.toLowerCase(); |
||||||
|
for (int i = 0; i < deviceArray.length; i++) { |
||||||
|
if (requestHeader.toLowerCase().contains(deviceArray[i])) { |
||||||
|
LogUtils.debug4plugin("current request:{} is mobile request!", request.getRequestURI()); |
||||||
|
return true; |
||||||
|
} |
||||||
|
} |
||||||
|
String op = WebUtils.getHTTPRequestParameter(request, "op"); |
||||||
|
return StringUtils.isNotBlank(op) && StringUtils.equals("h5", op); |
||||||
|
} |
||||||
|
|
||||||
|
public static void cacheParams(String key, Map<String, String> values) { |
||||||
|
try { |
||||||
|
DecisionStatusService.originUrlStatusService().put(key, values); |
||||||
|
} catch (Exception e) { |
||||||
|
throw new RuntimeException(e); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
public static String cacheParam(HttpServletResponse res, String jump) { |
||||||
|
String id = UUID.randomUUID().toString(); |
||||||
|
try { |
||||||
|
DecisionStatusService.originUrlStatusService().put(id, new OriginUrlResponseBean(jump));//添加重定向地址
|
||||||
|
} catch (Exception e) { |
||||||
|
FineLoggerFactory.getLogger().error(e.getMessage(), e); |
||||||
|
} |
||||||
|
Cookie cookie = new Cookie("ORIGIN_URL", id); |
||||||
|
cookie.setPath("/"); |
||||||
|
cookie.setMaxAge(60); |
||||||
|
//cookie.setHttpOnly(true);
|
||||||
|
res.addCookie(cookie); |
||||||
|
return id; |
||||||
|
} |
||||||
|
|
||||||
|
public static String getOrigin(HttpServletRequest req) throws Exception { |
||||||
|
Cookie[] cookies = req.getCookies(); |
||||||
|
if (cookies == null) { |
||||||
|
return null; |
||||||
|
} |
||||||
|
for (int i = 0; i < cookies.length; i++) { |
||||||
|
if ("ORIGIN_URL".equals(cookies[i].getName())) { |
||||||
|
String id = cookies[i].getValue(); |
||||||
|
OriginUrlResponseBean path = DecisionStatusService.originUrlStatusService().get(id); |
||||||
|
if (path != null) { |
||||||
|
DecisionStatusService.originUrlStatusService().delete(id); |
||||||
|
return path.getOriginUrl(); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
return StringUtils.EMPTY; |
||||||
|
} |
||||||
|
|
||||||
|
public static String getCachedParam(String key, String name) { |
||||||
|
try { |
||||||
|
Map<String, String> values = DecisionStatusService.originUrlStatusService().get(key); |
||||||
|
return values.get(name); |
||||||
|
} catch (Exception e) { |
||||||
|
throw new RuntimeException(e); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
public static 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); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
} |
@ -0,0 +1,119 @@ |
|||||||
|
package com.fr.plugin.xxx.xxx.sso.util; |
||||||
|
|
||||||
|
import com.fr.json.JSONArray; |
||||||
|
import com.fr.json.JSONObject; |
||||||
|
import com.fr.stable.StringUtils; |
||||||
|
|
||||||
|
import java.io.BufferedReader; |
||||||
|
import java.io.IOException; |
||||||
|
import java.io.InputStreamReader; |
||||||
|
import java.io.PrintWriter; |
||||||
|
import java.net.URL; |
||||||
|
import java.net.URLConnection; |
||||||
|
|
||||||
|
/** |
||||||
|
* @Author xxx |
||||||
|
* @Date 2020/7/8 |
||||||
|
* @Description 这个类的所有代码都是根据用户的示例代码中获得,未做修改,不做代码验证 |
||||||
|
**/ |
||||||
|
public class HttpUtil { |
||||||
|
|
||||||
|
private static final String UID = "uid"; |
||||||
|
|
||||||
|
private static String PARAMS = "client_id=CLIENT_ID&client_secret=CLIENT_SECRET&grant_type=GRANTTYPE&redirect_uri=REDIRECTURL&code=CODE"; |
||||||
|
|
||||||
|
|
||||||
|
public static String getUsername(String accessUrl ,String profileUrl ,String clientId ,String clientSec ,String grantType ,String redirectUrl,String code){ |
||||||
|
String username = StringUtils.EMPTY; |
||||||
|
String replacedParams = PARAMS.replace("CLIENT_ID", clientId) |
||||||
|
.replace("CLIENT_SECRET", clientSec) |
||||||
|
.replace("GRANTTYPE", grantType) |
||||||
|
.replace("REDIRECTURL", redirectUrl) |
||||||
|
.replace("CODE", code); |
||||||
|
// 整合客户端及服务端参数,构建获取AccessToken的URL
|
||||||
|
// client_id:服务端预设的访问代码,即应用名称
|
||||||
|
// client_secret:服务端预设的访问密钥
|
||||||
|
// grant_type:授权类型使用“authorization_code”
|
||||||
|
// redirect_uri:回调地址
|
||||||
|
// code:服务器端颁发的ST授权码
|
||||||
|
LogUtils.debug4plugin("access token url is {} param is {}",accessUrl,replacedParams); |
||||||
|
String access_token_str = sendPost(accessUrl, replacedParams); |
||||||
|
LogUtils.debug4plugin("AccessToken:" + access_token_str); |
||||||
|
// 取得有效的AccessToken值
|
||||||
|
// access_token:访问令牌
|
||||||
|
// expires:过期时间
|
||||||
|
String access_token = access_token_str.split("&")[0]; |
||||||
|
|
||||||
|
// 使用AccessToken参数构建URL,获取认证成功后返回的信息,其中信息是以Json格式提供
|
||||||
|
LogUtils.debug4plugin("profile url is {} access token is {}",profileUrl,access_token); |
||||||
|
String principal_json = sendPost(profileUrl, access_token); |
||||||
|
|
||||||
|
LogUtils.debug4plugin("JSON:" + principal_json); |
||||||
|
|
||||||
|
// 以下示例为解析Json获取所需信息
|
||||||
|
JSONObject jsonObj = new JSONObject(principal_json); |
||||||
|
|
||||||
|
if (jsonObj.has("id")) { |
||||||
|
LogUtils.debug4plugin("ID:" + jsonObj.get("id")); |
||||||
|
} |
||||||
|
|
||||||
|
if (jsonObj.has("attributes")) { |
||||||
|
JSONArray array = jsonObj.getJSONArray("attributes"); |
||||||
|
for (int i = 0; i < array.size(); i++) { |
||||||
|
LogUtils.debug4plugin("Array:" + array.getString(i) + " "); |
||||||
|
JSONObject obj = array.getJSONObject(i); |
||||||
|
if (obj.has(UID)) { |
||||||
|
LogUtils.debug4plugin("UID:" + obj.get(UID)); |
||||||
|
} |
||||||
|
if (obj.has("customized-reserve7")) { |
||||||
|
LogUtils.debug4plugin("customized-reserve7:" + obj.get("customized-reserve7")); |
||||||
|
return obj.getString("customized-reserve7"); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
return username; |
||||||
|
} |
||||||
|
|
||||||
|
public static String sendPost(String url, String param) { |
||||||
|
PrintWriter out = null; |
||||||
|
BufferedReader in = null; |
||||||
|
String result = ""; |
||||||
|
try { |
||||||
|
URL realUrl = new URL(url); |
||||||
|
// 打开和URL之间的连接
|
||||||
|
URLConnection conn = realUrl.openConnection(); |
||||||
|
// 发送POST请求必须设置如下两行
|
||||||
|
conn.setDoOutput(true); |
||||||
|
conn.setDoInput(true); |
||||||
|
// 获取URLConnection对象对应的输出流
|
||||||
|
out = new PrintWriter(conn.getOutputStream()); |
||||||
|
// 发送请求参数
|
||||||
|
out.print(param); |
||||||
|
// flush输出流的缓冲
|
||||||
|
out.flush(); |
||||||
|
// 定义BufferedReader输入流来读取URL的响应
|
||||||
|
in = new BufferedReader(new InputStreamReader(conn.getInputStream())); |
||||||
|
String line; |
||||||
|
while ((line = in.readLine()) != null) { |
||||||
|
result += line; |
||||||
|
} |
||||||
|
} catch (Exception e) { |
||||||
|
LogUtils.debug4plugin("发送 POST 请求出现异常!" + e); |
||||||
|
e.printStackTrace(); |
||||||
|
} finally { |
||||||
|
try { |
||||||
|
if (out != null) { |
||||||
|
out.close(); |
||||||
|
} |
||||||
|
if (in != null) { |
||||||
|
in.close(); |
||||||
|
} |
||||||
|
} catch (IOException ex) { |
||||||
|
ex.printStackTrace(); |
||||||
|
} |
||||||
|
} |
||||||
|
return result; |
||||||
|
} |
||||||
|
|
||||||
|
} |
@ -0,0 +1,122 @@ |
|||||||
|
package com.fr.plugin.xxx.xxx.sso.util; |
||||||
|
|
||||||
|
import com.fr.log.FineLoggerFactory; |
||||||
|
import com.fr.log.FineLoggerProvider; |
||||||
|
import com.fr.plugin.context.PluginContexts; |
||||||
|
import com.fr.plugin.xxx.xxx.sso.xxxConfig; |
||||||
|
import com.fr.plugin.xxx.xxx.sso.PluginConstants; |
||||||
|
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 = PluginConstants.PLUGIN_NAME; |
||||||
|
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 (xxxConfig.getInstance().getDebugSwitch()) { |
||||||
|
LOGGER.error(DEBUG_PREFIX + LOG_PREFIX + s); |
||||||
|
} else { |
||||||
|
LOGGER.debug(LOG_PREFIX + s); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
public static void debug4plugin(String s, Object... objects) { |
||||||
|
if (xxxConfig.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 (xxxConfig.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); |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,56 @@ |
|||||||
|
package com.fr.plugin.xxx.xxx.sso.util; |
||||||
|
|
||||||
|
|
||||||
|
import java.io.UnsupportedEncodingException; |
||||||
|
import java.security.MessageDigest; |
||||||
|
import java.security.NoSuchAlgorithmException; |
||||||
|
|
||||||
|
/** |
||||||
|
* @Author xxx |
||||||
|
* @Date 2020/7/5 |
||||||
|
* @Description |
||||||
|
**/ |
||||||
|
public class Sha256Util { |
||||||
|
|
||||||
|
/** |
||||||
|
* 利用java原生的类实现SHA256加密 |
||||||
|
* |
||||||
|
* @param str 加密后的报文 |
||||||
|
* @return |
||||||
|
*/ |
||||||
|
public static String getSHA256(String str) { |
||||||
|
MessageDigest messageDigest; |
||||||
|
String encodestr = ""; |
||||||
|
try { |
||||||
|
messageDigest = MessageDigest.getInstance("SHA-256"); |
||||||
|
messageDigest.update(str.getBytes("UTF-8")); |
||||||
|
encodestr = byte2Hex(messageDigest.digest()); |
||||||
|
} catch (NoSuchAlgorithmException e) { |
||||||
|
LogUtils.debug4plugin(e.getMessage(), e); |
||||||
|
} catch (UnsupportedEncodingException e) { |
||||||
|
LogUtils.debug4plugin(e.getMessage(), e); |
||||||
|
} |
||||||
|
return encodestr; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 将byte转为16进制 |
||||||
|
* |
||||||
|
* @param bytes |
||||||
|
* @return |
||||||
|
*/ |
||||||
|
private static String byte2Hex(byte[] bytes) { |
||||||
|
StringBuffer stringBuffer = new StringBuffer(); |
||||||
|
String temp = null; |
||||||
|
for (int i = 0; i < bytes.length; i++) { |
||||||
|
temp = Integer.toHexString(bytes[i] & 0xFF); |
||||||
|
if (temp.length() == 1) { |
||||||
|
//1得到一位的进行补0操作
|
||||||
|
stringBuffer.append("0"); |
||||||
|
} |
||||||
|
stringBuffer.append(temp); |
||||||
|
} |
||||||
|
return stringBuffer.toString(); |
||||||
|
} |
||||||
|
|
||||||
|
} |
Loading…
Reference in new issue