From 9cff6a3549d18f4f3f49ff7c798e2271af0781c7 Mon Sep 17 00:00:00 2001 From: Zed Date: Tue, 15 Dec 2020 17:27:57 +0800 Subject: [PATCH] =?UTF-8?q?DEC-15861=20feat:=20Fr/Bi=E8=9E=8D=E5=90=88?= =?UTF-8?q?=E4=B8=8E=E7=8B=AC=E7=AB=8B=E9=83=A8=E7=BD=B2V1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../fanruan/api/conf/ConfigurationKit.java | 33 ++++- .../fanruan/api/decision/login/LoginKit.java | 119 ++++++++++++++++++ .../fanruan/api/decision/user/UserKit.java | 22 ++++ .../fanruan/api/report/ReportEntryKit.java | 24 ++++ .../api/conf/ConfigurationKitTest.java | 29 +++++ .../api/decision/login/LoginKitTest.java | 42 ++++++- .../api/decision/user/UserKitTest.java | 6 + .../api/report/ReportEntryKitTest.java | 24 ++++ 8 files changed, 293 insertions(+), 6 deletions(-) create mode 100644 src/main/java/com/fanruan/api/report/ReportEntryKit.java create mode 100644 src/test/java/com/fanruan/api/conf/ConfigurationKitTest.java create mode 100644 src/test/java/com/fanruan/api/report/ReportEntryKitTest.java diff --git a/src/main/java/com/fanruan/api/conf/ConfigurationKit.java b/src/main/java/com/fanruan/api/conf/ConfigurationKit.java index 30706ed..34bef0a 100644 --- a/src/main/java/com/fanruan/api/conf/ConfigurationKit.java +++ b/src/main/java/com/fanruan/api/conf/ConfigurationKit.java @@ -4,11 +4,14 @@ import com.fanruan.api.generic.Matcher; import com.fanruan.api.generic.Runner; import com.fr.config.Configuration; import com.fr.config.holder.ConfigChangeListener; +import com.fr.decision.webservice.utils.WebServiceUtils; import com.fr.transaction.Configurations; import com.fr.transaction.ValidateProxy; import com.fr.transaction.WorkerCallBack; import com.fr.transaction.WorkerFacade; +import java.util.Map; + /** * @author richie * @version 10.0 @@ -17,6 +20,26 @@ import com.fr.transaction.WorkerFacade; */ public class ConfigurationKit { + /** + * 获取配置项的属性,组成map + * + * @param config 配置项 + * @return map + */ + public static Map getConfig(Configuration config) { + return WebServiceUtils.getConfigValue(config); + } + + /** + * 更新配置项信息 + * + * @param config 配置项 + * @param map 更新内容 + */ + public static void setConfig(Configuration config, Map map) { + WebServiceUtils.setConfigValue(config, map); + } + /** * 兼容配置缓存失效 * @@ -64,8 +87,8 @@ public class ConfigurationKit { * 在一个事务中修改配置 * 支持 让 configType, configTypes 缓存失效 * - * @param configType 配置类 - * @param runner 执行器 + * @param configType 配置类 + * @param runner 执行器 * @param configTypes 配置类(所有的配置类都会缓存失效) */ public static void modify(final Runner runner, Class configType, Class... configTypes) { @@ -106,9 +129,9 @@ public class ConfigurationKit { * 在一个事务中修改配置 * 支持 让 configType, configTypes 失效 * - * @param configType 配置类 - * @param runner 执行器 - * @param callBack 事务回调 + * @param configType 配置类 + * @param runner 执行器 + * @param callBack 事务回调 * @param configTypes 配置类(所有的配置类都会缓存失效) */ public static void modify(final Runner runner, WorkerCallBack callBack, Class configType, Class... configTypes) { diff --git a/src/main/java/com/fanruan/api/decision/login/LoginKit.java b/src/main/java/com/fanruan/api/decision/login/LoginKit.java index 915d4a7..c8ffe87 100644 --- a/src/main/java/com/fanruan/api/decision/login/LoginKit.java +++ b/src/main/java/com/fanruan/api/decision/login/LoginKit.java @@ -1,13 +1,20 @@ package com.fanruan.api.decision.login; +import com.fr.data.NetworkHelper; import com.fr.decision.authorize.Passport; import com.fr.decision.authorize.impl.HttpPassport; import com.fr.decision.config.AppearanceConfig; import com.fr.decision.config.FSConfig; +import com.fr.decision.mobile.terminal.TerminalHandler; import com.fr.decision.webservice.Response; import com.fr.decision.webservice.v10.login.LoginService; +import com.fr.decision.webservice.v10.login.TokenResource; +import com.fr.general.ComparatorUtils; import com.fr.json.JSONObject; +import com.fr.log.FineLoggerFactory; +import com.fr.security.JwtUtils; import com.fr.stable.StringUtils; +import com.fr.stable.web.Device; import com.fr.third.fasterxml.jackson.databind.ObjectMapper; import com.fr.third.socketio.SocketIOClient; import com.fr.web.utils.WebUtils; @@ -23,6 +30,118 @@ import javax.servlet.http.HttpServletResponse; */ public class LoginKit { + /** + * 根据请求(header)获取用户名 + * @param req http请求 + * @return 用户名 + */ + public static String getUserNameFromRequest(HttpServletRequest req) { + return LoginService.getInstance().getUserNameFromRequest(req); + } + + /** + * 根据请求(cookie)获取用户名 + * @param req http请求 + * @return 用户名 + */ + public static String getUserNameFromRequestCookie(HttpServletRequest req) { + return LoginService.getInstance().getUserNameFromRequestCookie(req); + } + + /** + * 根据请求(header)获取展示名 + * @param req http请求 + * @return 用户名 + */ + public static String getDisplayNameFromRequest(HttpServletRequest req) { + return LoginService.getInstance().getDisplayNameFromRequest(req); + } + + /** + * 根据请求(cookie)获取展示名 + * @param req http请求 + * @return 用户名 + */ + public static String getDisplayNameFromRequestCookie(HttpServletRequest req) { + return LoginService.getInstance().getDisplayNameFromRequestCookie(req); + } + + /** + * 根据请求(header)获取用户名,不会抛出异常 + * @param req http请求 + * @return 用户名 + */ + public static String getCurrentUserNameFromRequest(HttpServletRequest req) { + return LoginService.getInstance().getCurrentUserNameFromRequest(req); + } + + /** + * 根据请求(cookie)获取用户名,不会抛出异常 + * @param req http请求 + * @return 用户名 + */ + public static String getCurrentUserNameFromRequestCookie(HttpServletRequest req) { + return LoginService.getInstance().getCurrentUserNameFromRequestCookie(req); + } + + /** + * 通过用户名生成token(仅用户名登录) + * + * @param req http请求 + * @param res http响应 + * @param username 用户名 + * @return token + * @throws Exception 异常 + */ + public static String login(HttpServletRequest req, HttpServletResponse res, String username) throws Exception { + return LoginService.getInstance().login(req, res, username); + } + + /** + * 通过请求的header获取token + * + * @param req http请求 + * @return token + */ + public static String getTokenByHeader(HttpServletRequest req) { + return TokenResource.HEADER.getToken(req); + } + + /** + * 通过请求的cookie获取token + * + * @param req http请求 + * @return token + */ + public static String getTokenByCookie(HttpServletRequest req) { + return TokenResource.COOKIE.getToken(req); + } + + /** + * 校验token是否有效(当前登录者和token信息是否一致 & token未超时) + * + * @param req http请求 + * @param token token + * @param currentUserName 当前用户名 + * @return 是否有效 + */ + public static boolean checkTokenValid(HttpServletRequest req, String token, String currentUserName) { + + try { + //当前登录用户和token对应的用户名不同,需要重新生成token + if (!ComparatorUtils.equals(currentUserName, JwtUtils.parseJWT(token).getSubject())) { + FineLoggerFactory.getLogger().info("Username changed:{}", currentUserName); + return false; + } + Device device = NetworkHelper.getDevice(req); + LoginService.getInstance().loginStatusValid(token, TerminalHandler.getTerminal(req, device)); + return true; + } catch (Exception ignore) { + } + + return false; + } + /** * 获取决策平台设置的认证类型 * diff --git a/src/main/java/com/fanruan/api/decision/user/UserKit.java b/src/main/java/com/fanruan/api/decision/user/UserKit.java index 5b5f570..50cea6e 100644 --- a/src/main/java/com/fanruan/api/decision/user/UserKit.java +++ b/src/main/java/com/fanruan/api/decision/user/UserKit.java @@ -16,6 +16,17 @@ import java.util.List; */ public class UserKit { + /** + * 判断是否存在该用户名 + * + * @param username 用户名 + * @return 是否存在 + * @throws Exception 如果在获取用户的过程中出错了,则抛出此异常 + */ + public static boolean existUsername(String username) throws Exception { + return UserService.getInstance().getUserByUserName(username) != null; + } + /** * 根据用户名获取这个用户的来源 * @@ -87,6 +98,17 @@ public class UserKit { return UserService.getInstance().getCurrentUserId(request); } + /** + * 获取当前用户id(cookie) + * + * @param request 请求体 + * @return 用户id + * @throws Exception 异常 + */ + public static String getCurrentUserIdFromCookie(HttpServletRequest request) throws Exception { + return UserService.getInstance().getCurrentUserIdFromCookie(request); + } + /** * 获取超级管理角色下的用户ID列表 * diff --git a/src/main/java/com/fanruan/api/report/ReportEntryKit.java b/src/main/java/com/fanruan/api/report/ReportEntryKit.java new file mode 100644 index 0000000..982ffba --- /dev/null +++ b/src/main/java/com/fanruan/api/report/ReportEntryKit.java @@ -0,0 +1,24 @@ +package com.fanruan.api.report; + +import com.fr.decision.webservice.v10.entry.ReportEntryService; + +/** + * @author Zed + * @version 10.0 + * Created by Zed on 2020/12/14 + * 在平台挂查看FR模板的工具类 + */ +public class ReportEntryKit { + + /** + * 获取FR模板树 + * + * @param currentUserId 当前用户id + * @param keyword keyword + * @return 模板树 + * @throws Exception 异常 + */ + public static Object getReportTemplateTree(String currentUserId, String keyword) throws Exception { + return ReportEntryService.getInstance().getReportTemplateTree(currentUserId, keyword); + } +} diff --git a/src/test/java/com/fanruan/api/conf/ConfigurationKitTest.java b/src/test/java/com/fanruan/api/conf/ConfigurationKitTest.java new file mode 100644 index 0000000..36b2f56 --- /dev/null +++ b/src/test/java/com/fanruan/api/conf/ConfigurationKitTest.java @@ -0,0 +1,29 @@ +package com.fanruan.api.conf; + +import com.fr.config.Configuration; +import org.junit.Assert; +import org.junit.Test; + +import java.lang.reflect.Method; +import java.util.Map; + +/** + * @author Zed + * @version 10.0 + * Created by Zed on 2020/12/15 + */ +public class ConfigurationKitTest { + + @Test + public void update() throws Exception { + + Class classBook = Class.forName("com.fanruan.api.conf.ConfigurationKit"); + Assert.assertNotNull(classBook); + + Method method1 = classBook.getDeclaredMethod("getConfig", Configuration.class); + Assert.assertNotNull(method1); + + Method method2 = classBook.getDeclaredMethod("setConfig", Configuration.class, Map.class); + Assert.assertNotNull(method2); + } +} diff --git a/src/test/java/com/fanruan/api/decision/login/LoginKitTest.java b/src/test/java/com/fanruan/api/decision/login/LoginKitTest.java index 16deed6..91376ae 100644 --- a/src/test/java/com/fanruan/api/decision/login/LoginKitTest.java +++ b/src/test/java/com/fanruan/api/decision/login/LoginKitTest.java @@ -19,6 +19,10 @@ import org.junit.Assert; import org.junit.Before; import org.junit.Test; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.lang.reflect.Method; + /** * @author lidongy * @version 10.0 @@ -38,7 +42,7 @@ public class LoginKitTest { } @Test - public void testRefreshToken() throws Exception{ + public void testRefreshToken() throws Exception { String jwtString = JwtKit.createDefaultJWT("123"); try { LoginKit.refreshToken(jwtString, 1000 * 60, null); @@ -62,4 +66,40 @@ public class LoginKitTest { AppearanceConfig.getInstance().setLoginPageId("aaa"); Assert.assertEquals(LoginKit.getLoginPageId(), "aaa"); } + + @Test + public void getUserNameFromRequest() throws Exception { + Class classBook = Class.forName("com.fanruan.api.decision.login.LoginKit"); + Assert.assertNotNull(classBook); + + Method method1 = classBook.getDeclaredMethod("getUserNameFromRequest", HttpServletRequest.class); + Assert.assertNotNull(method1); + + Method method2 = classBook.getDeclaredMethod("getUserNameFromRequestCookie", HttpServletRequest.class); + Assert.assertNotNull(method2); + + Method method3 = classBook.getDeclaredMethod("getDisplayNameFromRequest", HttpServletRequest.class); + Assert.assertNotNull(method3); + + Method method4 = classBook.getDeclaredMethod("getDisplayNameFromRequestCookie", HttpServletRequest.class); + Assert.assertNotNull(method4); + + Method method5 = classBook.getDeclaredMethod("getCurrentUserNameFromRequest", HttpServletRequest.class); + Assert.assertNotNull(method5); + + Method method6 = classBook.getDeclaredMethod("getCurrentUserNameFromRequestCookie", HttpServletRequest.class); + Assert.assertNotNull(method6); + + Method method7 = classBook.getDeclaredMethod("login", HttpServletRequest.class, HttpServletResponse.class, String.class); + Assert.assertNotNull(method7); + + Method method8 = classBook.getDeclaredMethod("getTokenByHeader", HttpServletRequest.class); + Assert.assertNotNull(method8); + + Method method9 = classBook.getDeclaredMethod("getTokenByCookie", HttpServletRequest.class); + Assert.assertNotNull(method9); + + Method method10 = classBook.getDeclaredMethod("checkTokenValid", HttpServletRequest.class, String.class, String.class); + Assert.assertNotNull(method10); + } } diff --git a/src/test/java/com/fanruan/api/decision/user/UserKitTest.java b/src/test/java/com/fanruan/api/decision/user/UserKitTest.java index ef2165a..7606386 100644 --- a/src/test/java/com/fanruan/api/decision/user/UserKitTest.java +++ b/src/test/java/com/fanruan/api/decision/user/UserKitTest.java @@ -31,6 +31,12 @@ public class UserKitTest { Method method1 = classBook.getDeclaredMethod("getCurrentUserId", HttpServletRequest.class); Assert.assertNotNull(method1); + + Method method2 = classBook.getDeclaredMethod("getCurrentUserIdFromCookie", HttpServletRequest.class); + Assert.assertNotNull(method2); + + Method method3 = classBook.getDeclaredMethod("existUsername", String.class); + Assert.assertNotNull(method3); } @Test diff --git a/src/test/java/com/fanruan/api/report/ReportEntryKitTest.java b/src/test/java/com/fanruan/api/report/ReportEntryKitTest.java new file mode 100644 index 0000000..e798618 --- /dev/null +++ b/src/test/java/com/fanruan/api/report/ReportEntryKitTest.java @@ -0,0 +1,24 @@ +package com.fanruan.api.report; + +import org.junit.Assert; +import org.junit.Test; + +import java.lang.reflect.Method; + +/** + * @author Zed + * @version 10.0 + * Created by Zed on 2020/12/15 + */ +public class ReportEntryKitTest { + + @Test + public void getReportTemplateTree() throws Exception { + + Class classBook = Class.forName("com.fanruan.api.report.ReportEntryKit"); + Assert.assertNotNull(classBook); + + Method method1 = classBook.getDeclaredMethod("getReportTemplateTree", String.class, String.class); + Assert.assertNotNull(method1); + } +}