算法工具类

v1.4.1
Parker 5 years ago
parent df30ea6bac
commit 98b4c1e684

@ -0,0 +1,152 @@
/**
* Copyright 2020 OPSLI https://www.opsli.com
* <p>
* 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
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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 org.opsli.api.web.system.other.crypto;
import org.opsli.api.base.result.ResultVo;
import org.opsli.api.wrapper.system.other.crypto.OtherCryptoAsymmetricModel;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* @BelongsProject: opsli-boot
* @BelongsPackage: org.opsli.api.web.other.crypto
* @Author: Parker
* @CreateTime: 2021-02-10 17:09:34
* @Description:
*
* API @GetMapping @PostMapping
* Mapping Controller
*
*
*
*
*/
public interface OtherCryptoAsymmetricRestApi {
/** 标题 */
String TITLE = "非对称加密";
/**
*
* @param model
* @return ResultVo
*/
@GetMapping("/get")
ResultVo<OtherCryptoAsymmetricModel> get(OtherCryptoAsymmetricModel model);
/**
*
* @param pageNo
* @param pageSize
* @param request request
* @return ResultVo
*/
@GetMapping("/findPage")
ResultVo<?> findPage(
@RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
@RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
HttpServletRequest request
);
/**
*
* @param model
* @return ResultVo
*/
@PostMapping("/insert")
ResultVo<?> insert(@RequestBody OtherCryptoAsymmetricModel model);
/**
*
* @param model
* @return ResultVo
*/
@PostMapping("/update")
ResultVo<?> update(@RequestBody OtherCryptoAsymmetricModel model);
/**
*
* @param id ID
* @return ResultVo
*/
@PostMapping("/del")
ResultVo<?> del(String id);
/**
*
* @param ids ID
* @return ResultVo
*/
@PostMapping("/delAll")
ResultVo<?> delAll(String ids);
/**
* Excel
*
* Token
*
* 使2
*
* socketJava
* response javascript alert
*
* @param request request
* @param response response
* @return ResultVo
*/
@GetMapping("/exportExcel")
void exportExcel(HttpServletRequest request, HttpServletResponse response);
/**
* Excel
* @param request request
* @return ResultVo
*/
@PostMapping("/importExcel")
ResultVo<?> importExcel(MultipartHttpServletRequest request);
/**
* Excel
* @param response response
* @return ResultVo
*/
@GetMapping("/importExcel/template")
void importTemplate(HttpServletResponse response);
/**
*
* @param optionCode
* @return ResultVo
*/
@GetMapping("/getByCryptoType")
ResultVo<OtherCryptoAsymmetricModel> getByCryptoType(String optionCode);
}

@ -17,7 +17,6 @@
package org.opsli.api.wrapper.system.options;
import java.util.Date;
import com.alibaba.excel.annotation.ExcelProperty;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
@ -27,8 +26,6 @@ import org.opsli.common.annotation.validation.ValidationArgs;
import org.opsli.common.annotation.validation.ValidationArgsLenMax;
import org.opsli.common.enums.ValiArgsType;
import org.opsli.plugins.excel.annotation.ExcelInfo;
import com.fasterxml.jackson.annotation.JsonFormat;
import org.springframework.format.annotation.DateTimeFormat;
/**
* @BelongsProject: opsli-boot
@ -51,8 +48,6 @@ public class OptionsModel extends ApiWrapper {
@ExcelInfo
@ValidationArgs({ValiArgsType.IS_NOT_NULL, ValiArgsType.IS_GENERAL})
@ValidationArgsLenMax(100)
@JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private String optionCode;
@ -63,8 +58,6 @@ public class OptionsModel extends ApiWrapper {
@ExcelInfo
@ValidationArgs({ValiArgsType.IS_NOT_NULL, ValiArgsType.IS_GENERAL_WITH_CHINESE})
@ValidationArgsLenMax(200)
@JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private String optionName;
@ -74,8 +67,6 @@ public class OptionsModel extends ApiWrapper {
@ExcelProperty(value = "参数值", order = 3)
@ExcelInfo
@ValidationArgsLenMax(500)
@JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private String optionValue;

@ -0,0 +1,72 @@
/**
* Copyright 2020 OPSLI https://www.opsli.com
* <p>
* 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
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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 org.opsli.api.wrapper.system.other.crypto;
import java.util.Date;
import com.alibaba.excel.annotation.ExcelProperty;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.opsli.api.base.warpper.ApiWrapper;
import org.opsli.common.annotation.validation.ValidationArgs;
import org.opsli.common.annotation.validation.ValidationArgsLenMax;
import org.opsli.common.enums.ValiArgsType;
import org.opsli.plugins.excel.annotation.ExcelInfo;
import com.fasterxml.jackson.annotation.JsonFormat;
import org.springframework.format.annotation.DateTimeFormat;
/**
* @BelongsProject: opsli-boot
* @BelongsPackage: org.opsli.api.wrapper.other.crypto
* @Author: Parker
* @CreateTime: 2021-02-10 17:09:34
* @Description:
*/
@Data
@EqualsAndHashCode(callSuper = false)
public class OtherCryptoAsymmetricModel extends ApiWrapper {
/** 加解密类别 */
@ApiModelProperty(value = "加解密类别")
@ExcelProperty(value = "加解密类别", order = 1)
@ExcelInfo
@ValidationArgs({ValiArgsType.IS_NOT_NULL, ValiArgsType.IS_GENERAL})
@ValidationArgsLenMax(100)
private String cryptoType;
/** 公钥 */
@ApiModelProperty(value = "公钥")
@ExcelProperty(value = "公钥", order = 2)
@ExcelInfo
@ValidationArgs({ValiArgsType.IS_NOT_NULL})
@ValidationArgsLenMax(2000)
private String publicKey;
/** 私钥 */
@ApiModelProperty(value = "私钥")
@ExcelProperty(value = "私钥", order = 3)
@ExcelInfo
@ValidationArgs({ValiArgsType.IS_NOT_NULL})
@ValidationArgsLenMax(2000)
private String privateKey;
}

@ -10,7 +10,7 @@ import java.lang.annotation.*;
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface InterfaceEncryptAndDecrypt {
public @interface InterfaceCrypto {
/** 加密启用状态 */
boolean enable() default true;

@ -0,0 +1,45 @@
package org.opsli.common.enums;
/**
*
*
* @author Parker
*/
public enum CryptoAsymmetricType {
/** 非对称算法类型 */
RSA("RSA", "RSA 算法"),
SM2("SM2", "SM2 算法"),
ECIES("ECIES", "ECIES 算法"),
;
private final String code;
private final String desc;
public static CryptoAsymmetricType getCacheType(String cacheType) {
CryptoAsymmetricType[] var1 = values();
for (CryptoAsymmetricType type : var1) {
if (type.code.equalsIgnoreCase(cacheType)) {
return type;
}
}
return null;
}
public String getCode() {
return this.code;
}
public String getDesc() {
return this.desc;
}
// =================
CryptoAsymmetricType(final String code, final String desc) {
this.code = code;
this.desc = desc;
}
}

@ -0,0 +1,44 @@
package org.opsli.common.enums;
/**
*
*
* @author Parker
*/
public enum OptionsType {
/** 参数类型 */
/** 目前支持 RSA SM2 ECIES 3种模式 */
CRYPTO_ASYMMETRIC("crypto_asymmetric", "加解密-非对称"),
;
private final String code;
private final String desc;
public static OptionsType getCacheType(String cacheType) {
OptionsType[] var1 = values();
for (OptionsType type : var1) {
if (type.code.equalsIgnoreCase(cacheType)) {
return type;
}
}
return null;
}
public String getCode() {
return this.code;
}
public String getDesc() {
return this.desc;
}
// =================
OptionsType(final String code, final String desc) {
this.code = code;
this.desc = desc;
}
}

@ -29,7 +29,7 @@ import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.opsli.api.base.encrypt.BaseEncrypt;
import org.opsli.api.base.result.ResultVo;
import org.opsli.common.annotation.InterfaceEncryptAndDecrypt;
import org.opsli.common.annotation.InterfaceCrypto;
import org.opsli.common.exception.ServiceException;
import org.opsli.common.utils.Props;
import org.opsli.core.msg.CoreMsg;
@ -96,7 +96,7 @@ public class InterfaceEncryptAndDecryptAop {
return RSA_PRIVATE_KEY;
}
@Pointcut("@annotation(org.opsli.common.annotation.InterfaceEncryptAndDecrypt)")
@Pointcut("@annotation(org.opsli.common.annotation.InterfaceCrypto)")
public void encryptAndDecrypt() {
}
@ -115,8 +115,8 @@ public class InterfaceEncryptAndDecryptAop {
// 获得 方法
Method method = signature.getMethod();
// 获得方法注解
InterfaceEncryptAndDecrypt annotation =
method.getAnnotation(InterfaceEncryptAndDecrypt.class);
InterfaceCrypto annotation =
method.getAnnotation(InterfaceCrypto.class);
if(annotation != null){
// 1. 拆解请求数据

@ -0,0 +1,359 @@
/**
* Copyright 2020 OPSLI https://www.opsli.com
* <p>
* 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
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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 org.opsli.core.utils;
import cn.hutool.core.util.CharsetUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.SmUtil;
import cn.hutool.crypto.asymmetric.ECIES;
import cn.hutool.crypto.asymmetric.KeyType;
import cn.hutool.crypto.asymmetric.RSA;
import cn.hutool.crypto.asymmetric.SM2;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.opsli.api.base.result.ResultVo;
import org.opsli.api.web.system.other.crypto.OtherCryptoAsymmetricRestApi;
import org.opsli.api.wrapper.system.other.crypto.OtherCryptoAsymmetricModel;
import org.opsli.common.enums.CryptoAsymmetricType;
import org.opsli.common.exception.ServiceException;
import org.opsli.core.cache.local.CacheUtil;
import org.opsli.core.msg.CoreMsg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import java.util.Collection;
import static org.opsli.common.constants.OrderConstants.UTIL_ORDER;
/**
* @BelongsProject: opsli-boot
* @BelongsPackage: org.opsli.core.utils
* @Author: Parker
* @CreateTime: 2020-09-19 20:03
* @Description:
*/
@Slf4j
@Order(UTIL_ORDER)
@Component
@Lazy(false)
public class CryptoAsymmetricUtil {
/** 前缀 */
public static final String PREFIX_CODE = "crypto:asymmetric:code:";
/** 非对称加密 Api */
private static OtherCryptoAsymmetricRestApi otherCryptoAsymmetricRestApi;
/** Crypto KEY */
private static final String CRYPTO_KEY = "data";
/**
* cryptoAsymmetricType
* @param cryptoAsymmetricType
* @return
*/
public static OtherCryptoAsymmetricModel getCryptoAsymmetric(final CryptoAsymmetricType cryptoAsymmetricType){
if(cryptoAsymmetricType == null){
return null;
}
String typeCode = cryptoAsymmetricType.getCode();
// 缓存Key
String cacheKey = PREFIX_CODE + typeCode;
// 先从缓存里拿
OtherCryptoAsymmetricModel model = CacheUtil.getTimed(OtherCryptoAsymmetricModel.class, cacheKey);
if (model != null){
return model;
}
// 拿不到 --------
// 防止缓存穿透判断
boolean hasNilFlag = CacheUtil.hasNilFlag(cacheKey);
if(hasNilFlag){
return null;
}
try {
// 分布式加锁
if(!DistributedLockUtil.lock(cacheKey)){
// 无法申领分布式锁
log.error(CoreMsg.REDIS_EXCEPTION_LOCK.getMessage());
return null;
}
// 如果获得锁 则 再次检查缓存里有没有, 如果有则直接退出, 没有的话才发起数据库请求
model = CacheUtil.getTimed(OtherCryptoAsymmetricModel.class, cacheKey);
if (model != null){
return model;
}
// 查询数据库
ResultVo<OtherCryptoAsymmetricModel> resultVo = otherCryptoAsymmetricRestApi.getByCryptoType(typeCode);
if(resultVo.isSuccess()){
model = resultVo.getData();
// 存入缓存
CacheUtil.put(cacheKey, model);
}
}catch (Exception e){
log.error(e.getMessage(),e);
}finally {
// 释放锁
DistributedLockUtil.unlock(cacheKey);
}
if(model == null){
// 设置空变量 用于防止穿透判断
CacheUtil.putNilFlag(cacheKey);
return null;
}
return model;
}
// ============== 刷新缓存 ==============
/**
* -
* @param model
* @return
*/
public static boolean refresh(final OtherCryptoAsymmetricModel model){
if(model == null || StringUtils.isEmpty(model.getCryptoType())){
return true;
}
// 计数器
int count = 0;
OtherCryptoAsymmetricModel tmpModel = CacheUtil.getTimed(OtherCryptoAsymmetricModel.class, PREFIX_CODE + model.getCryptoType());
boolean hasNilFlag = CacheUtil.hasNilFlag(PREFIX_CODE + model.getCryptoType());
// 只要不为空 则执行刷新
if (hasNilFlag){
count++;
// 清除空拦截
boolean tmp = CacheUtil.delNilFlag(PREFIX_CODE + model.getCryptoType());
if(tmp){
count--;
}
}
if(tmpModel != null){
count++;
// 先删除
boolean tmp = CacheUtil.del(PREFIX_CODE + model.getCryptoType());
if(tmp){
count--;
}
}
return count == 0;
}
/**
*
* @param cryptoAsymmetricType
* @return
*/
public static OtherCryptoAsymmetricModel create(final CryptoAsymmetricType cryptoAsymmetricType){
OtherCryptoAsymmetricModel model = new OtherCryptoAsymmetricModel();
switch (cryptoAsymmetricType){
case RSA:
RSA rsa = SecureUtil.rsa();
model.setPublicKey(rsa.getPublicKeyBase64());
model.setPrivateKey(rsa.getPrivateKeyBase64());
break;
case SM2:
SM2 sm2 = SmUtil.sm2();
model.setPublicKey(sm2.getPublicKeyBase64());
model.setPrivateKey(sm2.getPrivateKeyBase64());
break;
case ECIES:
ECIES ecies = new ECIES();
model.setPublicKey(ecies.getPublicKeyBase64());
model.setPrivateKey(ecies.getPrivateKeyBase64());
break;
default:
break;
}
return model;
}
/**
*
* @param cryptoAsymmetricType
* @return
*/
public static String encrypt(final CryptoAsymmetricType cryptoAsymmetricType, final Object data){
JSONObject jsonObject = new JSONObject();
jsonObject.put(CRYPTO_KEY, data);
// 原始/加密 数据
String encryptedStr = jsonObject.toString();
OtherCryptoAsymmetricModel cryptoAsymmetric = getCryptoAsymmetric(cryptoAsymmetricType);
// 如果找不到 公私钥 直接返回原始数据
if(cryptoAsymmetric == null){
return encryptedStr;
}
try {
switch (cryptoAsymmetricType){
case RSA:
RSA rsa = SecureUtil.rsa(cryptoAsymmetric.getPrivateKey(), cryptoAsymmetric.getPublicKey());
encryptedStr = rsa.encryptBase64(
StrUtil.bytes(encryptedStr, CharsetUtil.CHARSET_UTF_8), KeyType.PublicKey);
break;
case SM2:
SM2 sm2 = SmUtil.sm2(cryptoAsymmetric.getPrivateKey(), cryptoAsymmetric.getPublicKey());
encryptedStr = sm2.encryptBase64(
StrUtil.bytes(encryptedStr, CharsetUtil.CHARSET_UTF_8), KeyType.PublicKey);
break;
case ECIES:
ECIES ecies = new ECIES(cryptoAsymmetric.getPrivateKey(), cryptoAsymmetric.getPublicKey());
encryptedStr = ecies.encryptBase64(
StrUtil.bytes(encryptedStr, CharsetUtil.CHARSET_UTF_8), KeyType.PublicKey);
break;
default:
break;
}
}catch (Exception e){
// 加密失败
throw new ServiceException(CoreMsg.OTHER_EXCEPTION_RSA_EN);
}
return encryptedStr;
}
/**
*
* @param cryptoAsymmetricType
* @return
*/
public static String decrypt(final CryptoAsymmetricType cryptoAsymmetricType, final String data){
if(StringUtils.isEmpty(data)){
return null;
}
OtherCryptoAsymmetricModel cryptoAsymmetric = getCryptoAsymmetric(cryptoAsymmetricType);
// 如果找不到 公私钥 直接返回原始数据
if(cryptoAsymmetric == null){
return data;
}
String decryptStr;
try {
switch (cryptoAsymmetricType){
case RSA:
RSA rsa = SecureUtil.rsa(cryptoAsymmetric.getPrivateKey(), cryptoAsymmetric.getPublicKey());
decryptStr = rsa.decryptStr(data, KeyType.PrivateKey);
break;
case SM2:
SM2 sm2 = SmUtil.sm2(cryptoAsymmetric.getPrivateKey(), cryptoAsymmetric.getPublicKey());
decryptStr = sm2.decryptStr(data, KeyType.PrivateKey);
break;
case ECIES:
ECIES ecies = new ECIES(cryptoAsymmetric.getPrivateKey(), cryptoAsymmetric.getPublicKey());
decryptStr = ecies.decryptStr(data, KeyType.PrivateKey);
break;
default:
break;
}
}catch (Exception e){
// 加密失败
throw new ServiceException(CoreMsg.OTHER_EXCEPTION_RSA_EN);
}
//解密,因为编码传值时有空格出现
String decryptStr;
try{
String tmp = rsa.decryptStr(data, KeyType.PrivateKey);
JSONObject jsonObject = JSONObject.parseObject(tmp);
Object obj = jsonObject.get(rsaKey);
if(obj instanceof Collection){
decryptStr = jsonObject.getJSONArray(rsaKey).toJSONString();
}else{
decryptStr = jsonObject.getJSONObject(rsaKey).toJSONString();
}
}catch (Exception e){
// 解密失败
throw new ServiceException(CoreMsg.OTHER_EXCEPTION_RSA_DE);
}
JSONObject jsonObject = new JSONObject();
jsonObject.put(CRYPTO_KEY, data);
// 原始/加密 数据
String encryptedStr = jsonObject.toString();
OtherCryptoAsymmetricModel cryptoAsymmetric = getCryptoAsymmetric(cryptoAsymmetricType);
// 如果找不到 公私钥 直接返回原始数据
if(cryptoAsymmetric == null){
return encryptedStr;
}
try {
switch (cryptoAsymmetricType){
case RSA:
RSA rsa = SecureUtil.rsa(cryptoAsymmetric.getPrivateKey(), cryptoAsymmetric.getPublicKey());
encryptedStr = rsa.encryptBase64(
StrUtil.bytes(encryptedStr, CharsetUtil.CHARSET_UTF_8), KeyType.PublicKey);
break;
case SM2:
SM2 sm2 = SmUtil.sm2(cryptoAsymmetric.getPrivateKey(), cryptoAsymmetric.getPublicKey());
encryptedStr = sm2.encryptBase64(
StrUtil.bytes(encryptedStr, CharsetUtil.CHARSET_UTF_8), KeyType.PublicKey);
break;
case ECIES:
ECIES ecies = new ECIES(cryptoAsymmetric.getPrivateKey(), cryptoAsymmetric.getPublicKey());
encryptedStr = ecies.encryptBase64(
StrUtil.bytes(encryptedStr, CharsetUtil.CHARSET_UTF_8), KeyType.PublicKey);
break;
default:
break;
}
}catch (Exception e){
// 加密失败
throw new ServiceException(CoreMsg.OTHER_EXCEPTION_RSA_EN);
}
return encryptedStr;
}
// =====================================
@Autowired
public static void setOtherCryptoAsymmetricRestApi(OtherCryptoAsymmetricRestApi otherCryptoAsymmetricRestApi) {
CryptoAsymmetricUtil.otherCryptoAsymmetricRestApi = otherCryptoAsymmetricRestApi;
}
}

@ -21,9 +21,7 @@ import org.opsli.api.base.result.ResultVo;
import org.opsli.api.web.system.menu.MenuApi;
import org.opsli.api.wrapper.system.menu.MenuModel;
import org.opsli.core.cache.local.CacheUtil;
import org.opsli.core.cache.pushsub.msgs.MenuMsgFactory;
import org.opsli.core.msg.CoreMsg;
import org.opsli.plugins.redis.RedisPlugin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.core.annotation.Order;
@ -47,10 +45,6 @@ public class MenuUtil {
/** 前缀 */
public static final String PREFIX_CODE = "menu:code:";
/** Redis插件 */
private static RedisPlugin redisPlugin;
/** 菜单 Api */
private static MenuApi menuApi;
@ -150,11 +144,6 @@ public class MenuUtil {
if(tmp){
count--;
}
// 发送通知消息
redisPlugin.sendMessage(
MenuMsgFactory.createMenuMsg(menu)
);
}
return count == 0;
@ -165,11 +154,6 @@ public class MenuUtil {
// =====================================
@Autowired
public void setRedisPlugin(RedisPlugin redisPlugin) {
MenuUtil.redisPlugin = redisPlugin;
}
@Autowired
public void setMenuApi(MenuApi menuApi) {
MenuUtil.menuApi = menuApi;

@ -18,15 +18,11 @@ package org.opsli.core.utils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.opsli.api.base.result.ResultVo;
import org.opsli.api.web.system.menu.MenuApi;
import org.opsli.api.web.system.options.OptionsApi;
import org.opsli.api.wrapper.system.menu.MenuModel;
import org.opsli.api.wrapper.system.options.OptionsModel;
import org.opsli.common.enums.OptionsType;
import org.opsli.core.cache.local.CacheUtil;
import org.opsli.core.cache.pushsub.msgs.MenuMsgFactory;
import org.opsli.core.cache.pushsub.msgs.OptionMsgFactory;
import org.opsli.core.msg.CoreMsg;
import org.opsli.plugins.redis.RedisPlugin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.core.annotation.Order;
@ -50,14 +46,23 @@ public class OptionsUtil {
/** 前缀 */
public static final String PREFIX_CODE = "options:code:";
/** Redis插件 */
private static RedisPlugin redisPlugin;
/** 参数 Api */
private static OptionsApi optionsApi;
/**
* optionsType
* @param optionsType
* @return
*/
public static OptionsModel getOptionByCode(OptionsType optionsType){
if(optionsType == null){
return null;
}
return OptionsUtil.getOptionByCode(optionsType.getCode());
}
/**
* optionCode
* @param optionCode
@ -153,11 +158,6 @@ public class OptionsUtil {
if(tmp){
count--;
}
// 发送通知消息
redisPlugin.sendMessage(
OptionMsgFactory.createOptionMsg(option)
);
}
return count == 0;
@ -168,10 +168,6 @@ public class OptionsUtil {
// =====================================
@Autowired
public void setRedisPlugin(RedisPlugin redisPlugin) {
OptionsUtil.redisPlugin = redisPlugin;
}
@Autowired
public void setOptionsApi(OptionsApi optionsApi) {

@ -21,9 +21,7 @@ import org.opsli.api.base.result.ResultVo;
import org.opsli.api.web.system.user.UserApi;
import org.opsli.api.wrapper.system.user.UserOrgRefModel;
import org.opsli.core.cache.local.CacheUtil;
import org.opsli.core.cache.pushsub.msgs.OrgMsgFactory;
import org.opsli.core.msg.CoreMsg;
import org.opsli.plugins.redis.RedisPlugin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.core.annotation.Order;
@ -47,10 +45,6 @@ public class OrgUtil {
/** 前缀 */
public static final String PREFIX_CODE = "org:userId:";
/** Redis插件 */
private static RedisPlugin redisPlugin;
/** 用户 Api */
private static UserApi userApi;
@ -150,11 +144,6 @@ public class OrgUtil {
if(tmp){
count--;
}
// 发送通知消息
redisPlugin.sendMessage(
OrgMsgFactory.createOrgMsg(orgRefModel)
);
}
return count == 0;
}
@ -164,11 +153,6 @@ public class OrgUtil {
// =====================================
@Autowired
public void setRedisPlugin(RedisPlugin redisPlugin) {
OrgUtil.redisPlugin = redisPlugin;
}
@Autowired
public void setUserApi(UserApi userApi) {
OrgUtil.userApi = userApi;

@ -21,9 +21,7 @@ import org.opsli.api.base.result.ResultVo;
import org.opsli.api.web.system.tenant.TenantApi;
import org.opsli.api.wrapper.system.tenant.TenantModel;
import org.opsli.core.cache.local.CacheUtil;
import org.opsli.core.cache.pushsub.msgs.TenantMsgFactory;
import org.opsli.core.msg.CoreMsg;
import org.opsli.plugins.redis.RedisPlugin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.core.annotation.Order;
@ -47,10 +45,6 @@ public class TenantUtil {
/** 前缀 */
public static final String PREFIX_CODE = "tenant:id:";
/** Redis插件 */
private static RedisPlugin redisPlugin;
/** 租户 Api */
private static TenantApi tenantApi;
@ -150,11 +144,6 @@ public class TenantUtil {
if(tmp){
count--;
}
// 发送通知消息
redisPlugin.sendMessage(
TenantMsgFactory.createTenantMsg(tenantModel)
);
}
return count == 0;
@ -165,11 +154,6 @@ public class TenantUtil {
// =====================================
@Autowired
public void setRedisPlugin(RedisPlugin redisPlugin) {
TenantUtil.redisPlugin = redisPlugin;
}
@Autowired
public void setTenantApi(TenantApi tenantApi) {
TenantUtil.tenantApi = tenantApi;

@ -28,10 +28,8 @@ import org.opsli.common.api.TokenThreadLocal;
import org.opsli.common.exception.TokenException;
import org.opsli.core.autoconfigure.properties.GlobalProperties;
import org.opsli.core.cache.local.CacheUtil;
import org.opsli.core.cache.pushsub.msgs.UserMsgFactory;
import org.opsli.core.msg.CoreMsg;
import org.opsli.core.msg.TokenMsg;
import org.opsli.plugins.redis.RedisPlugin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.core.annotation.Order;
@ -61,10 +59,6 @@ public class UserUtil {
public static final String PREFIX_ID_MENUS = "userId:menus:";
public static final String PREFIX_USERNAME = "username:";
/** Redis插件 */
private static RedisPlugin redisPlugin;
/** 用户Service */
private static UserApi userApi;
@ -468,11 +462,6 @@ public class UserUtil {
}
}
// 发送通知消息
redisPlugin.sendMessage(
UserMsgFactory.createUserMsg(user)
);
return count == 0;
}
@ -508,11 +497,6 @@ public class UserUtil {
}
}
// 发送通知消息
redisPlugin.sendMessage(
UserMsgFactory.createUserRolesMsg(userId, null)
);
return count == 0;
}
@ -547,10 +531,6 @@ public class UserUtil {
}
}
// 发送通知消息
redisPlugin.sendMessage(
UserMsgFactory.createUserPermsMsg(userId, null)
);
return count == 0;
}
@ -586,11 +566,6 @@ public class UserUtil {
}
}
// 发送通知消息
redisPlugin.sendMessage(
UserMsgFactory.createUserMenusMsg(userId, null)
);
return count == 0;
}
@ -623,11 +598,6 @@ public class UserUtil {
// =====================================
@Autowired
public void setRedisPlugin(RedisPlugin redisPlugin) {
UserUtil.redisPlugin = redisPlugin;
}
@Autowired
public void setUserApi(UserApi userApi) {
UserUtil.userApi = userApi;

@ -17,6 +17,8 @@ opsli:
- sys_user_org_ref
- sys_area
- sys_org
- sys_options
- other_crypto_asymmetric
# 排除字段

@ -86,6 +86,12 @@ public enum SystemMsg implements BaseMsg {
*/
EXCEPTION_OPTIONS_UNIQUE(20700,"参数编号重复,该角色已存在"),
/**
* -
*/
EXCEPTION_OTHER_CRYPTO_UNIQUE(20800,"算法类型重复,该角色已存在"),
;
private final int code;

@ -22,14 +22,11 @@ import com.google.common.collect.Lists;
import org.apache.commons.lang3.StringUtils;
import org.opsli.api.wrapper.system.dict.DictDetailModel;
import org.opsli.api.wrapper.system.dict.DictModel;
import org.opsli.api.wrapper.system.dict.DictWrapper;
import org.opsli.common.constants.MyBatisConstants;
import org.opsli.common.enums.DictType;
import org.opsli.common.exception.ServiceException;
import org.opsli.common.utils.HumpUtil;
import org.opsli.core.base.service.impl.CrudServiceImpl;
import org.opsli.core.cache.pushsub.enums.CacheHandleType;
import org.opsli.core.cache.pushsub.msgs.DictMsgFactory;
import org.opsli.core.msg.CoreMsg;
import org.opsli.core.persistence.querybuilder.GenQueryBuilder;
import org.opsli.core.persistence.querybuilder.QueryBuilder;
@ -39,7 +36,6 @@ import org.opsli.modulars.system.dict.entity.SysDictDetail;
import org.opsli.modulars.system.dict.mapper.DictDetailMapper;
import org.opsli.modulars.system.dict.service.IDictDetailService;
import org.opsli.modulars.system.dict.service.IDictService;
import org.opsli.plugins.redis.RedisPlugin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@ -61,8 +57,6 @@ public class DictDetailServiceImpl extends CrudServiceImpl<DictDetailMapper, Sys
private DictDetailMapper mapper;
@Autowired
private IDictService iDictService;
@Autowired
private RedisPlugin redisPlugin;
/**
*
@ -83,24 +77,11 @@ public class DictDetailServiceImpl extends CrudServiceImpl<DictDetailMapper, Sys
DictDetailModel ret = super.insert(model);
if(ret != null){
List<DictDetailModel> listByTypeCode = this.findListByTypeCode(ret.getTypeCode());
if(listByTypeCode != null && listByTypeCode.size() > 0){
List<DictWrapper> dictWrapperList = Lists.newArrayListWithCapacity(listByTypeCode.size());
for (DictDetailModel dictDetailModel : listByTypeCode) {
DictWrapper dictWrapperModel = new DictWrapper();
dictWrapperModel.setTypeCode(dictDetailModel.getTypeCode());
dictWrapperModel.setDictName(dictDetailModel.getDictName());
dictWrapperModel.setDictValue(dictDetailModel.getDictValue());
dictWrapperList.add(dictWrapperModel);
}
if(CollUtil.isNotEmpty(listByTypeCode)){
// 删除缓存
this.clearCache(Collections.singletonList(
ret.getTypeCode()
));
// 广播缓存数据 - 通知其他服务器同步数据
redisPlugin.sendMessage(
DictMsgFactory.createMsg(dictWrapperList, CacheHandleType.DELETE)
);
}
}
@ -129,24 +110,11 @@ public class DictDetailServiceImpl extends CrudServiceImpl<DictDetailMapper, Sys
DictDetailModel ret = super.update(model);
if(ret != null){
List<DictDetailModel> listByTypeCode = this.findListByTypeCode(oldModel.getTypeCode());
if(listByTypeCode != null && listByTypeCode.size() > 0){
List<DictWrapper> dictWrapperList = Lists.newArrayListWithCapacity(listByTypeCode.size());
for (DictDetailModel dictDetailModel : listByTypeCode) {
DictWrapper dictWrapperModel = new DictWrapper();
dictWrapperModel.setTypeCode(dictDetailModel.getTypeCode());
dictWrapperModel.setDictName(dictDetailModel.getDictName());
dictWrapperModel.setDictValue(dictDetailModel.getDictValue());
dictWrapperList.add(dictWrapperModel);
}
if(CollUtil.isNotEmpty(listByTypeCode)){
// 删除缓存
this.clearCache(Collections.singletonList(
oldModel.getTypeCode()
));
// 广播缓存数据 - 通知其他服务器同步数据
redisPlugin.sendMessage(
DictMsgFactory.createMsg(dictWrapperList, CacheHandleType.DELETE)
);
}
}
@ -165,25 +133,10 @@ public class DictDetailServiceImpl extends CrudServiceImpl<DictDetailMapper, Sys
DictDetailModel dictModel = this.get(id);
boolean ret = super.delete(id);
if(ret){
List<DictDetailModel> listByTypeCode = this.findListByTypeCode(dictModel.getTypeCode());
if(listByTypeCode != null && listByTypeCode.size() > 0){
List<DictWrapper> dictWrapperList = Lists.newArrayListWithCapacity(listByTypeCode.size());
for (DictDetailModel dictDetailModel : listByTypeCode) {
DictWrapper dictWrapperModel = new DictWrapper();
dictWrapperModel.setTypeCode(dictDetailModel.getTypeCode());
dictWrapperModel.setDictName(dictDetailModel.getDictName());
dictWrapperModel.setDictValue(dictDetailModel.getDictValue());
dictWrapperList.add(dictWrapperModel);
}
// 删除缓存
this.clearCache(Collections.singletonList(
dictModel.getTypeCode()
));
// 广播缓存数据 - 通知其他服务器同步数据
redisPlugin.sendMessage(
DictMsgFactory.createMsg(dictWrapperList, CacheHandleType.DELETE)
);
}
// 删除缓存
this.clearCache(Collections.singletonList(
dictModel.getTypeCode()
));
}
return ret;
}
@ -203,26 +156,6 @@ public class DictDetailServiceImpl extends CrudServiceImpl<DictDetailMapper, Sys
this.clearCache(Collections.singletonList(
dictModel.getTypeCode()
));
List<DictDetailModel> listByTypeCode = this.findListByTypeCode(dictModel.getTypeCode());
if(listByTypeCode != null && listByTypeCode.size() > 0){
List<DictWrapper> dictWrapperList = Lists.newArrayListWithCapacity(listByTypeCode.size());
for (DictDetailModel dictDetailModel : listByTypeCode) {
DictWrapper dictWrapperModel = new DictWrapper();
dictWrapperModel.setTypeCode(dictDetailModel.getTypeCode());
dictWrapperModel.setDictName(dictDetailModel.getDictName());
dictWrapperModel.setDictValue(dictDetailModel.getDictValue());
dictWrapperList.add(dictWrapperModel);
}
// 删除缓存
this.clearCache(Collections.singletonList(
dictModel.getTypeCode()
));
// 广播缓存数据 - 通知其他服务器同步数据
redisPlugin.sendMessage(
DictMsgFactory.createMsg(dictWrapperList, CacheHandleType.DELETE)
);
}
}
return ret;
}
@ -243,31 +176,17 @@ public class DictDetailServiceImpl extends CrudServiceImpl<DictDetailMapper, Sys
boolean ret = super.deleteAll(ids);
if(ret){
if(list != null && list.size() > 0){
List<DictWrapper> dictWrapperModels = Lists.newArrayListWithCapacity(list.size());
if(CollUtil.isNotEmpty(list)){
Set<String> typeCodes = new HashSet<>();
// 封装数据
for (SysDictDetail sysDictDetail : list) {
DictWrapper dictWrapperModel = new DictWrapper();
dictWrapperModel.setTypeCode(sysDictDetail.getTypeCode());
dictWrapperModel.setDictName(sysDictDetail.getDictName());
dictWrapperModel.setDictValue(sysDictDetail.getDictValue());
dictWrapperModels.add(dictWrapperModel);
typeCodes.add(dictWrapperModel.getTypeCode());
typeCodes.add(sysDictDetail.getTypeCode());
}
List<String> typeCodeList = Lists.newArrayListWithCapacity(typeCodes.size());
typeCodeList.addAll(typeCodes);
// 删除缓存
this.clearCache(typeCodeList);
// 广播缓存数据 - 通知其他服务器同步数据
redisPlugin.sendMessage(
DictMsgFactory.createMsg(dictWrapperModels, CacheHandleType.DELETE)
);
}
}
@ -298,30 +217,16 @@ public class DictDetailServiceImpl extends CrudServiceImpl<DictDetailMapper, Sys
if(ret){
if(list != null && list.size() > 0){
List<DictWrapper> dictWrapperModels = Lists.newArrayListWithCapacity(list.size());
Set<String> typeCodes = new HashSet<>();
// 封装数据
for (SysDictDetail sysDictDetail : list) {
DictWrapper dictWrapperModel = new DictWrapper();
dictWrapperModel.setTypeCode(sysDictDetail.getTypeCode());
dictWrapperModel.setDictName(sysDictDetail.getDictName());
dictWrapperModel.setDictValue(sysDictDetail.getDictValue());
dictWrapperModels.add(dictWrapperModel);
typeCodes.add(dictWrapperModel.getTypeCode());
typeCodes.add(sysDictDetail.getTypeCode());
}
List<String> typeCodeList = Lists.newArrayListWithCapacity(typeCodes.size());
typeCodeList.addAll(typeCodes);
// 删除缓存
this.clearCache(typeCodeList);
// 广播缓存数据 - 通知其他服务器同步数据
redisPlugin.sendMessage(
DictMsgFactory.createMsg(dictWrapperModels, CacheHandleType.DELETE)
);
}
}
@ -347,24 +252,11 @@ public class DictDetailServiceImpl extends CrudServiceImpl<DictDetailMapper, Sys
boolean removeFlag = super.remove(queryWrapper);
if(removeFlag){
DictModel dictModel = iDictService.get(parentId);
List<DictDetailModel> listByTypeCode = this.findListByTypeCode(dictModel.getTypeCode());
if(listByTypeCode != null && listByTypeCode.size() > 0){
List<DictWrapper> dictWrapperList = Lists.newArrayListWithCapacity(listByTypeCode.size());
for (DictDetailModel dictDetailModel : listByTypeCode) {
DictWrapper dictWrapperModel = new DictWrapper();
dictWrapperModel.setTypeCode(dictDetailModel.getTypeCode());
dictWrapperModel.setDictName(dictDetailModel.getDictName());
dictWrapperModel.setDictValue(dictDetailModel.getDictValue());
dictWrapperList.add(dictWrapperModel);
}
if(dictModel != null){
// 删除缓存
this.clearCache(Collections.singletonList(
dictModel.getTypeCode()
));
// 广播缓存数据 - 通知其他服务器同步数据
redisPlugin.sendMessage(
DictMsgFactory.createMsg(dictWrapperList, CacheHandleType.DELETE)
);
}
}
return removeFlag;

@ -20,27 +20,23 @@ import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.opsli.api.base.result.ResultVo;
import org.opsli.core.autoconfigure.properties.GlobalProperties;
import org.opsli.core.utils.ValidationUtil;
import org.opsli.api.wrapper.system.tenant.TenantModel;
import org.opsli.api.wrapper.system.user.UserModel;
import org.opsli.common.annotation.InterfaceEncryptAndDecrypt;
import org.opsli.common.annotation.InterfaceCrypto;
import org.opsli.common.annotation.Limiter;
import org.opsli.common.api.TokenThreadLocal;
import org.opsli.common.enums.AlertType;
import org.opsli.common.exception.TokenException;
import org.opsli.common.thread.refuse.AsyncProcessQueueReFuse;
import org.opsli.common.utils.IPUtil;
import org.opsli.common.utils.OutputStreamUtil;
import org.opsli.core.filters.aspect.InterfaceEncryptAndDecryptAop;
import org.opsli.core.msg.TokenMsg;
import org.opsli.core.security.shiro.realm.JwtRealm;
import org.opsli.core.utils.*;
import org.opsli.modulars.system.login.entity.LoginForm;
import org.opsli.modulars.system.user.service.IUserService;
import org.opsli.plugins.redis.RedisPlugin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
@ -74,7 +70,7 @@ public class LoginRestController {
*
*/
@Limiter
@InterfaceEncryptAndDecrypt(responseEncrypt = false)
@InterfaceCrypto(responseEncrypt = false)
@ApiOperation(value = "登录", notes = "登录")
@PostMapping("/sys/login")
public ResultVo<UserTokenUtil.TokenRet> login(@RequestBody LoginForm form, HttpServletRequest request){

@ -0,0 +1,52 @@
/**
* Copyright 2020 OPSLI https://www.opsli.com
* <p>
* 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
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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 org.opsli.modulars.system.other.crypto.entity;
import java.util.Date;
import com.baomidou.mybatisplus.annotation.FieldStrategy;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableLogic;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.opsli.core.base.entity.BaseEntity;
/**
* @BelongsProject: opsli-boot
* @BelongsPackage: org.opsli.modulars.system.other.crypto.entity
* @Author: Parker
* @CreateTime: 2021-02-10 17:09:34
* @Description:
*/
@Data
@EqualsAndHashCode(callSuper = false)
public class OtherCryptoAsymmetric extends BaseEntity {
/** 加解密类别 */
private String cryptoType;
/** 公钥 */
private String publicKey;
/** 私钥 */
private String privateKey;
// ========================================
}

@ -0,0 +1,37 @@
/**
* Copyright 2020 OPSLI https://www.opsli.com
* <p>
* 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
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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 org.opsli.modulars.system.other.crypto.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.opsli.modulars.system.other.crypto.entity.OtherCryptoAsymmetric;
/**
* @BelongsProject: opsli-boot
* @BelongsPackage: org.opsli.modulars.system.other.crypto.mapper
* @Author: Parker
* @CreateTime: 2021-02-10 17:09:34
* @Description: Mapper
*/
@Mapper
public interface OtherCryptoAsymmetricMapper extends BaseMapper<OtherCryptoAsymmetric> {
}

@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="org.opsli.modulars.system.other.crypto.mapper.OtherCryptoAsymmetricMapper">
</mapper>

@ -0,0 +1,37 @@
/**
* Copyright 2020 OPSLI https://www.opsli.com
* <p>
* 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
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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 org.opsli.modulars.system.other.crypto.service;
import org.opsli.api.wrapper.system.other.crypto.OtherCryptoAsymmetricModel;
import org.opsli.core.base.service.interfaces.CrudServiceInterface;
import org.opsli.modulars.system.other.crypto.entity.OtherCryptoAsymmetric;
/**
* @BelongsProject: opsli-boot
* @BelongsPackage: org.opsli.modulars.system.other.crypto.service
* @Author: Parker
* @CreateTime: 2021-02-10 17:09:34
* @Description: Service
*/
public interface IOtherCryptoAsymmetricService extends CrudServiceInterface<OtherCryptoAsymmetric, OtherCryptoAsymmetricModel> {
}

@ -0,0 +1,190 @@
/**
* Copyright 2020 OPSLI https://www.opsli.com
* <p>
* 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
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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 org.opsli.modulars.system.other.crypto.service.impl;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.commons.lang3.StringUtils;
import org.opsli.api.wrapper.system.other.crypto.OtherCryptoAsymmetricModel;
import org.opsli.common.constants.MyBatisConstants;
import org.opsli.common.enums.CryptoAsymmetricType;
import org.opsli.common.exception.ServiceException;
import org.opsli.core.base.service.impl.CrudServiceImpl;
import org.opsli.core.msg.CoreMsg;
import org.opsli.core.utils.CryptoAsymmetricUtil;
import org.opsli.modulars.system.SystemMsg;
import org.opsli.modulars.system.other.crypto.entity.OtherCryptoAsymmetric;
import org.opsli.modulars.system.other.crypto.mapper.OtherCryptoAsymmetricMapper;
import org.opsli.modulars.system.other.crypto.service.IOtherCryptoAsymmetricService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.Collections;
import java.util.List;
/**
* @BelongsProject: opsli-boot
* @BelongsPackage: org.opsli.modulars.system.other.crypto.service.impl
* @Author: Parker
* @CreateTime: 2021-02-10 17:09:34
* @Description: Service Impl
*/
@Service
public class OtherCryptoAsymmetricServiceImpl extends CrudServiceImpl<OtherCryptoAsymmetricMapper, OtherCryptoAsymmetric, OtherCryptoAsymmetricModel>
implements IOtherCryptoAsymmetricService {
@Autowired(required = false)
private OtherCryptoAsymmetricMapper mapper;
@Override
@Transactional(rollbackFor = Exception.class)
public OtherCryptoAsymmetricModel insert(OtherCryptoAsymmetricModel model) {
if(model == null){
return null;
}
// 唯一验证
Integer count = this.uniqueVerificationByCode(model);
if(count != null && count > 0){
// 重复
throw new ServiceException(SystemMsg.EXCEPTION_OTHER_CRYPTO_UNIQUE);
}
return super.insert(model);
}
/***
*
* @param cryptoAsymmetricType
* @return
*/
@Transactional(rollbackFor = Exception.class)
@Override
public OtherCryptoAsymmetricModel reset(CryptoAsymmetricType cryptoAsymmetricType) {
if(cryptoAsymmetricType == null){
return null;
}
// 唯一验证
OtherCryptoAsymmetricModel model = new OtherCryptoAsymmetricModel();
model.setCryptoType(cryptoAsymmetricType.getCode());
Integer count = this.uniqueVerificationByCode(model);
// 修改
if(count > 0){
}
// 新增
if(count != null && count > 0){
// 重复
throw new ServiceException(SystemMsg.EXCEPTION_OTHER_CRYPTO_UNIQUE);
}
model = super.update(model);
if(model != null){
// 清除缓存
this.clearCache(Collections.singletonList(model));
}
return model;
}
@Override
@Transactional(rollbackFor = Exception.class)
public boolean delete(String id) {
OtherCryptoAsymmetricModel model = super.get(id);
boolean ret = super.delete(id);
if(ret){
// 清除缓存
this.clearCache(Collections.singletonList(model));
}
return ret;
}
@Override
@Transactional(rollbackFor = Exception.class)
public boolean deleteAll(String[] ids) {
QueryWrapper<OtherCryptoAsymmetric> queryWrapper = new QueryWrapper<>();
queryWrapper.in(MyBatisConstants.FIELD_ID, Convert.toList(String.class, ids));
List<OtherCryptoAsymmetricModel> modelList = super.transformTs2Ms(
super.findList(queryWrapper)
);
// 清除缓存
this.clearCache(modelList);
return super.deleteAll(ids);
}
// =======================
/**
*
* @param model model
* @return Integer
*/
@Transactional(readOnly = true)
public Integer uniqueVerificationByCode(OtherCryptoAsymmetricModel model){
if(model == null){
return null;
}
QueryWrapper<OtherCryptoAsymmetric> wrapper = new QueryWrapper<>();
wrapper.eq("crypto_type", model.getCryptoType());
// 重复校验排除自身
if(StringUtils.isNotEmpty(model.getId())){
wrapper.notIn(MyBatisConstants.FIELD_ID, model.getId());
}
return super.count(wrapper);
}
/**
*
* @param modelList
*/
private void clearCache(List<OtherCryptoAsymmetricModel> modelList){
// 清空缓存
if(CollUtil.isNotEmpty(modelList)){
int cacheCount = 0;
for (OtherCryptoAsymmetricModel model : modelList) {
cacheCount++;
boolean tmp = CryptoAsymmetricUtil.refresh(model);
if(tmp){
cacheCount--;
}
}
// 判断删除状态
if(cacheCount != 0){
// 删除缓存失败
throw new ServiceException(CoreMsg.CACHE_DEL_EXCEPTION);
}
}
}
}

@ -0,0 +1,232 @@
/**
* Copyright 2020 OPSLI https://www.opsli.com
* <p>
* 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
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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 org.opsli.modulars.system.other.crypto.web;
import cn.hutool.core.util.ReflectUtil;
import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.opsli.api.web.system.other.crypto.OtherCryptoAsymmetricRestApi;
import org.opsli.api.wrapper.system.options.OptionsModel;
import org.opsli.api.wrapper.system.other.crypto.OtherCryptoAsymmetricModel;
import org.opsli.common.utils.WrapperUtil;
import org.opsli.core.base.service.interfaces.CrudServiceInterface;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.opsli.common.annotation.RequiresPermissionsCus;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.opsli.api.base.result.ResultVo;
import org.opsli.common.annotation.ApiRestController;
import org.opsli.common.annotation.EnableLog;
import org.opsli.core.base.controller.BaseRestController;
import org.opsli.core.persistence.Page;
import org.opsli.core.persistence.querybuilder.QueryBuilder;
import org.opsli.core.persistence.querybuilder.WebQueryBuilder;
import org.opsli.modulars.system.options.entity.SysOptions;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Method;
import org.opsli.modulars.system.other.crypto.entity.OtherCryptoAsymmetric;
import org.opsli.modulars.system.other.crypto.service.IOtherCryptoAsymmetricService;
/**
* @BelongsProject: opsli-boot
* @BelongsPackage: org.opsli.modulars.system.other.crypto.web
* @Author: Parker
* @CreateTime: 2021-02-10 17:09:34
* @Description: Controller
*/
@Api(tags = "非对称加密")
@Slf4j
@ApiRestController("/other/crypto")
public class OtherCryptoAsymmetricRestController extends BaseRestController<OtherCryptoAsymmetric, OtherCryptoAsymmetricModel, IOtherCryptoAsymmetricService>
implements OtherCryptoAsymmetricRestApi {
/**
*
* @param model
* @return ResultVo
*/
@ApiOperation(value = "获得单条非对称加密", notes = "获得单条非对称加密 - ID")
@RequiresPermissions("other_crypto_select")
@Override
public ResultVo<OtherCryptoAsymmetricModel> get(OtherCryptoAsymmetricModel model) {
// 如果系统内部调用 则直接查数据库
if(model != null && model.getIzApi() != null && model.getIzApi()){
model = IService.get(model);
}
return ResultVo.success(model);
}
/**
*
* @param pageNo
* @param pageSize
* @param request request
* @return ResultVo
*/
@ApiOperation(value = "获得分页数据", notes = "获得分页数据 - 查询构造器")
@RequiresPermissions("other_crypto_select")
@Override
public ResultVo<?> findPage(Integer pageNo, Integer pageSize, HttpServletRequest request) {
QueryBuilder<OtherCryptoAsymmetric> queryBuilder = new WebQueryBuilder<>(entityClazz, request.getParameterMap());
Page<OtherCryptoAsymmetric, OtherCryptoAsymmetricModel> page = new Page<>(pageNo, pageSize);
page.setQueryWrapper(queryBuilder.build());
page = IService.findPage(page);
return ResultVo.success(page.getBootstrapData());
}
/**
*
* @param model
* @return ResultVo
*/
@ApiOperation(value = "新增非对称加密数据", notes = "新增非对称加密数据")
@RequiresPermissions("other_crypto_insert")
@EnableLog
@Override
public ResultVo<?> insert(OtherCryptoAsymmetricModel model) {
// 调用新增方法
IService.insert(model);
return ResultVo.success("新增非对称加密成功");
}
/**
*
* @param model
* @return ResultVo
*/
@ApiOperation(value = "修改非对称加密数据", notes = "修改非对称加密数据")
@RequiresPermissions("other_crypto_update")
@EnableLog
@Override
public ResultVo<?> update(OtherCryptoAsymmetricModel model) {
// 调用修改方法
IService.update(model);
return ResultVo.success("修改非对称加密成功");
}
/**
*
* @param id ID
* @return ResultVo
*/
@ApiOperation(value = "删除非对称加密数据", notes = "删除非对称加密数据")
@RequiresPermissions("other_crypto_update")
@EnableLog
@Override
public ResultVo<?> del(String id){
IService.delete(id);
return ResultVo.success("删除非对称加密成功");
}
/**
*
* @param ids ID
* @return ResultVo
*/
@ApiOperation(value = "批量删除非对称加密数据", notes = "批量删除非对称加密数据")
@RequiresPermissions("other_crypto_update")
@EnableLog
@Override
public ResultVo<?> delAll(String ids){
String[] idArray = Convert.toStrArray(ids);
IService.deleteAll(idArray);
return ResultVo.success("批量删除非对称加密成功");
}
/**
* Excel
* RequiresPermissionsCus
*
* Token
*
* 使2
*
* socketJava
* response javascript alert
*
* @param request request
* @param response response
*/
@ApiOperation(value = "导出Excel", notes = "导出Excel")
@RequiresPermissionsCus("other_crypto_export")
@EnableLog
@Override
public void exportExcel(HttpServletRequest request, HttpServletResponse response) {
// 当前方法
Method method = ReflectUtil.getMethodByName(this.getClass(), "exportExcel");
QueryBuilder<OtherCryptoAsymmetric> queryBuilder = new WebQueryBuilder<>(entityClazz, request.getParameterMap());
super.excelExport(OtherCryptoAsymmetricRestApi.TITLE, queryBuilder.build(), response, method);
}
/**
* Excel
* RequiresPermissions Shiro
* @param request request
* @return ResultVo
*/
@ApiOperation(value = "导入Excel", notes = "导入Excel")
@RequiresPermissions("other_crypto_import")
@EnableLog
@Override
public ResultVo<?> importExcel(MultipartHttpServletRequest request) {
return super.importExcel(request);
}
/**
* Excel
* RequiresPermissionsCus
* @param response response
*/
@ApiOperation(value = "导出Excel模版", notes = "导出Excel模版")
@RequiresPermissionsCus("other_crypto_import")
@Override
public void importTemplate(HttpServletResponse response) {
// 当前方法
Method method = ReflectUtil.getMethodByName(this.getClass(), "importTemplate");
super.importTemplate(OtherCryptoAsymmetricRestApi.TITLE, response, method);
}
// =======================
@Override
public ResultVo<OtherCryptoAsymmetricModel> getByCryptoType(String cryptoType) {
QueryWrapper<OtherCryptoAsymmetric> wrapper = new QueryWrapper<>();
wrapper.eq("crypto_type", cryptoType);
OtherCryptoAsymmetric entity = IService.getOne(wrapper);
return ResultVo.success(
WrapperUtil.transformInstance(entity, OtherCryptoAsymmetricModel.class)
);
}
}
Loading…
Cancel
Save