chore: top.charles7c.continew => top.continew

1.groupId 及基础包名调整,更短的包名,优化品牌形象
2.全局代码格式化
This commit is contained in:
2024-04-22 20:29:17 +08:00
parent da3a4e0756
commit 08eeabc47d
238 changed files with 1121 additions and 1154 deletions

View File

@@ -0,0 +1,69 @@
/*
* 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.continew.admin.common.util;
import cn.hutool.core.codec.Base64;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.asymmetric.KeyType;
import top.continew.admin.common.config.properties.RsaProperties;
import top.continew.starter.core.util.validate.ValidationUtils;
/**
* 加密/解密工具类
*
* @author Charles7c
* @since 2022/12/21 21:41
*/
public class SecureUtils {
private 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 加密过)
* @return 解密后的内容
*/
public static String decryptByRsaPrivateKey(String data) {
String privateKey = RsaProperties.PRIVATE_KEY;
ValidationUtils.throwIfBlank(privateKey, "请配置 RSA 私钥");
return decryptByRsaPrivateKey(data, privateKey);
}
/**
* 私钥解密
*
* @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));
}
}

View File

@@ -0,0 +1,139 @@
/*
* 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.continew.admin.common.util.helper;
import cn.dev33.satoken.context.SaHolder;
import cn.dev33.satoken.exception.NotLoginException;
import cn.dev33.satoken.session.SaSession;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.servlet.JakartaServletUtil;
import cn.hutool.extra.spring.SpringUtil;
import jakarta.servlet.http.HttpServletRequest;
import top.continew.admin.common.constant.CacheConstants;
import top.continew.admin.common.model.dto.LoginUser;
import top.continew.starter.core.util.ExceptionUtils;
import top.continew.starter.core.util.IpUtils;
import top.continew.starter.extension.crud.service.CommonUserService;
import top.continew.starter.web.util.ServletUtils;
import java.time.LocalDateTime;
/**
* 登录助手
*
* @author Charles7c
* @author Lion Li<a href="https://gitee.com/dromara/RuoYi-Vue-Plus">RuoYi-Vue-Plus</a>
* @since 2022/12/24 12:58
*/
public class LoginHelper {
private LoginHelper() {
}
/**
* 用户登录并缓存用户信息
*
* @param loginUser 登录用户信息
* @return 令牌
*/
public static String login(LoginUser loginUser) {
// 记录登录信息
HttpServletRequest request = ServletUtils.getRequest();
loginUser.setIp(JakartaServletUtil.getClientIP(request));
loginUser.setAddress(IpUtils.getAddress(loginUser.getIp()));
loginUser.setBrowser(ServletUtils.getBrowser(request));
loginUser.setLoginTime(LocalDateTime.now());
loginUser.setOs(StrUtil.subBefore(ServletUtils.getOs(request), " or", false));
// 登录并缓存用户信息
StpUtil.login(loginUser.getId());
SaHolder.getStorage().set(CacheConstants.LOGIN_USER_KEY, loginUser);
String tokenValue = StpUtil.getTokenValue();
loginUser.setToken(tokenValue);
StpUtil.getTokenSession().set(CacheConstants.LOGIN_USER_KEY, loginUser);
return tokenValue;
}
/**
* 获取登录用户信息
*
* @return 登录用户信息
* @throws NotLoginException 未登录异常
*/
public static LoginUser getLoginUser() throws NotLoginException {
StpUtil.checkLogin();
LoginUser loginUser = (LoginUser)SaHolder.getStorage().get(CacheConstants.LOGIN_USER_KEY);
if (null != loginUser) {
return loginUser;
}
SaSession tokenSession = StpUtil.getTokenSession();
loginUser = (LoginUser)tokenSession.get(CacheConstants.LOGIN_USER_KEY);
SaHolder.getStorage().set(CacheConstants.LOGIN_USER_KEY, loginUser);
return loginUser;
}
/**
* 根据 Token 获取登录用户信息
*
* @param token 用户 Token
* @return 登录用户信息
*/
public static LoginUser getLoginUser(String token) {
SaSession tokenSession = StpUtil.getTokenSessionByToken(token);
if (null == tokenSession) {
return null;
}
return (LoginUser)tokenSession.get(CacheConstants.LOGIN_USER_KEY);
}
/**
* 获取登录用户 ID
*
* @return 登录用户 ID
*/
public static Long getUserId() {
return getLoginUser().getId();
}
/**
* 获取登录用户名
*
* @return 登录用户名
*/
public static String getUsername() {
return getLoginUser().getUsername();
}
/**
* 获取登录用户昵称
*
* @return 登录用户昵称
*/
public static String getNickname() {
return getNickname(getUserId());
}
/**
* 获取登录用户昵称
*
* @param userId 登录用户 ID
* @return 登录用户昵称
*/
public static String getNickname(Long userId) {
return ExceptionUtils.exToNull(() -> SpringUtil.getBean(CommonUserService.class).getNicknameById(userId));
}
}