inleft
2022-02-21 cfde488bdd0163986087c880e0a2762645f8c14c
commit | author | age
9bcb19 1 /*
I 2 Copyright [2020] [https://www.xiaonuo.vip]
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8   http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15
16 Snowy采用APACHE LICENSE 2.0开源协议,您在使用过程中,需要注意以下几点:
17
18 1.请不要删除和修改根目录下的LICENSE文件。
19 2.请不要删除和修改Snowy源码头部的版权声明。
20 3.请保留源码和相关描述文件的项目出处,作者声明等。
21 4.分发源码时候,请注明软件出处 https://gitee.com/xiaonuobase/snowy
22 5.在修改包名,模块名称,项目代码等时,请注明软件出处 https://gitee.com/xiaonuobase/snowy
23 6.若您的项目无法满足以上几点,可申请商业授权,获取Snowy商业授权许可,请在官网购买授权,地址为 https://www.xiaonuo.vip
24  */
25 package vip.xiaonuo.core.context.constant;
26
27 import cn.hutool.core.collection.CollectionUtil;
28 import cn.hutool.core.convert.Convert;
29 import cn.hutool.core.util.ObjectUtil;
30 import cn.hutool.core.util.RandomUtil;
31 import cn.hutool.core.util.StrUtil;
32 import cn.hutool.log.Log;
33 import vip.xiaonuo.core.consts.CommonConstant;
34 import vip.xiaonuo.core.consts.SymbolConstant;
35 import vip.xiaonuo.core.exception.ServiceException;
36 import vip.xiaonuo.core.pojo.cryptogram.CryptogramConfigs;
37 import vip.xiaonuo.core.pojo.email.EmailConfigs;
38 import vip.xiaonuo.core.pojo.oauth.OauthConfigs;
39 import vip.xiaonuo.core.pojo.sms.AliyunSmsConfigs;
40 import vip.xiaonuo.core.pojo.sms.TencentSmsConfigs;
41
42 import java.util.List;
43
44 import static vip.xiaonuo.core.exception.enums.ServerExceptionEnum.CONSTANT_EMPTY;
45
46 /**
47  * 系统参数配置获取
48  *
49  * @author xuyuxiang
50  * @date 2020/4/14 15:34
51  */
52 public class ConstantContextHolder {
53
54     private static final Log log = Log.get();
55
56     /**
57      * 获取租户功能是否开启
58      *
59      * @author xuyuxiang
60      * @date 2020/9/3
61      */
62     public static Boolean getTenantOpenFlag() {
63         return getSysConfigWithDefault("SNOWY_TENANT_OPEN", Boolean.class, false);
64     }
65
66     /**
67      * 获取放开xss过滤的接口
68      *
69      * @author yubaoshan
70      * @date 2020/6/20 22:13
71      */
72     public static List<String> getUnXssFilterUrl() {
73         String snowyUnXssFilterUrl = getSysConfigWithDefault("SNOWY_UN_XSS_FILTER_URL", String.class, null);
74         if (ObjectUtil.isEmpty(snowyUnXssFilterUrl)) {
75             return CollectionUtil.newArrayList();
76         } else {
77             return CollectionUtil.toList(snowyUnXssFilterUrl.split(SymbolConstant.COMMA));
78         }
79     }
80
81     /**
82      * 获取演示环境开关是否开启,默认为false
83      *
84      * @author yubaoshan
85      * @date 2020/6/20 22:13
86      */
87     public static Boolean getDemoEnvFlag() {
88         return getSysConfigWithDefault("SNOWY_DEMO_ENV_FLAG", Boolean.class, false);
89     }
90
91     /**
92      * 邮件的配置
93      *
94      * @author yubaoshan
95      * @date 2020/6/19 18:08
96      */
97     public static EmailConfigs getEmailConfigs() {
98         String host = getSysConfig("SNOWY_EMAIL_HOST", String.class, true);
99         String username = getSysConfig("SNOWY_EMAIL_USERNAME", String.class, true);
100         String password = getSysConfig("SNOWY_EMAIL_PASSWORD", String.class, true);
101         Integer port = getSysConfig("SNOWY_EMAIL_PORT", Integer.class, true);
102         String from = getSysConfig("SNOWY_EMAIL_FROM", String.class, true);
103         Boolean ssl = getSysConfig("SNOWY_EMAIL_SSL", Boolean.class, true);
104
105         EmailConfigs emailConfigs = new EmailConfigs();
106         emailConfigs.setHost(host);
107         emailConfigs.setUser(username);
108         emailConfigs.setPass(password);
109         emailConfigs.setPort(port);
110         emailConfigs.setFrom(from);
111         emailConfigs.setSslEnable(ssl);
112         return emailConfigs;
113     }
114
115     /**
116      * 获取腾讯云短信的配置
117      *
118      * @author yubaoshan
119      * @date 2020/6/19 18:08
120      */
121     public static TencentSmsConfigs getTencentSmsConfigs() {
122         String snowyTencentSmsSecretId = getSysConfig("SNOWY_TENCENT_SMS_SECRET_ID", String.class, true);
123         String snowyTencentSmsSecretKey = getSysConfig("SNOWY_TENCENT_SMS_SECRET_KEY", String.class, true);
124         String snowyTencentSmsSdkAppId = getSysConfig("SNOWY_TENCENT_SMS_SDK_APP_ID", String.class, true);
125         String snowyTencentSmsSign = getSysConfig("SNOWY_TENCENT_SMS_SIGN", String.class, true);
126
127         TencentSmsConfigs tencentSmsConfigs = new TencentSmsConfigs();
128         tencentSmsConfigs.setSecretId(snowyTencentSmsSecretId);
129         tencentSmsConfigs.setSecretKey(snowyTencentSmsSecretKey);
130         tencentSmsConfigs.setSdkAppId(snowyTencentSmsSdkAppId);
131         tencentSmsConfigs.setSign(snowyTencentSmsSign);
132         return tencentSmsConfigs;
133     }
134
135     /**
136      * 获取阿里云短信的配置
137      *
138      * @author yubaoshan
139      * @date 2020/6/19 18:08
140      */
141     public static AliyunSmsConfigs getAliyunSmsConfigs() {
142             String snowySmsAccesskeyId = getSysConfig("SNOWY_ALIYUN_SMS_ACCESSKEY_ID", String.class, true);
143         String snowySmsAccesskeySecret = getSysConfig("SNOWY_ALIYUN_SMS_ACCESSKEY_SECRET", String.class, true);
144         String snowySmsSignName = getSysConfig("SNOWY_ALIYUN_SMS_SIGN_NAME", String.class, true);
145         String snowySmsLoginTemplateCode = getSysConfig("SNOWY_ALIYUN_SMS_LOGIN_TEMPLATE_CODE", String.class, true);
146         String snowySmsInvalidateMinutes = getSysConfig("SNOWY_ALIYUN_SMS_INVALIDATE_MINUTES", String.class, true);
147
148         AliyunSmsConfigs aliyunSmsProperties = new AliyunSmsConfigs();
149         aliyunSmsProperties.setAccessKeyId(snowySmsAccesskeyId);
150         aliyunSmsProperties.setAccessKeySecret(snowySmsAccesskeySecret);
151         aliyunSmsProperties.setSignName(snowySmsSignName);
152         aliyunSmsProperties.setLoginTemplateCode(snowySmsLoginTemplateCode);
153         aliyunSmsProperties.setInvalidateMinutes(Convert.toInt(snowySmsInvalidateMinutes));
154         return aliyunSmsProperties;
155     }
156
157     /**
158      * 获取jwt密钥,默认是32位随机字符串
159      *
160      * @author yubaoshan
161      * @date 2020/6/19 18:08
162      */
163     public static String getJwtSecret() {
164         return getSysConfigWithDefault("SNOWY_JWT_SECRET", String.class, RandomUtil.randomString(32));
165     }
166
167     /**
168      * 获取默认密码
169      *
170      * @author yubaoshan
171      * @date 2020/6/19 18:08
172      */
173     public static String getDefaultPassWord() {
174         return getSysConfigWithDefault("SNOWY_DEFAULT_PASSWORD", String.class, CommonConstant.DEFAULT_PASSWORD);
175     }
176
177     /**
178      * 获取会话过期时间,默认2小时
179      *
180      * @author yubaoshan
181      * @date 2020/7/9 16:18
182      */
183     public static Long getSessionTokenExpireSec() {
184         return getSysConfigWithDefault("SNOWY_SESSION_EXPIRE", Long.class, 2 * 60 * 60L);
185     }
186
187     /**
188      * 获取token过期时间(单位:秒)
189      * <p>
190      * 默认时间1天
191      *
192      * @author xuyuxiang
193      * @date 2020/6/19 18:08
194      */
195     public static Long getTokenExpireSec() {
196         return getSysConfigWithDefault("SNOWY_TOKEN_EXPIRE", Long.class, 86400L);
197     }
198
199     /**
200      * 获取自定义的windows环境本地文件上传路径
201      *
202      * @author xuyuxiang
203      * @date 2020/6/19 18:09
204      */
205     public static String getDefaultFileUploadPathForWindows() {
206         return getSysConfigWithDefault("SNOWY_FILE_UPLOAD_PATH_FOR_WINDOWS", String.class, "");
207     }
208
209     /**
210      * 获取自定义的linux环境本地文件上传路径
211      *
212      * @author xuyuxiang
213      * @date 2020/6/19 18:09
214      */
215     public static String getDefaultFileUploadPathForLinux() {
216         return getSysConfigWithDefault("SNOWY_FILE_UPLOAD_PATH_FOR_LINUX", String.class, "");
217     }
218
219     /**
220      * 获取是否允许单用户登陆的开关, 默认为false
221      *
222      * @author xuyuxiang
223      * @date 2020/6/19 18:09
224      */
225     public static Boolean getEnableSingleLogin() {
226         return getSysConfigWithDefault("SNOWY_ENABLE_SINGLE_LOGIN", Boolean.class, false);
227     }
228
229     /**
230      * 获取阿里云定位接口
231      *
232      * @author xuyuxiang
233      * @date 2020/7/20 9:20
234      **/
235     public static String getIpGeoApi() {
236         return getSysConfig("SNOWY_IP_GEO_API", String.class, false);
237     }
238
239     /**
240      * 获取阿里云定位appCode
241      *
242      * @author xuyuxiang
243      * @date 2020/7/20 10:33
244      **/
245     public static String getIpGeoAppCode() {
246         return getSysConfig("SNOWY_IP_GEO_APP_CODE", String.class, false);
247     }
248
249     /**
250      * 获取Oauth码云第三方登录的配置
251      *
252      * @author xuyuxiang
253      * @date 2020/7/28 17:16
254      **/
255     public static OauthConfigs getGiteeOauthConfigs() {
256         String snowyClientId = getSysConfig("SNOWY_OAUTH_GITEE_CLIENT_ID", String.class, true);
257         String snowyClientSecret = getSysConfig("SNOWY_OAUTH_GITEE_CLIENT_SECRET", String.class, true);
258         String snowyRedirectUri = getSysConfig("SNOWY_OAUTH_GITEE_REDIRECT_URI", String.class, true);
259
260         OauthConfigs oauthConfigs = new OauthConfigs();
261         oauthConfigs.setClientId(snowyClientId);
262         oauthConfigs.setClientSecret(snowyClientSecret);
263         oauthConfigs.setRedirectUri(snowyRedirectUri);
264         return oauthConfigs;
265     }
266
267     /**
268      * 获取OauthGithub第三方登录的配置
269      *
270      * @author xuyuxiang
271      * @date 2020/7/28 17:16
272      **/
273     public static OauthConfigs getGithubOauthConfigs() {
274         String snowyClientId = getSysConfig("SNOWY_OAUTH_GITHUB_CLIENT_ID", String.class, true);
275         String snowyClientSecret = getSysConfig("SNOWY_OAUTH_GITHUB_CLIENT_SECRET", String.class, true);
276         String snowyRedirectUri = getSysConfig("SNOWY_OAUTH_GITHUB_REDIRECT_URI", String.class, true);
277
278         OauthConfigs oauthConfigs = new OauthConfigs();
279         oauthConfigs.setClientId(snowyClientId);
280         oauthConfigs.setClientSecret(snowyClientSecret);
281         oauthConfigs.setRedirectUri(snowyRedirectUri);
282         return oauthConfigs;
283     }
284
285     /**
286      * 获取是否允许Oauth用户登陆的开关, 默认为false
287      *
288      * @author xuyuxiang
289      * @date 2020/7/28 16:37
290      **/
291     public static Boolean getEnableOauthLogin() {
292         return getSysConfigWithDefault("SNOWY_ENABLE_OAUTH_LOGIN", Boolean.class, false);
293     }
294
295     /**
296      * 获取前端项目的地址
297      *
298      * @author xuyuxiang
299      * @date 2020/7/29 14:08
300      **/
301     public static String getWebUrl() {
302         return getSysConfig("SNOWY_WEB_URL", String.class, true);
303     }
304
305     /**
306      * 获取支付宝支付成功转发地址
307      *
308      * @author xuyuxiang
309      * @date 2020/7/29 14:08
310      **/
311     public static String getAlipayReturnUrl() {
312         return getSysConfig("SNOWY_ALIPAY_RETURN_URL", String.class, true);
313     }
314
315     /**
316      * 获取OnlyOffice地址
317      *
318      * @author xuyuxiang
319      * @date 2020/7/29 14:08
320      **/
321     public static String getOnlyOfficeUrl() {
322         return getSysConfig("SNOWY_ONLY_OFFICE_SERVICE_URL", String.class, true);
323     }
324
325     /**
cfde48 326      * 获取blog资源地址访问前缀
I 327      * @return
328      */
329     public static String getBlogSourcePrefix() {
330         return getSysConfig("visit", String.class, true);
331     }
332
333     /**
9bcb19 334      * 获取config表中的配置,如果为空返回默认值
I 335      *
336      * @param configCode   变量名称,对应sys_config表中的code
337      * @param clazz        返回变量值的类型
338      * @param defaultValue 如果结果为空返回此默认值
339      * @author yubaoshan
340      * @date 2020/6/20 22:03
341      */
342     public static <T> T getSysConfigWithDefault(String configCode, Class<T> clazz, T defaultValue) {
343         String configValue = ConstantContext.me().getStr(configCode);
344         if (ObjectUtil.isEmpty(configValue)) {
345             // 将默认值加入到缓存常量
346             log.warn(">>> 系统配置sys_config表中存在空项,configCode为:{},系统采用默认值:{}", configCode, defaultValue);
347             ConstantContext.me().put(configCode, defaultValue);
348             return defaultValue;
349         } else {
350             try {
351                 return Convert.convert(clazz, configValue);
352             } catch (Exception e) {
353                 return defaultValue;
354             }
355         }
356     }
357
358     /**
359      * 获取config表中的配置,如果为空,是否抛出异常
360      *
361      * @param configCode   变量名称,对应sys_config表中的code
362      * @param clazz        返回变量值的类型
363      * @param nullThrowExp 若为空是否抛出异常
364      * @author yubaoshan
365      * @date 2020/6/20 22:03
366      */
367     public static <T> T getSysConfig(String configCode, Class<T> clazz, Boolean nullThrowExp) {
368         String configValue = ConstantContext.me().getStr(configCode);
369         if (ObjectUtil.isEmpty(configValue)) {
370             if (nullThrowExp) {
371                 String format = StrUtil.format(">>> 系统配置sys_config表中存在空项,configCode为:{}", configCode);
372                 log.error(format);
373                 throw new ServiceException(CONSTANT_EMPTY.getCode(), format);
374             } else {
375                 return null;
376             }
377         } else {
378             try {
379                 return Convert.convert(clazz, configValue);
380             } catch (Exception e) {
381                 if (nullThrowExp) {
382                     String format = StrUtil.format(">>> 系统配置sys_config表中存在格式错误的值,configCode={},configValue={}", configCode, configValue);
383                     log.error(format);
384                     throw new ServiceException(CONSTANT_EMPTY.getCode(), format);
385                 } else {
386                     return null;
387                 }
388             }
389         }
390     }
391
392     /**
393      * 获取验证码 开关标识
394      *
395      * @author Jax
396      * @Date 2021/1/21 15:22
397      */
398     public static Boolean getCaptchaOpenFlag() {
399         return getSysConfigWithDefault("SNOWY_CAPTCHA_OPEN", Boolean.class, true);
400     }
401
402     /**
403      * 获取加解密的配置
404      *
405      * @author yubaoshan
406      */
407     public static CryptogramConfigs getCryptogramConfigs() {
408         boolean snowyTokenEncDec = getSysConfigWithDefault("SNOWY_TOKEN_ENCRYPTION_OPEN", Boolean.class, true);
409         boolean snowyVisLogEnc = getSysConfigWithDefault("SNOWY_VISLOG_ENCRYPTION_OPEN", Boolean.class, true);
410         boolean snowyOpLogEnc = getSysConfigWithDefault("SNOWY_OPLOG_ENCRYPTION_OPEN", Boolean.class, true);
411         boolean snowyFieldEncDec = getSysConfigWithDefault("SNOWY_FIELD_ENC_DEC_OPEN", Boolean.class, true);
412
413         CryptogramConfigs cryptogramConfigs = new CryptogramConfigs();
414         cryptogramConfigs.setTokenEncDec(snowyTokenEncDec);
415         cryptogramConfigs.setVisLogEnc(snowyVisLogEnc);
416         cryptogramConfigs.setOpLogEnc(snowyOpLogEnc);
417         cryptogramConfigs.setFieldEncDec(snowyFieldEncDec);
418         return cryptogramConfigs;
419     }
420
421 }