From 2ba49be1537041129774df06a1e9d60ba42887e8 Mon Sep 17 00:00:00 2001 From: richie Date: Thu, 19 Sep 2019 17:45:56 +0800 Subject: [PATCH] =?UTF-8?q?=E5=86=B3=E7=AD=96=E5=B9=B3=E5=8F=B0=E7=9B=B8?= =?UTF-8?q?=E5=85=B3=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/fanruan/api/decision/CommonKit.java | 54 +++++ .../com/fanruan/api/decision/UserKit.java | 39 ++++ .../api/decision/auth/BasePassport.java | 25 ++ .../decision/auth/bean/BasePassportBean.java | 40 ++++ .../api/{ => decision}/login/LoginKit.java | 16 +- .../api/decision/middle/ReadOnlyUser.java | 45 ++++ .../com/fanruan/api/runtime/ResourceKit.java | 219 ++++++++++++++++++ 7 files changed, 437 insertions(+), 1 deletion(-) create mode 100644 src/main/java/com/fanruan/api/decision/CommonKit.java create mode 100644 src/main/java/com/fanruan/api/decision/UserKit.java create mode 100644 src/main/java/com/fanruan/api/decision/auth/BasePassport.java create mode 100644 src/main/java/com/fanruan/api/decision/auth/bean/BasePassportBean.java rename src/main/java/com/fanruan/api/{ => decision}/login/LoginKit.java (75%) create mode 100644 src/main/java/com/fanruan/api/decision/middle/ReadOnlyUser.java create mode 100644 src/main/java/com/fanruan/api/runtime/ResourceKit.java diff --git a/src/main/java/com/fanruan/api/decision/CommonKit.java b/src/main/java/com/fanruan/api/decision/CommonKit.java new file mode 100644 index 0000000..a0c49a7 --- /dev/null +++ b/src/main/java/com/fanruan/api/decision/CommonKit.java @@ -0,0 +1,54 @@ +package com.fanruan.api.decision; + +import com.fanruan.api.log.LogKit; +import com.fanruan.api.util.StringKit; +import com.fr.base.Base64; +import com.fr.base.ServerConfig; +import com.fr.decision.webservice.utils.DecisionServiceConstants; +import com.fr.stable.StringUtils; + +/** + * @author richie + * @version 10.0 + * Created by richie on 2019-09-19 + * 常规工具类 + */ +public class CommonKit { + + /** + * 这个方法用于判断从前端返回服务器端的密码是否有被修改过 + * + * @param password 从前端返回的密码文本 + * @return 如果密码没有被修改过就返回true,否则就返回false + */ + public static boolean isDefaultPasswordHolderString(String password) { + return DecisionServiceConstants.DEFAULT_PASSWORD.equals(password); + } + + /** + * 获取默认密码占位符 + * + * @return 默认密码占位符 + */ + public static String fetchDefaultPasswordHolderString() { + return DecisionServiceConstants.DEFAULT_PASSWORD; + } + + /** + * 将base64编码的字符串转为常规的字符串 + * + * @param encodeStr base64编码的字符串 + * @return 常规字符串 + */ + public static String getBase64DecodeStr(String encodeStr) { + try { + if (StringUtils.isNotEmpty(encodeStr)) { + return new String(Base64.decode(encodeStr), ServerConfig.getInstance().getServerCharset()); + } + } catch (Exception e) { + LogKit.error(e.getMessage(), e); + } + + return StringKit.EMPTY; + } +} diff --git a/src/main/java/com/fanruan/api/decision/UserKit.java b/src/main/java/com/fanruan/api/decision/UserKit.java new file mode 100644 index 0000000..323d6b4 --- /dev/null +++ b/src/main/java/com/fanruan/api/decision/UserKit.java @@ -0,0 +1,39 @@ +package com.fanruan.api.decision; + +import com.fanruan.api.decision.middle.ReadOnlyUser; +import com.fr.decision.authority.data.User; +import com.fr.decision.webservice.utils.UserSourceFactory; +import com.fr.decision.webservice.utils.user.source.UserSource; +import com.fr.decision.webservice.v10.user.UserService; + +/** + * @author richie + * @version 10.0 + * Created by richie on 2019-09-19 + */ +public class UserKit { + + /** + * 根据用户名获取这个用户的来源 + * + * @param username 用户名 + * @return 用户来源 + * @throws Exception 如果在获取用户的过程中出错了,则抛出此异常 + */ + public static UserSource fetchUserSource(String username) throws Exception { + User user = UserService.getInstance().getUserByUserName(username); + return UserSourceFactory.getInstance().getUserSource(user); + } + + /** + * 根据用户名获取用户信息 + * + * @param username 用户名 + * @return 用户信息 + * @throws Exception 如果获取用户出错,则抛出此异常 + */ + public static ReadOnlyUser getReadOnlyUser(String username) throws Exception { + User user = UserService.getInstance().getUserByUserName(username); + return ReadOnlyUser.build(user.getUserName(), user.getPassword(), user.getRealName()); + } +} diff --git a/src/main/java/com/fanruan/api/decision/auth/BasePassport.java b/src/main/java/com/fanruan/api/decision/auth/BasePassport.java new file mode 100644 index 0000000..e0e8aea --- /dev/null +++ b/src/main/java/com/fanruan/api/decision/auth/BasePassport.java @@ -0,0 +1,25 @@ +package com.fanruan.api.decision.auth; + +import com.fr.decision.authorize.impl.AbstractPassport; + +/** + * @author richie + * @version 10.0 + * Created by richie on 2019-09-19 + * 认证方式 + */ +public abstract class BasePassport extends AbstractPassport { + + /** + * 检查登录信息是否有效 + * + * @param username 用户名 + * @param inputPassword 输入的密码 + * @param savedPassword 配置文件中保存的密码 + * @param hashPassword 哈希后的密码 + * @return 登录凭证是有效的则返回true,否则返回false + */ + public boolean checkTicket(String username, String inputPassword, String savedPassword, String hashPassword) { + return false; + } +} diff --git a/src/main/java/com/fanruan/api/decision/auth/bean/BasePassportBean.java b/src/main/java/com/fanruan/api/decision/auth/bean/BasePassportBean.java new file mode 100644 index 0000000..e8b8b91 --- /dev/null +++ b/src/main/java/com/fanruan/api/decision/auth/bean/BasePassportBean.java @@ -0,0 +1,40 @@ +package com.fanruan.api.decision.auth.bean; + +import com.fr.decision.authorize.Passport; +import com.fr.decision.webservice.bean.authentication.PassportBean; +import com.fr.third.fasterxml.jackson.annotation.JsonTypeInfo; + +/** + * @author richie + * @version 10.0 + * Created by richie on 2019-09-19 + * 用于表示不同认证方式的接口 + */ +@JsonTypeInfo(use = JsonTypeInfo.Id.CLASS, include = JsonTypeInfo.As.PROPERTY, property = "@class") +public abstract class BasePassportBean extends PassportBean { + + /** + * 用于标记认证类型的字符串 + * + * @return 字符串 + */ + @Override + public abstract String markType(); + + /** + * 根据配置对象生成认证配置数据层对象 + * + * @param t 配置对象 + * @return 配属数据层 + */ + @Override + public abstract BasePassportBean createPassportBean(T t); + + /** + * 生成配置对象 + * + * @return 配置 + */ + @Override + public abstract Passport createPassport(); +} diff --git a/src/main/java/com/fanruan/api/login/LoginKit.java b/src/main/java/com/fanruan/api/decision/login/LoginKit.java similarity index 75% rename from src/main/java/com/fanruan/api/login/LoginKit.java rename to src/main/java/com/fanruan/api/decision/login/LoginKit.java index be128c1..620c2a2 100644 --- a/src/main/java/com/fanruan/api/login/LoginKit.java +++ b/src/main/java/com/fanruan/api/decision/login/LoginKit.java @@ -1,4 +1,4 @@ -package com.fanruan.api.login; +package com.fanruan.api.decision.login; import com.fr.decision.authorize.Passport; import com.fr.decision.authorize.impl.HttpPassport; @@ -14,14 +14,27 @@ public class LoginKit { /** * 获取决策平台设置的认证类型 + * * @return 认证对象 */ public static Passport getCurrentPassport() { return FSConfig.getInstance().getPassport(); } + /** + * 获取指定类型的通行证,如果不存在,就返回null + * + * @param type 通行证类型 + * @param 类型 + * @return 通行证 + */ + public static T getPassport(Class type) { + return FSConfig.getInstance().getPassport(type); + } + /** * 获取http认证的地址 + * * @return http认证地址 */ public static @Nullable String getHttpPassportUrl() { @@ -34,6 +47,7 @@ public class LoginKit { /** * 获取http认证的秘钥 + * * @return http认证地址 */ public static @Nullable String getHttpPassportKey() { diff --git a/src/main/java/com/fanruan/api/decision/middle/ReadOnlyUser.java b/src/main/java/com/fanruan/api/decision/middle/ReadOnlyUser.java new file mode 100644 index 0000000..0c1321d --- /dev/null +++ b/src/main/java/com/fanruan/api/decision/middle/ReadOnlyUser.java @@ -0,0 +1,45 @@ +package com.fanruan.api.decision.middle; + +/** + * @author richie + * @version 10.0 + * Created by richie on 2019-09-19 + */ +public class ReadOnlyUser { + + /** + * 非空项,无默认值 + */ + private String userName = null; + /** + * 非空项,无默认值 + */ + private String password = null; + + /** + * 姓名 + */ + private String realName = null; + + public static ReadOnlyUser build(String userName, String password, String realName) { + return new ReadOnlyUser(userName, password, realName); + } + + private ReadOnlyUser(String userName, String password, String realName) { + this.userName = userName; + this.password = password; + this.realName = realName; + } + + public String getUserName() { + return userName; + } + + public String getPassword() { + return password; + } + + public String getRealName() { + return realName; + } +} diff --git a/src/main/java/com/fanruan/api/runtime/ResourceKit.java b/src/main/java/com/fanruan/api/runtime/ResourceKit.java new file mode 100644 index 0000000..3bb638e --- /dev/null +++ b/src/main/java/com/fanruan/api/runtime/ResourceKit.java @@ -0,0 +1,219 @@ +package com.fanruan.api.runtime; + +import com.fr.io.utils.ResourceIOUtils; +import com.fr.stable.Filter; + +import java.io.IOException; +import java.io.InputStream; + +/** + * @author richie + * @version 10.0 + * Created by richie on 2019-09-19 + * 操作运行环境下文件的工具方法 + */ +public class ResourceKit { + + /** + * 路径是否存在 + * + * @param target 路径 + * @return 是否存在 + */ + public static boolean exist(String target) { + return ResourceIOUtils.exist(target); + } + + /** + * 创建文件 + * + * @param file 文件路径 + * @return 是否存在 + */ + public static boolean createFile(String file) { + return ResourceIOUtils.createFile(file); + } + + /** + * 创建文件夹 + * + * @param dir 文件夹路径 + * @return 是否成功 + */ + public static boolean createDirectory(String dir) { + return ResourceIOUtils.createDirectory(dir); + } + + /** + * 是否为文件夹 + * + * @param path 路径 + * @return 是否是文件夹 + */ + public static boolean isDirectory(String path) { + return ResourceIOUtils.isDirectory(path); + } + + /** + * 获取文件或者文件夹的名称 + * + * @param path 路径 + * @return 名称 + */ + public static String getName(String path) { + return ResourceIOUtils.getName(path); + } + + /** + * 列出路径下的所有内容的名称 + * + * @param dir 路径 + * @return 所有内容名称List + */ + public static String[] list(String dir) { + return ResourceIOUtils.list(dir); + } + + /** + * 列出路径下的所有符合要求的内容的名称 + * + * @param dir 路径 + * @param filter 过滤器 + * @return 所有内容名称List + */ + public static String[] list(String dir, Filter filter) { + return ResourceIOUtils.list(dir, filter); + } + + /** + * 文件夹是否存在 + * + * @param dir 文件夹 + * @return 是否存在 + */ + public static boolean isDirectoryExist(String dir) { + return ResourceIOUtils.isDirectoryExist(dir); + } + + + public static String getRealPath(String relativePath) { + return ResourceIOUtils.getRealPath(relativePath); + } + + /** + * 读取文件到流 + *

+ * 相对路径下如果是仓库资源从仓库读取,其它从本地读取 + *

+ * 据对路径按绝对路径读 + * + * @param path 路径 + * @return 流 + */ + public static InputStream read(String path) { + return ResourceIOUtils.read(path); + } + + /** + * 读取文件到bytes + * + * @param path 文件路径 + * @return bytes + */ + public static byte[] readBytes(String path) { + return ResourceIOUtils.readBytes(path); + } + + /** + * 写入数据流到文件 + * + * @param path 文件路径 + * @param data 数据 + */ + public static void write(String path, InputStream data) { + ResourceIOUtils.write(path, data); + } + + /** + * 写入字符串数据到文件 + * + * @param path 文件路径 + * @param data 数据 + */ + public static void write(String path, String data) { + ResourceIOUtils.write(path, data); + } + + /** + * 写入字节数组数据到文件 + * + * @param path 文件路径 + * @param data 数据 + */ + public static void write(String path, byte[] data) { + ResourceIOUtils.write(path, data); + } + + /** + * 获取父路径 + * + * @param path 路径 + * @return 父路径 + */ + public static String getParent(String path) { + return ResourceIOUtils.getParent(path); + } + + /** + * 从仓库删除 + * + * @param path 路径 + * @return 是否删除成功 + */ + public static boolean delete(String path) { + return ResourceIOUtils.delete(path); + } + + /** + * 文件的最后修改时间 + * + * @param path 文件路径 + * @return 修改的时间戳 + */ + public static long lastModified(String path) { + return ResourceIOUtils.lastModified(path); + } + + /** + * 文件大小 + * + * @param path 文件路径 + * @return 文件字节数 + */ + public static long getLength(String path) { + return ResourceIOUtils.getLength(path); + } + + /** + * 重命名 + *

+ * 文件位置根据oldFile来确定 + * + * @param oldFile 原文件path + * @param newFile 新文件path + * @return 是否成功 + */ + public static boolean renameTo(String oldFile, String newFile) { + return ResourceIOUtils.renameTo(oldFile, newFile); + } + + /** + * 拷贝文件到某目录下 + * + * @param src 源文件 + * @param dir 目标文件 + */ + public static void copy(String src, String dir) throws IOException { + ResourceIOUtils.copy(src, dir); + } +}