diff --git a/JSD-8975-需求确认书V.docx b/JSD-8975-需求确认书V.docx new file mode 100644 index 0000000..330b780 Binary files /dev/null and b/JSD-8975-需求确认书V.docx differ diff --git a/README.md b/README.md index 5daa8fc..d16fa55 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,6 @@ # open-JSD-8975 -JSD-8975 单点(PC+移动端区分) \ No newline at end of file +JSD-8975 单点(PC+移动端区分)\ +免责说明:该源码为第三方爱好者提供,不保证源码和方案的可靠性,也不提供任何形式的源码教学指导和协助!\ +仅作为开发者学习参考使用!禁止用于任何商业用途!\ +为保护开发者隐私,开发者信息已隐去!若原开发者希望公开自己的信息,可联系hugh处理。 \ No newline at end of file diff --git a/lib/aws-api-client.jar b/lib/aws-api-client.jar new file mode 100644 index 0000000..6ed336f Binary files /dev/null and b/lib/aws-api-client.jar differ diff --git a/lib/commons-codec-1.15.jar b/lib/commons-codec-1.15.jar new file mode 100644 index 0000000..f14985a Binary files /dev/null and b/lib/commons-codec-1.15.jar differ diff --git a/plugin.xml b/plugin.xml new file mode 100644 index 0000000..2a5a87c --- /dev/null +++ b/plugin.xml @@ -0,0 +1,26 @@ + + com.fr.plugin.bsSSO + + yes + 1.0.22 + 10.0 + 2018-07-31 + fr.open + + + com.fr.plugin.bsSSO + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/main/java/com/fr/plugin/bsSSO/bean/simple/DemoInitializeMonitor.java b/src/main/java/com/fr/plugin/bsSSO/bean/simple/DemoInitializeMonitor.java new file mode 100644 index 0000000..a4b5b6f --- /dev/null +++ b/src/main/java/com/fr/plugin/bsSSO/bean/simple/DemoInitializeMonitor.java @@ -0,0 +1,25 @@ +package com.fr.plugin.bsSSO.bean.simple; + + + + +import com.fr.plugin.context.PluginContext; +import com.fr.plugin.observer.inner.AbstractPluginLifecycleMonitor; +import com.fr.plugin.bsSSO.bean.simple.account.PluginSimpleConfig; + +/** + * @author fr.open + * @version 10.0 + * Created by fr.open on 2018-12-04 + */ +public class DemoInitializeMonitor extends AbstractPluginLifecycleMonitor { + @Override + public void afterRun(PluginContext pluginContext) { + PluginSimpleConfig.getInstance(); + } + + @Override + public void beforeStop(PluginContext pluginContext) { + + } +} diff --git a/src/main/java/com/fr/plugin/bsSSO/bean/simple/PluginInitializeFilterBridge.java b/src/main/java/com/fr/plugin/bsSSO/bean/simple/PluginInitializeFilterBridge.java new file mode 100644 index 0000000..f7182cf --- /dev/null +++ b/src/main/java/com/fr/plugin/bsSSO/bean/simple/PluginInitializeFilterBridge.java @@ -0,0 +1,24 @@ +package com.fr.plugin.bsSSO.bean.simple; + + +import com.fr.decision.fun.impl.AbstractEmbedRequestFilterProvider; +import com.fr.plugin.bsSSO.bean.simple.account.PluginSimpleConfig; + +import javax.servlet.FilterConfig; +import javax.servlet.ServletException; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.IOException; + +public class PluginInitializeFilterBridge extends AbstractEmbedRequestFilterProvider { + + @Override + public void init(FilterConfig filterConfig) { + PluginSimpleConfig.getInstance(); + } + + @Override + public void filter(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws IOException, ServletException { + + } +} diff --git a/src/main/java/com/fr/plugin/bsSSO/bean/simple/PluginLocaleFinderBridge.java b/src/main/java/com/fr/plugin/bsSSO/bean/simple/PluginLocaleFinderBridge.java new file mode 100644 index 0000000..294bbf8 --- /dev/null +++ b/src/main/java/com/fr/plugin/bsSSO/bean/simple/PluginLocaleFinderBridge.java @@ -0,0 +1,10 @@ +package com.fr.plugin.bsSSO.bean.simple; + +import com.fr.stable.fun.impl.AbstractLocaleFinder; + +public class PluginLocaleFinderBridge extends AbstractLocaleFinder { + @Override + public String find() { + return "conf"; + } +} diff --git a/src/main/java/com/fr/plugin/bsSSO/bean/simple/account/PluginSimpleConfig.java b/src/main/java/com/fr/plugin/bsSSO/bean/simple/account/PluginSimpleConfig.java new file mode 100644 index 0000000..9bab25d --- /dev/null +++ b/src/main/java/com/fr/plugin/bsSSO/bean/simple/account/PluginSimpleConfig.java @@ -0,0 +1,140 @@ +package com.fr.plugin.bsSSO.bean.simple.account; + +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; +import com.fr.record.analyzer.EnableMetrics; + +@Visualization(category = "Plugin-Config_Group") +@EnableMetrics +public class PluginSimpleConfig extends DefaultConfiguration { + + private static volatile PluginSimpleConfig config = null; + + @Focus(id="com.fr.plugin.bsSSO.bean.simple", text = "Plugin-Config_Demo", source = Original.PLUGIN) + public static PluginSimpleConfig getInstance() { + if (config == null) { + config = ConfigContext.getConfigInstance(PluginSimpleConfig.class); + } + return config; + } + + @Identifier(value = "awsServer", name = "认证中心地址", description = "认证中心地址", status = Status.SHOW) + private Conf awsServer = Holders.simple("https://xxx"); + + @Identifier(value = "accessKey", name = "认证中心秘钥", description = "认证中心秘钥", status = Status.SHOW) + private Conf accessKey = Holders.simple(""); + + @Identifier(value = "index", name = "帆软首页", description = "帆软首页", status = Status.SHOW) + private Conf index = Holders.simple("http://localhost:8075/webroot/decision"); + + @Identifier(value = "mindex", name = "帆软移动端首页", description = "帆软移动端首页", status = Status.SHOW) + private Conf mindex = Holders.simple("http://localhost:8075/webroot/decision/url/mobile"); + + @Identifier(value = "mtoken", name = "移动端获取用户id接口地址", description = "移动端获取用户id接口地址", status = Status.SHOW) + private Conf mtoken = Holders.simple("https://xxxx"); + + @Identifier(value = "muser", name = "移动端获取用户信息接口地址", description = "移动端获取用户信息接口地址", status = Status.SHOW) + private Conf muser = Holders.simple("https://xxxx"); + + @Identifier(value = "mdomain", name = "移动端接口域名", description = "移动端接口域名", status = Status.SHOW) + private Conf mdomain = Holders.simple("xxxx"); + + @Identifier(value = "puser", name = "PC端获取用户信息接口地址", description = "PC端获取用户信息接口地址", status = Status.SHOW) + private Conf puser = Holders.simple("http://xxxx"); + + @Identifier(value = "pkey", name = "PC端accessKey", description = "pc用", status = Status.SHOW) + private Conf pkey = Holders.simple("xxxx"); + + @Identifier(value = "psecret", name = "PC端secret", description = "pc用", status = Status.SHOW) + private Conf psecret = Holders.simple("xxxx"); + + public String getAwsServer() { + return awsServer.get(); + } + + public void setAwsServer(String url) { + this.awsServer.set(url); + } + + public String getAccessKey() { + return accessKey.get(); + } + + public void setAccessKey(String url) { + this.accessKey.set(url); + } + + public String getIndex() { + return index.get(); + } + + public void setIndex(String url) { + this.index.set(url); + } + + public String getMindex() { + return mindex.get(); + } + + public void setMindex(String url) { + this.mindex.set(url); + } + + public String getMtoken() { + return mtoken.get(); + } + + public void setMtoken(String url) { + this.mtoken.set(url); + } + + public String getMuser() { + return muser.get(); + } + + public void setMuser(String url) { + this.muser.set(url); + } + + public String getPuser() { + return puser.get(); + } + + public void setPuser(String url) { + this.puser.set(url); + } + + public String getPkey() { + return pkey.get(); + } + + public void setPkey(String url) { + this.pkey.set(url); + } + + public String getPsecret() { + return psecret.get(); + } + + public void setPsecret(String url) { + this.psecret.set(url); + } + + public String getMdomain() { + return mdomain.get(); + } + + public void setMdomain(String url) { + this.mdomain.set(url); + } + + @Override + public Object clone() throws CloneNotSupportedException { + PluginSimpleConfig cloned = (PluginSimpleConfig) super.clone(); + + return cloned; + } +} diff --git a/src/main/java/com/fr/plugin/bsSSO/filter/LoginFilter.java b/src/main/java/com/fr/plugin/bsSSO/filter/LoginFilter.java new file mode 100644 index 0000000..02a32d2 --- /dev/null +++ b/src/main/java/com/fr/plugin/bsSSO/filter/LoginFilter.java @@ -0,0 +1,57 @@ +package com.fr.plugin.bsSSO.filter; + +import com.fr.decision.fun.impl.AbstractEmbedRequestFilterProvider; +import com.fr.json.JSONObject; +import com.fr.plugin.bsSSO.bean.simple.account.PluginSimpleConfig; +import com.fr.plugin.bsSSO.utils.FRUtils; +import com.fr.plugin.bsSSO.utils.HttpUtils; +import com.fr.plugin.bsSSO.utils.Utils; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.IOException; + +public class LoginFilter extends AbstractEmbedRequestFilterProvider { + private static String awsServer = ""; //认证中心url + private static String accessKey = ""; //认证中心秘钥 + + static + { + PluginSimpleConfig psc = PluginSimpleConfig.getInstance(); + awsServer = psc.getAwsServer(); + accessKey = psc.getAccessKey(); + } + + @Override + public void filter(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws IOException { + String tokenId = httpServletRequest.getParameter("tokenId"); + + if(!Utils.isNullStr(tokenId)){ + FRUtils.FRLogInfo("SSO:tokenId:"+tokenId); + String tokenUrl = awsServer+"/r/jd?cmd=com.actionsoft.apps.addons.sso_validate&tokenId="+tokenId+"&access_key="+accessKey; + + FRUtils.FRLogInfo("SSO:tokenUrl:"+tokenUrl); + + String result = HttpUtils.HttpPost(tokenUrl); + + if(!Utils.isNullStr(result)){ + JSONObject jsonObject = new JSONObject(result); + + JSONObject data = jsonObject.getJSONObject("data"); + boolean validate = data.getBoolean("validate"); + + if(validate){ + String uid = data.getString("uid"); + FRUtils.FRLogInfo("SSO:uid:"+uid); + + FRUtils.Login(httpServletRequest,httpServletResponse,uid,""); + + return; + } + } + + } + + } + +} diff --git a/src/main/java/com/fr/plugin/bsSSO/handler/ExtendAttrHandlerProvider.java b/src/main/java/com/fr/plugin/bsSSO/handler/ExtendAttrHandlerProvider.java new file mode 100644 index 0000000..ec4a24a --- /dev/null +++ b/src/main/java/com/fr/plugin/bsSSO/handler/ExtendAttrHandlerProvider.java @@ -0,0 +1,13 @@ +package com.fr.plugin.bsSSO.handler; + +import com.fr.decision.fun.HttpHandler; +import com.fr.decision.fun.impl.AbstractHttpHandlerProvider; + +public class ExtendAttrHandlerProvider extends AbstractHttpHandlerProvider { + @Override + public HttpHandler[] registerHandlers() { + return new HttpHandler[]{ + new MobileLogin(),new PCLogin(),new PCLogin2() + }; + } +} diff --git a/src/main/java/com/fr/plugin/bsSSO/handler/MobileLogin.java b/src/main/java/com/fr/plugin/bsSSO/handler/MobileLogin.java new file mode 100644 index 0000000..6273cb1 --- /dev/null +++ b/src/main/java/com/fr/plugin/bsSSO/handler/MobileLogin.java @@ -0,0 +1,146 @@ +package com.fr.plugin.bsSSO.handler; + +import com.fr.base.TemplateUtils; +import com.fr.decision.fun.impl.BaseHttpHandler; +import com.fr.json.JSONObject; +import com.fr.plugin.bsSSO.bean.simple.account.PluginSimpleConfig; +import com.fr.plugin.bsSSO.utils.FRUtils; +import com.fr.plugin.bsSSO.utils.HttpUtils; +import com.fr.plugin.bsSSO.utils.ResponseUtils; +import com.fr.plugin.bsSSO.utils.Utils; +import com.fr.plugin.transform.FunctionRecorder; +import com.fr.third.org.apache.http.HttpEntity; +import com.fr.third.org.apache.http.HttpResponse; +import com.fr.third.org.apache.http.client.HttpClient; +import com.fr.third.org.apache.http.client.methods.HttpGet; +import com.fr.third.org.apache.http.impl.client.BasicCookieStore; +import com.fr.third.org.apache.http.impl.client.HttpClientBuilder; +import com.fr.third.org.apache.http.impl.cookie.BasicClientCookie; +import com.fr.third.org.apache.http.util.EntityUtils; +import com.fr.third.springframework.web.bind.annotation.RequestMethod; +import com.fr.web.utils.WebUtils; + +import javax.servlet.http.Cookie; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.IOException; +import java.util.HashMap; +import java.util.Map; + +public class MobileLogin extends BaseHttpHandler { + + + public MobileLogin() { + } + + @Override + public RequestMethod getMethod() { + return RequestMethod.GET; + } + + @Override + public String getPath() { + return "/mobilelogin"; + } + + @Override + public boolean isPublic() { + return true; + } + + @Override + public void handle(HttpServletRequest req, HttpServletResponse res) throws Exception { + PluginSimpleConfig psc = PluginSimpleConfig.getInstance(); + + //获取请求参数 + String token = req.getParameter("token"); + //生成cookie + BasicClientCookie cookie = new BasicClientCookie("access_token", token); + cookie.setDomain(psc.getMdomain()); + cookie.setPath("/"); + //获取用户id + String userid = getUserid(cookie,psc); + //获取用户名 + String username = getUsername(cookie,userid,psc); + + if(!FRUtils.isUserExist(username)){ + String tip ="数据平台不存在该账号:"+username; + Map parameterMap = new HashMap(); + String path = "/com/fr/plugin/bsSSO/html/error.html"; + parameterMap.put("tip", tip); + String macPage = TemplateUtils.renderTemplate(path, parameterMap); + WebUtils.printAsString(res, macPage); + return ; + } + + FRUtils.Login(req,res,username,PluginSimpleConfig.getInstance().getMindex()); + + +// try { +// FRUtils.Login2(req,res,username,PluginSimpleConfig.getInstance().getMindex()); +// } catch (Exception e) { +//// res.sendRedirect(PluginSimpleConfig.getInstance().getMindex()); +// } + } + + private String getUsername(BasicClientCookie cookies, String userid, PluginSimpleConfig psc) { + String url = psc.getMuser(); + url += userid; + + String result = get(cookies,url); + + if(Utils.isNullStr(result)){ + return ""; + } + + JSONObject resultJson = new JSONObject(result); + int status = resultJson.getInt("status"); + + if(status != 200){ + return ""; + } + + return resultJson.getJSONObject("content").getString("login"); + } + + //获取用户id + private String getUserid(BasicClientCookie cookies,PluginSimpleConfig psc) { + String url = psc.getMtoken(); + String result = get(cookies,url); + + if(Utils.isNullStr(result)){ + return ""; + } + + JSONObject resultJson = new JSONObject(result); + int status = resultJson.getInt("status"); + + if(status != 200){ + return ""; + } + + return resultJson.getString("content"); + } + + private String get(BasicClientCookie cookie,String url) { + BasicCookieStore cookieStore = new BasicCookieStore(); + cookieStore.addCookie(cookie); + + HttpClient client = HttpClientBuilder.create().setDefaultCookieStore(cookieStore).build(); + final HttpGet request = new HttpGet(url); + + String returnResult = ""; + + try{ + HttpResponse response = client.execute(request); + HttpEntity entity = response.getEntity(); + returnResult = EntityUtils.toString(entity, "utf-8"); + }catch(Exception e){ + FRUtils.FRLogInfo("get exception " + e.getMessage()); + } + + + return returnResult; + } +} + diff --git a/src/main/java/com/fr/plugin/bsSSO/handler/PCLogin.java b/src/main/java/com/fr/plugin/bsSSO/handler/PCLogin.java new file mode 100644 index 0000000..e7c54b0 --- /dev/null +++ b/src/main/java/com/fr/plugin/bsSSO/handler/PCLogin.java @@ -0,0 +1,121 @@ +package com.fr.plugin.bsSSO.handler; + +import com.fr.decision.fun.impl.BaseHttpHandler; +import com.fr.json.JSONObject; +import com.fr.plugin.bsSSO.bean.simple.account.PluginSimpleConfig; +import com.fr.plugin.bsSSO.utils.*; +import com.fr.plugin.transform.FunctionRecorder; +import com.fr.third.springframework.web.bind.annotation.RequestMethod; + +import javax.servlet.http.Cookie; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +public class PCLogin extends BaseHttpHandler { + + + public PCLogin() { + } + + @Override + public RequestMethod getMethod() { + return RequestMethod.POST; + } + + @Override + public String getPath() { + return "/openapi"; + } + + @Override + public boolean isPublic() { + return true; + } + + @Override + public void handle(HttpServletRequest req, HttpServletResponse res){ + PluginSimpleConfig psc = PluginSimpleConfig.getInstance(); + String access_key = req.getParameter("access_key"); + + FRUtils.FRLogInfo("pc access_key " + access_key); + if(Utils.isNullStr(access_key) || !psc.getPkey().equals(access_key)){ + FRUtils.FRLogInfo("access_key 无效"); + ResponseUtils.response(res,"access_key 无效","-1","","error"); + return ; + } + + String sig = req.getParameter("sig"); + FRUtils.FRLogInfo("query sig "+sig); + + String allUrl = FRUtils.getAllUrl(req); + FRUtils.FRLogInfo("allUrl: "+allUrl); + String queryStr = allUrl.split("\\?")[1]; + + String validateSign = OpenapiUtil.generateSign(queryStr,psc.getPsecret()); + + FRUtils.FRLogInfo("validateSign "+sig); + + if(!sig.equals(validateSign)){ + FRUtils.FRLogInfo("验签失败"); + + ResponseUtils.response(res,"验签失败","-1","","error"); + return ; + } + + //获取请求参数 + String token = req.getParameter("token"); + //获取用户名 + String username = getUsername(token); + +// String username = "xiamaofa"; + + if(Utils.isNullStr(username)){ + FRUtils.FRLogInfo("获取用户信息失败"); + + ResponseUtils.response(res,"获取用户信息失败","-1","","error"); + return ; + } + + if(!FRUtils.isUserExist(username)){ + FRUtils.FRLogInfo("数据平台不存在该账号:"+username); + ResponseUtils.response(res,"数据平台不存在该账号:"+username,"-1","","error"); + return ; + } + + String frtoken = FRUtils.getToken(req,res,username); + + if(Utils.isNullStr(frtoken)){ + FRUtils.FRLogInfo("生成token失败:"); + + ResponseUtils.response(res,"生成token失败","-1","","error"); + return ; + } + + String url = psc.getIndex()+"/url/pclogin?token="+frtoken; + + ResponseUtils.response(res,"","200",url,"ok"); + + } + + private String getUsername(String token) { + String url = PluginSimpleConfig.getInstance().getPuser(); + url += "?token="+token; + + String result = HttpUtils.get(url,null,null); + + if(Utils.isNullStr(result)){ + return ""; + } + + JSONObject resultJson = new JSONObject(result); + String status = resultJson.getString("Status"); + + if(!status.equals("S")){ + return ""; + } + + return resultJson.getString("LoginId"); + } + +} + diff --git a/src/main/java/com/fr/plugin/bsSSO/handler/PCLogin2.java b/src/main/java/com/fr/plugin/bsSSO/handler/PCLogin2.java new file mode 100644 index 0000000..fa76c46 --- /dev/null +++ b/src/main/java/com/fr/plugin/bsSSO/handler/PCLogin2.java @@ -0,0 +1,48 @@ +package com.fr.plugin.bsSSO.handler; + +import com.fr.decision.fun.impl.BaseHttpHandler; +import com.fr.json.JSONObject; +import com.fr.plugin.bsSSO.bean.simple.account.PluginSimpleConfig; +import com.fr.plugin.bsSSO.utils.*; +import com.fr.third.springframework.web.bind.annotation.RequestMethod; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +public class PCLogin2 extends BaseHttpHandler { + + + public PCLogin2() { + } + + @Override + public RequestMethod getMethod() { + return RequestMethod.GET; + } + + @Override + public String getPath() { + return "/pclogin"; + } + + @Override + public boolean isPublic() { + return true; + } + + @Override + public void handle(HttpServletRequest req, HttpServletResponse res){ + String token = req.getParameter("token"); + + if(Utils.isNullStr(token)){ + ResponseUtils.response(res,"token不能为空","error","",""); + return ; + } + + FRUtils.loginByToken(req,res,token,PluginSimpleConfig.getInstance().getIndex()); + } + + + +} + diff --git a/src/main/java/com/fr/plugin/bsSSO/handler/URLAliasProvide.java b/src/main/java/com/fr/plugin/bsSSO/handler/URLAliasProvide.java new file mode 100644 index 0000000..278d1fb --- /dev/null +++ b/src/main/java/com/fr/plugin/bsSSO/handler/URLAliasProvide.java @@ -0,0 +1,16 @@ +package com.fr.plugin.bsSSO.handler; + +import com.fr.decision.fun.impl.AbstractURLAliasProvider; +import com.fr.decision.webservice.url.alias.URLAlias; +import com.fr.decision.webservice.url.alias.URLAliasFactory; + +public class URLAliasProvide extends AbstractURLAliasProvider { + @Override + public URLAlias[] registerAlias() { + return new URLAlias[]{ + URLAliasFactory.createPluginAlias("/openapi","/openapi",true), + URLAliasFactory.createPluginAlias("/pclogin","/pclogin",true), + URLAliasFactory.createPluginAlias("/mobilelogin","/mobilelogin",true), + }; + } +} diff --git a/src/main/java/com/fr/plugin/bsSSO/utils/FRUtils.java b/src/main/java/com/fr/plugin/bsSSO/utils/FRUtils.java new file mode 100644 index 0000000..087faff --- /dev/null +++ b/src/main/java/com/fr/plugin/bsSSO/utils/FRUtils.java @@ -0,0 +1,244 @@ +package com.fr.plugin.bsSSO.utils; + +import com.fr.base.ServerConfig; +import com.fr.decision.authority.AuthorityContext; +import com.fr.decision.webservice.login.LogInOutResultInfo; +import com.fr.decision.webservice.utils.DecisionServiceConstants; +import com.fr.decision.webservice.v10.login.LoginService; +import com.fr.decision.webservice.v10.login.event.LogInOutEvent; +import com.fr.event.EventDispatcher; +import com.fr.log.FineLoggerFactory; +import com.fr.stable.StringUtils; +import com.fr.stable.query.QueryFactory; +import com.fr.stable.query.restriction.RestrictionFactory; +import com.fr.web.utils.WebUtils; + +import javax.servlet.http.Cookie; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import javax.servlet.http.HttpSession; +import java.io.IOException; +import java.util.List; + +public class FRUtils { + private static void writeToken2Cookie(HttpServletResponse var1, String var2, int var3) { + try { + if (StringUtils.isNotEmpty(var2)) { + Cookie var4 = new Cookie("fine_auth_token", var2); + long var5 = var3 == -2 ? 1209600000L : (long)var3; + var4.setMaxAge((int)var5); + var4.setPath(ServerConfig.getInstance().getCookiePath()); + var1.addCookie(var4); + Cookie var7 = new Cookie("fine_remember_login", String.valueOf(var3 == -2 ? -2 : -1)); + var7.setMaxAge((int)var5); + var7.setPath(ServerConfig.getInstance().getCookiePath()); + var1.addCookie(var7); + } else { + FineLoggerFactory.getLogger().error("empty token cannot save."); + } + } catch (Exception var8) { + FineLoggerFactory.getLogger().error(var8.getMessage(), var8); + } + + } + + /** + * 帆软登录 + * @param httpServletRequest + * @param httpServletResponse + * @param token + * @param url + */ + public static void loginByToken(HttpServletRequest httpServletRequest,HttpServletResponse httpServletResponse,String token,String url){ + + FineLoggerFactory.getLogger().info("FRLOG:token:"+token); + FineLoggerFactory.getLogger().info("FRLOG:跳转链接:"+url); + + + //判断用户名是否为空 + if(!Utils.isNullStr(token)){ + writeToken2Cookie(httpServletResponse,token,-1); + + HttpSession session = httpServletRequest.getSession(true); + + httpServletRequest.setAttribute(DecisionServiceConstants.FINE_AUTH_TOKEN_NAME,token); + + session.setAttribute(DecisionServiceConstants.FINE_AUTH_TOKEN_NAME, token); + + if(!Utils.isNullStr(url)){ + try { + httpServletResponse.sendRedirect(url); + } catch (IOException e) { + ResponseUtils.failedResponse(httpServletResponse,"跳转异常!"); + FineLoggerFactory.getLogger().info("FRLOG:跳转异常!"); + } + } + }else{ + ResponseUtils.failedResponse(httpServletResponse,"token不能为空!"); + FineLoggerFactory.getLogger().info("FRLOG:token不能为空!"); + } + } + + /** + * 获取token + * @param httpServletRequest + * @param httpServletResponse + * @param username + * @return + */ + public static String getToken(HttpServletRequest httpServletRequest,HttpServletResponse httpServletResponse,String username){ + String token = ""; + try { + token = LoginService.getInstance().login(httpServletRequest, httpServletResponse, username); + } catch (Exception e) { + FineLoggerFactory.getLogger().info("FRLOG:获取token失败"+e.getMessage()); + } + + return token; + } + /** + * 获取带参数的访问链接 + * @return + */ + public static String getAllUrl(HttpServletRequest httpServletRequest){ + return WebUtils.getOriginalURL(httpServletRequest); + } + /** + * 判断用户是否存在 + * @param userName + * @return + */ + public static boolean isUserExist(String userName){ + if (StringUtils.isEmpty(userName)) { + return false; + } else { + try { + List var1 = AuthorityContext.getInstance().getUserController().find(QueryFactory.create().addRestriction(RestrictionFactory.eq("userName", userName))); + return var1 != null && !var1.isEmpty(); + } catch (Exception var2) { + FineLoggerFactory.getLogger().error(var2.getMessage()); + return false; + } + } + } + + /** + * 判断是否登录FR + * @param req + * @return + */ + public static boolean isLogin(HttpServletRequest req){ + + + return LoginService.getInstance().isLogged(req); + } + + /** + * 帆软登录 + * @param httpServletRequest + * @param httpServletResponse + * @param userName + * @param url + */ + public static void Login(HttpServletRequest httpServletRequest,HttpServletResponse httpServletResponse,String userName,String url){ + + FineLoggerFactory.getLogger().info("FRLOG:用户名:"+userName); + FineLoggerFactory.getLogger().info("FRLOG:跳转链接:"+url); + + + //判断用户名是否为空 + if(!Utils.isNullStr(userName)){ + if(isUserExist(userName)){ + String FRToken = ""; + + try { + HttpSession session = httpServletRequest.getSession(true); + + FRToken = LoginService.getInstance().login(httpServletRequest, httpServletResponse, userName); + httpServletRequest.setAttribute(DecisionServiceConstants.FINE_AUTH_TOKEN_NAME,FRToken); + + session.setAttribute(DecisionServiceConstants.FINE_AUTH_TOKEN_NAME, FRToken); + EventDispatcher.fire(LogInOutEvent.LOGIN,new LogInOutResultInfo(httpServletRequest,httpServletResponse,userName,true)); + FineLoggerFactory.getLogger().info("FRLOG:登陆成功!"); + + if(!Utils.isNullStr(url)){ + httpServletResponse.sendRedirect(url); + } + } catch (Exception e) { + ResponseUtils.failedResponse(httpServletResponse,"登录异常,请联系管理员!"); + FineLoggerFactory.getLogger().info("FRLOG:登录异常,请联系管理员!"); + FineLoggerFactory.getLogger().info("FRLOGException:"+e.getMessage()); + } + }else{ + ResponseUtils.failedResponse(httpServletResponse,"用户在报表系统中不存在!"); + FineLoggerFactory.getLogger().info("FRLOG:用户在报表系统中不存在!"); + } + }else{ + ResponseUtils.failedResponse(httpServletResponse,"用户名不能为空!"); + FineLoggerFactory.getLogger().info("FRLOG:用户名不能为空!"); + } + } + + /** + * 帆软登录 + * @param httpServletRequest + * @param httpServletResponse + * @param userName + * @param url + */ + public static void Login2(HttpServletRequest httpServletRequest,HttpServletResponse httpServletResponse,String userName,String url) throws Exception { + FineLoggerFactory.getLogger().info("FRLOG:用户名:"+userName); + FineLoggerFactory.getLogger().info("FRLOG:跳转链接:"+url); + + //判断用户名是否为空 + if(!Utils.isNullStr(userName)){ + if(isUserExist(userName)){ + String FRToken = ""; + HttpSession session = httpServletRequest.getSession(true); + + FRToken = LoginService.getInstance().login(httpServletRequest, httpServletResponse, userName); + httpServletRequest.setAttribute(DecisionServiceConstants.FINE_AUTH_TOKEN_NAME,FRToken); + + session.setAttribute(DecisionServiceConstants.FINE_AUTH_TOKEN_NAME, FRToken); + EventDispatcher.fire(LogInOutEvent.LOGIN,new LogInOutResultInfo(httpServletRequest,httpServletResponse,userName,true)); + FineLoggerFactory.getLogger().info("FRLOG:登陆成功!"); + + if(!Utils.isNullStr(url)){ + httpServletResponse.sendRedirect(url); + } + }else{ + throw new RuntimeException(); + } + }else{ + throw new RuntimeException(); + } + } + + /** + * + * @param httpServletRequest + * @param httpServletResponse + */ + public static void logout(HttpServletRequest httpServletRequest,HttpServletResponse httpServletResponse) + { + if(!isLogin(httpServletRequest)){ + return ; + } + + try { + LoginService.getInstance().logout(httpServletRequest,httpServletResponse); + } catch (Exception e) { + ResponseUtils.failedResponse(httpServletResponse,"登出异常,请联系管理员!"); + FineLoggerFactory.getLogger().info("FRLOG:登出异常,请联系管理员!"); + FineLoggerFactory.getLogger().info("FRLOGException:"+e.getMessage()); + } + } + + /** + * 打印FR日志 + * @param message + */ + public static void FRLogInfo(String message){ + FineLoggerFactory.getLogger().info("FRLOG:"+message); + } +} diff --git a/src/main/java/com/fr/plugin/bsSSO/utils/HttpUtils.java b/src/main/java/com/fr/plugin/bsSSO/utils/HttpUtils.java new file mode 100644 index 0000000..28323df --- /dev/null +++ b/src/main/java/com/fr/plugin/bsSSO/utils/HttpUtils.java @@ -0,0 +1,208 @@ +package com.fr.plugin.bsSSO.utils; + +import com.fr.log.FineLoggerFactory; +import com.fr.third.org.apache.http.HttpEntity; +import com.fr.third.org.apache.http.HttpResponse; +import com.fr.third.org.apache.http.HttpStatus; +import com.fr.third.org.apache.http.client.CookieStore; +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.entity.StringEntity; +import com.fr.third.org.apache.http.impl.client.BasicCookieStore; +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.impl.cookie.BasicClientCookie; +import com.fr.third.org.apache.http.ssl.SSLContexts; +import com.fr.third.org.apache.http.ssl.TrustStrategy; +import com.fr.third.org.apache.http.util.EntityUtils; + +import javax.net.ssl.SSLContext; +import javax.servlet.http.Cookie; +import java.security.cert.CertificateException; +import java.security.cert.X509Certificate; +import java.util.Map; +import java.util.Set; +public class HttpUtils { + + /** + * cookies转cookieStore + * @param cookies + * @return + */ + public static CookieStore cookieToCookieStore(Cookie[] cookies){ + CookieStore cookieStore = new BasicCookieStore(); + + if(cookies != null && cookies.length>0){ + for(Cookie cookie : cookies){ + BasicClientCookie cookie1 = new BasicClientCookie(cookie.getName(), cookie.getValue()); + cookieStore.addCookie(cookie1); + } + } + + return cookieStore; + } + + private static CloseableHttpClient createHttpClient(Cookie[] cookies){ + + SSLContext sslContext = null; + try { + sslContext = SSLContexts.custom().loadTrustMaterial(null, new TrustStrategy() { + @Override + public boolean isTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException { + return true; + } + }).build(); + } catch (Exception e) { + FRUtils.FRLogInfo("exception:"+e.getMessage()); + } + + CloseableHttpClient httpclient = null; + + if(cookies != null && cookies.length > 0){ + CookieStore cookieStore = cookieToCookieStore(cookies); + + httpclient = HttpClients.custom().setSslcontext(sslContext). + setSSLHostnameVerifier(new NoopHostnameVerifier()).setDefaultCookieStore(cookieStore).build(); + } + else{ + httpclient = HttpClients.custom().setSslcontext(sslContext). + setSSLHostnameVerifier(new NoopHostnameVerifier()).build(); + } + + return httpclient; + } + + /** + * httpGet请求 + * @param url + * @return + */ + public static String get(String url,Cookie[] cookies,Map header){ + FineLoggerFactory.getLogger().info("FRLOG:HttpUtils.get--url:"+url); + + //创建httpClient + CloseableHttpClient httpclient = createHttpClient(cookies); + + HttpGet getMethod = new HttpGet(url); + + if(header != null && header.size() > 0){ + Set keySet = header.keySet(); + + for(String key : keySet){ + getMethod.setHeader(key,header.get(key)); + } + } + + try { + HttpResponse response = httpclient.execute(getMethod); + int status =response.getStatusLine().getStatusCode(); + HttpEntity entity = response.getEntity(); + String returnResult = EntityUtils.toString(entity, "utf-8"); + + FineLoggerFactory.getLogger().info("FRLOG:HttpUtils.get--status:"+status); + FineLoggerFactory.getLogger().info("FRLOG:HttpUtils.get--returnResult:"+returnResult); + + if (status == HttpStatus.SC_OK) { + return returnResult; + } + } catch (Exception e) { + FineLoggerFactory.getLogger().info("FRLOG:HttpUtils.get--exception:"+e.getMessage()); + } + + return ""; + } + + /** + * httpGet请求 + * @param url + * @return + */ + public static String get(String url){ + FineLoggerFactory.getLogger().info("FRLOG:HttpUtils.get--url:"+url); + + HttpGet getMethod = new HttpGet(url); + CloseableHttpClient httpclient = HttpClients.createDefault(); + + try { + HttpResponse response = httpclient.execute(getMethod); + + FineLoggerFactory.getLogger().info("FRLOG:HttpUtils.get--status:"+response.getStatusLine().getStatusCode()); + + if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { + HttpEntity entity = response.getEntity(); + String returnResult = EntityUtils.toString(entity, "utf-8"); + + FineLoggerFactory.getLogger().info("FRLOG:HttpUtils.get--returnResult:"+returnResult); + + return returnResult; + } + } catch (Exception e) { + FineLoggerFactory.getLogger().info("FRLOG:HttpUtils.get--exception:"+e.getMessage()); + } + + return ""; + } + + /** + * HttpPost请求 + * @param url + * @return + */ + public static String HttpPost(String url){ + FineLoggerFactory.getLogger().info("FRLOG:HttpPost:url:"+url); + + HttpPost postMethod = new HttpPost(url); + CloseableHttpClient httpclient = HttpClients.createDefault(); + + try { + HttpResponse response = httpclient.execute(postMethod); + + FineLoggerFactory.getLogger().info("FRLOG:HttpPost:status:"+response.getStatusLine().getStatusCode()); + + if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { + HttpEntity entity = response.getEntity(); + String returnResult = EntityUtils.toString(entity, "utf-8"); + + FineLoggerFactory.getLogger().info("FRLOG:HttpPost:returnResult:"+returnResult); + + return returnResult; + } + } catch (Exception e) { + FineLoggerFactory.getLogger().info("FRLOG:HttpPost:exception:"+e.getMessage()); + } + + return ""; + } + + public static String HttpPostXML(String url, String xmlParam){ + FineLoggerFactory.getLogger().info("FRLOG:HttpPostXML:url:"+url); + + HttpPost postMethod = new HttpPost(url); + CloseableHttpClient httpclient = HttpClients.createDefault(); + + + try { + postMethod.setHeader("Content-type", "text/html"); + HttpEntity entity2 = new StringEntity(xmlParam); + postMethod.setEntity(entity2); + + HttpResponse response = httpclient.execute(postMethod); + + FineLoggerFactory.getLogger().info("FRLOG:HttpPostXML:status:"+response.getStatusLine().getStatusCode()); + + if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { + HttpEntity entity = response.getEntity(); + String returnResult = EntityUtils.toString(entity, "utf-8"); + + FineLoggerFactory.getLogger().info("FRLOG:HttpPostXML:returnResult:"+returnResult); + + return returnResult; + } + } catch (Exception e) { + FineLoggerFactory.getLogger().info("FRLOG:HttpPostXML:exception:"+e.getMessage()); + } + + return ""; + } +} diff --git a/src/main/java/com/fr/plugin/bsSSO/utils/OpenapiUtil.java b/src/main/java/com/fr/plugin/bsSSO/utils/OpenapiUtil.java new file mode 100644 index 0000000..fc922ab --- /dev/null +++ b/src/main/java/com/fr/plugin/bsSSO/utils/OpenapiUtil.java @@ -0,0 +1,44 @@ +package com.fr.plugin.bsSSO.utils; + +import com.actionsoft.bpms.api.common.ApiUtils; + +import java.util.Collection; +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; + +public class OpenapiUtil { + + /** + * 生成签名 + * @param queryStr + * @param secret + * @return + */ + public static String generateSign(String queryStr,String secret){ + Map param = getParam(queryStr); + + return ApiUtils.makeSig(param, secret, (Collection)Collections.EMPTY_LIST); + } + + private static Map getParam(String url){ + Map param = new HashMap(); + + String[] oneSplit = url.split("&"); + + for(int i = 0;i + + + + + + + +
+
+
数据平台无相关账号
+
+
+ + + + \ No newline at end of file