mirror of
https://github.com/continew-org/continew-admin.git
synced 2025-09-25 06:57:15 +08:00
新增:新增用户登录和退出 API(引入 Sa-Token 依赖,详情可见 README 介绍)
This commit is contained in:
@@ -0,0 +1,116 @@
|
||||
/*
|
||||
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package top.charles7c.cnadmin.common.util;
|
||||
|
||||
import lombok.AccessLevel;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
|
||||
import top.charles7c.cnadmin.common.exception.BadRequestException;
|
||||
|
||||
/**
|
||||
* 检查工具类
|
||||
*
|
||||
* @author Charles7c
|
||||
* @since 2022/12/21 20:56
|
||||
*/
|
||||
@Slf4j
|
||||
@NoArgsConstructor(access = AccessLevel.PRIVATE)
|
||||
public class CheckUtils {
|
||||
|
||||
/**
|
||||
* 如果为空,抛出异常
|
||||
*
|
||||
* @param obj
|
||||
* 被检测的对象
|
||||
* @param message
|
||||
* 错误信息
|
||||
*/
|
||||
public static void exIfNull(Object obj, String message) {
|
||||
if (obj == null) {
|
||||
log.error(message);
|
||||
throw new BadRequestException(message);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 如果为空,抛出异常
|
||||
*
|
||||
* @param str
|
||||
* 被检测的字符串
|
||||
* @param message
|
||||
* 错误信息
|
||||
*/
|
||||
public static void exIfBlank(CharSequence str, String message) {
|
||||
if (StrUtil.isBlank(str)) {
|
||||
log.error(message);
|
||||
throw new BadRequestException(message);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 如果相同,抛出异常
|
||||
*
|
||||
* @param obj1
|
||||
* 要比较的对象1
|
||||
* @param obj2
|
||||
* 要比较的对象2
|
||||
* @param message
|
||||
* 错误信息
|
||||
*/
|
||||
public static void exIfEqual(Object obj1, Object obj2, String message) {
|
||||
if (ObjectUtil.equals(obj1, obj2)) {
|
||||
log.error(message);
|
||||
throw new BadRequestException(message);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 如果不相同,抛出异常
|
||||
*
|
||||
* @param obj1
|
||||
* 要比较的对象1
|
||||
* @param obj2
|
||||
* 要比较的对象2
|
||||
* @param message
|
||||
* 错误信息
|
||||
*/
|
||||
public static void exIfNotEqual(Object obj1, Object obj2, String message) {
|
||||
if (ObjectUtil.notEqual(obj1, obj2)) {
|
||||
log.error(message);
|
||||
throw new BadRequestException(message);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 如果条件成立,抛出异常
|
||||
*
|
||||
* @param conditionSupplier
|
||||
* 条件
|
||||
* @param message
|
||||
* 错误信息
|
||||
*/
|
||||
public static void exIfCondition(java.util.function.BooleanSupplier conditionSupplier, String message) {
|
||||
if (conditionSupplier != null && conditionSupplier.getAsBoolean()) {
|
||||
log.error(message);
|
||||
throw new BadRequestException(message);
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,117 @@
|
||||
/*
|
||||
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package top.charles7c.cnadmin.common.util;
|
||||
|
||||
import java.util.function.Consumer;
|
||||
|
||||
import lombok.AccessLevel;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
/**
|
||||
* 异常工具类
|
||||
*
|
||||
* @author Charles7c
|
||||
* @since 2022/12/21 20:56
|
||||
*/
|
||||
@NoArgsConstructor(access = AccessLevel.PRIVATE)
|
||||
public class ExceptionUtils {
|
||||
|
||||
/**
|
||||
* 如果有异常,返回 null
|
||||
*
|
||||
* @param exSupplier
|
||||
* 可能会出现异常的方法执行
|
||||
* @param <T>
|
||||
* /
|
||||
* @return /
|
||||
*/
|
||||
public static <T> T exToNull(ExSupplier<T> exSupplier) {
|
||||
return exToDefault(exSupplier, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* 如果有异常,执行异常处理
|
||||
*
|
||||
* @param supplier
|
||||
* 可能会出现异常的方法执行
|
||||
* @param exConsumer
|
||||
* 异常处理
|
||||
* @param <T>
|
||||
* /
|
||||
* @return /
|
||||
*/
|
||||
public static <T> T exToNull(ExSupplier<T> supplier, Consumer<Exception> exConsumer) {
|
||||
return exToDefault(supplier, null, exConsumer);
|
||||
}
|
||||
|
||||
/**
|
||||
* 如果有异常,返回默认值
|
||||
*
|
||||
* @param exSupplier
|
||||
* 可能会出现异常的方法执行
|
||||
* @param defaultValue
|
||||
* 默认值
|
||||
* @param <T>
|
||||
* /
|
||||
* @return /
|
||||
*/
|
||||
public static <T> T exToDefault(ExSupplier<T> exSupplier, T defaultValue) {
|
||||
return exToDefault(exSupplier, defaultValue, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* 如果有异常,执行异常处理,返回默认值
|
||||
*
|
||||
* @param exSupplier
|
||||
* 可能会出现异常的方法执行
|
||||
* @param defaultValue
|
||||
* 默认值
|
||||
* @param exConsumer
|
||||
* 异常处理
|
||||
* @param <T>
|
||||
* /
|
||||
* @return /
|
||||
*/
|
||||
public static <T> T exToDefault(ExSupplier<T> exSupplier, T defaultValue, Consumer<Exception> exConsumer) {
|
||||
try {
|
||||
return exSupplier.get();
|
||||
} catch (Exception ex) {
|
||||
if (exConsumer != null) {
|
||||
exConsumer.accept(ex);
|
||||
}
|
||||
return defaultValue;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 异常提供者
|
||||
*
|
||||
* @param <T>
|
||||
* /
|
||||
*/
|
||||
public interface ExSupplier<T> {
|
||||
/**
|
||||
* 获取返回值
|
||||
*
|
||||
* @return /
|
||||
* @throws Exception
|
||||
* /
|
||||
*/
|
||||
T get() throws Exception;
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,73 @@
|
||||
/*
|
||||
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package top.charles7c.cnadmin.common.util;
|
||||
|
||||
import lombok.AccessLevel;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import cn.hutool.core.codec.Base64;
|
||||
import cn.hutool.crypto.SecureUtil;
|
||||
import cn.hutool.crypto.asymmetric.KeyType;
|
||||
|
||||
/**
|
||||
* 加密/解密工具类
|
||||
*
|
||||
* @author Charles7c
|
||||
* @since 2022/12/21 21:41
|
||||
*/
|
||||
@NoArgsConstructor(access = AccessLevel.PRIVATE)
|
||||
public class SecureUtils {
|
||||
|
||||
/**
|
||||
* 公钥加密
|
||||
*
|
||||
* @param data
|
||||
* 要加密的内容
|
||||
* @param publicKey
|
||||
* 公钥
|
||||
* @return 公钥加密并 Base64 加密后的内容
|
||||
*/
|
||||
public static String encryptByRsaPublicKey(String data, String publicKey) {
|
||||
return Base64.encode(SecureUtil.rsa(null, publicKey).encrypt(data, KeyType.PublicKey));
|
||||
}
|
||||
|
||||
/**
|
||||
* 私钥解密
|
||||
*
|
||||
* @param data
|
||||
* 要解密的内容(Base64 加密过)
|
||||
* @param privateKey
|
||||
* 私钥
|
||||
* @return 解密后的内容
|
||||
*/
|
||||
public static String decryptByRsaPrivateKey(String data, String privateKey) {
|
||||
return new String(SecureUtil.rsa(privateKey, null).decrypt(Base64.decode(data), KeyType.PrivateKey));
|
||||
}
|
||||
|
||||
/**
|
||||
* MD5 加密
|
||||
*
|
||||
* @param data
|
||||
* 要加密的内容
|
||||
* @param salt
|
||||
* 盐
|
||||
* @return 加密后的内容
|
||||
*/
|
||||
public static String md5Salt(String data, String salt) {
|
||||
return SecureUtil.md5(SecureUtil.md5(data) + salt);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user