Browse Source

DEC-15861 feat: Fr/Bi融合与独立部署V1

master
zed 4 years ago
parent
commit
9cff6a3549
  1. 23
      src/main/java/com/fanruan/api/conf/ConfigurationKit.java
  2. 119
      src/main/java/com/fanruan/api/decision/login/LoginKit.java
  3. 22
      src/main/java/com/fanruan/api/decision/user/UserKit.java
  4. 24
      src/main/java/com/fanruan/api/report/ReportEntryKit.java
  5. 29
      src/test/java/com/fanruan/api/conf/ConfigurationKitTest.java
  6. 42
      src/test/java/com/fanruan/api/decision/login/LoginKitTest.java
  7. 6
      src/test/java/com/fanruan/api/decision/user/UserKitTest.java
  8. 24
      src/test/java/com/fanruan/api/report/ReportEntryKitTest.java

23
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<String, Object> getConfig(Configuration config) {
return WebServiceUtils.getConfigValue(config);
}
/**
* 更新配置项信息
*
* @param config 配置项
* @param map 更新内容
*/
public static void setConfig(Configuration config, Map<String, Object> map) {
WebServiceUtils.setConfigValue(config, map);
}
/**
* 兼容配置缓存失效
*

119
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;
}
/**
* 获取决策平台设置的认证类型
*

22
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);
}
/**
* 获取当前用户idcookie
*
* @param request 请求体
* @return 用户id
* @throws Exception 异常
*/
public static String getCurrentUserIdFromCookie(HttpServletRequest request) throws Exception {
return UserService.getInstance().getCurrentUserIdFromCookie(request);
}
/**
* 获取超级管理角色下的用户ID列表
*

24
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);
}
}

29
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);
}
}

42
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);
}
}

6
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

24
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);
}
}
Loading…
Cancel
Save