init src
This commit is contained in:
@@ -0,0 +1,55 @@
|
|||||||
|
package com.fly.boot.biz.base.entity;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.annotation.IdType;
|
||||||
|
import com.baomidou.mybatisplus.annotation.TableId;
|
||||||
|
import com.baomidou.mybatisplus.annotation.TableName;
|
||||||
|
import com.fly.boot.common.entity.BaseEntity;
|
||||||
|
////
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.EqualsAndHashCode;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 自营广告 实体类
|
||||||
|
*
|
||||||
|
* @author 刘玮杰
|
||||||
|
* @since 2023-06-16
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
@TableName("self_advertisement")
|
||||||
|
@EqualsAndHashCode(callSuper = true)
|
||||||
|
public class Advertisement extends BaseEntity {
|
||||||
|
|
||||||
|
@TableId(value = "id",type = IdType.ASSIGN_ID)
|
||||||
|
private Long id;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 广告地址
|
||||||
|
*/
|
||||||
|
private String mediaUrl;
|
||||||
|
/**
|
||||||
|
* 外挑地址
|
||||||
|
*/
|
||||||
|
private String outUrl;
|
||||||
|
/**
|
||||||
|
* 总时长
|
||||||
|
*/
|
||||||
|
private Integer totalDuration;
|
||||||
|
/**
|
||||||
|
* 最短时长
|
||||||
|
*/
|
||||||
|
private Integer minimumDuration;
|
||||||
|
/**
|
||||||
|
* 媒体类型 1-视频 2-图片
|
||||||
|
*/
|
||||||
|
private Integer mediaType;
|
||||||
|
/**
|
||||||
|
* 广告类型 1-开屏 2-任务
|
||||||
|
*/
|
||||||
|
private Integer advertisementType;
|
||||||
|
/**
|
||||||
|
* 备忘
|
||||||
|
*/
|
||||||
|
private String remark;
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
package com.fly.boot.biz.base.mapper;
|
||||||
|
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||||
|
import com.fly.boot.biz.base.entity.Address;
|
||||||
|
import org.apache.ibatis.annotations.Mapper;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 商品收货地址表(ShoppingAddress)数据库访问
|
||||||
|
*
|
||||||
|
* @author 刘伟杰
|
||||||
|
* @since 2023-10-28 16:39:28
|
||||||
|
*/
|
||||||
|
@Mapper
|
||||||
|
public interface AddressMapper extends BaseMapper<Address> {
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
@@ -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="com.fly.boot.biz.base.mapper.AddressMapper">
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
</mapper>
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
package com.fly.boot.biz.base.mapper;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||||
|
import com.fly.boot.biz.base.entity.Advertisement;
|
||||||
|
import org.apache.ibatis.annotations.Mapper;
|
||||||
|
|
||||||
|
@Mapper
|
||||||
|
public interface AdvertisementMapper extends BaseMapper<Advertisement> {
|
||||||
|
}
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
<?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="com.fly.boot.biz.base.mapper.AdvertisementMapper">
|
||||||
|
|
||||||
|
</mapper>
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
package com.fly.boot.biz.base.request;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.experimental.Accessors;
|
||||||
|
|
||||||
|
import javax.validation.constraints.NotNull;
|
||||||
|
import java.io.Serializable;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
@Accessors(chain = true)
|
||||||
|
public class AddressRemoveRequest implements Serializable {
|
||||||
|
|
||||||
|
@NotNull(message = "无效商品")
|
||||||
|
private List<Long> ids;
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,28 @@
|
|||||||
|
package com.fly.boot.biz.base.request;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.experimental.Accessors;
|
||||||
|
|
||||||
|
import javax.validation.constraints.NotBlank;
|
||||||
|
import java.io.Serializable;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
@Accessors(chain = true)
|
||||||
|
public class AddressSubmitRequest implements Serializable {
|
||||||
|
|
||||||
|
private Long id;
|
||||||
|
|
||||||
|
private Long regionsInfoId;
|
||||||
|
|
||||||
|
@NotBlank(message = "详细地址不能为空")
|
||||||
|
private String detail;
|
||||||
|
|
||||||
|
@NotBlank(message = "收货人不能为空")
|
||||||
|
private String consignee;
|
||||||
|
|
||||||
|
@NotBlank(message = "联系电话不能为空")
|
||||||
|
private String phone;
|
||||||
|
|
||||||
|
private Integer isDefault;
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,30 @@
|
|||||||
|
package com.fly.boot.biz.base.service;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.extension.service.IService;
|
||||||
|
import com.fly.boot.biz.base.entity.Address;
|
||||||
|
import com.fly.boot.biz.base.entity.RegionInfo;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 商品收货地址表(ShoppingAddress)服务接口
|
||||||
|
*
|
||||||
|
* @author 刘伟杰
|
||||||
|
* @since 2023-10-28 16:39:28
|
||||||
|
*/
|
||||||
|
public interface AddressService extends IService<Address> {
|
||||||
|
|
||||||
|
|
||||||
|
Map<Long, Address> selectCityByIds(Set<Long> ids);
|
||||||
|
|
||||||
|
Map<Long, Address> selectCityByUserId(@NotNull Set<Long> userId);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 区域人数统计
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
Map<Integer, Integer> selectUser(Map<Long, RegionInfo> regionInfoMap);
|
||||||
|
}
|
||||||
|
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
package com.fly.boot.biz.base.service;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.extension.service.IService;
|
||||||
|
import com.fly.boot.biz.base.entity.Advertisement;
|
||||||
|
|
||||||
|
public interface AdvertisementService extends IService<Advertisement> {
|
||||||
|
|
||||||
|
Advertisement getFirstScreen();
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,105 @@
|
|||||||
|
package com.fly.boot.biz.base.service.impl;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
||||||
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||||
|
import com.fly.boot.biz.base.entity.Address;
|
||||||
|
import com.fly.boot.biz.base.entity.RegionInfo;
|
||||||
|
import com.fly.boot.biz.base.mapper.AddressMapper;
|
||||||
|
import com.fly.boot.biz.base.service.AddressService;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
import org.springframework.util.CollectionUtils;
|
||||||
|
|
||||||
|
import java.util.*;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 商品收货地址表(ShoppingAddress)服务实现
|
||||||
|
*
|
||||||
|
* @author 刘伟杰
|
||||||
|
* @since 2023-10-28 16:39:28
|
||||||
|
*/
|
||||||
|
@Service
|
||||||
|
public class AddressServiceImpl extends ServiceImpl<AddressMapper, Address> implements AddressService {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Map<Long, Address> selectCityByIds(@NotNull Set<Long> ids) {
|
||||||
|
if(!CollectionUtils.isEmpty(ids)){
|
||||||
|
List<Address> result = list(Wrappers.lambdaQuery(Address.class)
|
||||||
|
.select(Address::getId,Address::getUserId,Address::getRegionsInfoId)
|
||||||
|
.in(Address::getId, ids));
|
||||||
|
if(!CollectionUtils.isEmpty(result)){
|
||||||
|
Map<Long,Address> map = new HashMap<>(result.size());
|
||||||
|
for (Address address : result) {
|
||||||
|
map.put(address.getId(), address);
|
||||||
|
}
|
||||||
|
return map;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return Collections.emptyMap();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Map<Long, Address> selectCityByUserId(@NotNull Set<Long> userId) {
|
||||||
|
List<Address> result = list(Wrappers.lambdaQuery(Address.class)
|
||||||
|
.select(Address::getId,Address::getUserId,Address::getRegionsInfoId)
|
||||||
|
.in(Address::getUserId, userId));
|
||||||
|
if(!CollectionUtils.isEmpty(result)){
|
||||||
|
Map<Long, Address> map = new HashMap<>(userId.size());
|
||||||
|
for (int i = result.size() - 1; i >= 0; i--) {
|
||||||
|
if(map.containsKey(result.get(i).getUserId())){
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
map.put(result.get(i).getUserId(), result.get(i));
|
||||||
|
}
|
||||||
|
return map;
|
||||||
|
}
|
||||||
|
return Collections.emptyMap();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Map<Integer, Integer> selectUser(Map<Long, RegionInfo> regionInfoMap) {
|
||||||
|
List<Address> all = list(Wrappers.lambdaQuery(Address.class).select(Address::getId, Address::getRegionsInfoId, Address::getUserId));
|
||||||
|
if(CollectionUtils.isEmpty(all)) {
|
||||||
|
return Collections.emptyMap();
|
||||||
|
}
|
||||||
|
//去重
|
||||||
|
Set<Long> user = new HashSet<>();
|
||||||
|
for (int i = all.size() - 1; i >= 0; i--) {
|
||||||
|
if(user.contains(all.get(i).getUserId())){
|
||||||
|
all.remove(i);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
user.add(all.get(i).getUserId());
|
||||||
|
}
|
||||||
|
Map<Long, List<Address>> group = all.stream().collect(Collectors.groupingBy(Address::getRegionsInfoId));
|
||||||
|
|
||||||
|
Map<Integer, Integer> result = new HashMap<>(all.size());
|
||||||
|
for (Map.Entry<Long, List<Address>> entry : group.entrySet()) {
|
||||||
|
result.put(entry.getKey().intValue(), entry.getValue().size());
|
||||||
|
}
|
||||||
|
//城市人数统计
|
||||||
|
for (RegionInfo info : regionInfoMap.values()) {
|
||||||
|
if(info.getLevel().equals(4)){
|
||||||
|
if(group.containsKey(info.getId())){
|
||||||
|
Integer level4 = info.getId().intValue();
|
||||||
|
Integer level4Num = result.get(level4);
|
||||||
|
RegionInfo district = regionInfoMap.get(info.getPid());
|
||||||
|
RegionInfo city = regionInfoMap.get(district.getPid());
|
||||||
|
|
||||||
|
Integer districtId = info.getPid().intValue();
|
||||||
|
Integer cityId = city.getId().intValue();
|
||||||
|
Integer provinceId = city.getPid().intValue();
|
||||||
|
|
||||||
|
result.put(districtId, result.containsKey(districtId) ? result.get(districtId) + level4Num : level4Num);
|
||||||
|
result.put(cityId, result.containsKey(cityId) ?result.get(cityId) + level4Num : level4Num);
|
||||||
|
result.put(provinceId, result.containsKey(provinceId) ? result.get(provinceId) + level4Num : level4Num);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -0,0 +1,33 @@
|
|||||||
|
package com.fly.boot.biz.base.service.impl;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||||
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||||
|
import com.fly.boot.biz.base.entity.Advertisement;
|
||||||
|
import com.fly.boot.biz.base.mapper.AdvertisementMapper;
|
||||||
|
import com.fly.boot.biz.base.service.AdvertisementService;
|
||||||
|
import com.xxl.job.core.handler.annotation.XxlJob;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import javax.annotation.PostConstruct;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class AdvertisementServiceImpl extends ServiceImpl<AdvertisementMapper, Advertisement> implements AdvertisementService {
|
||||||
|
|
||||||
|
private Advertisement firstScreen;
|
||||||
|
|
||||||
|
@PostConstruct
|
||||||
|
@XxlJob("index-refresh-first-screen")
|
||||||
|
public void initFirstScreen() {
|
||||||
|
firstScreen = getOne(new LambdaQueryWrapper<Advertisement>()
|
||||||
|
.eq(Advertisement::getAdvertisementType, 1)
|
||||||
|
.orderByDesc(Advertisement::getId)
|
||||||
|
.last("limit 1")
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Advertisement getFirstScreen() {
|
||||||
|
return firstScreen;
|
||||||
|
}
|
||||||
|
}
|
||||||
22
src/main/java/com/fly/boot/biz/base/vo/AdvertisementVO.java
Normal file
22
src/main/java/com/fly/boot/biz/base/vo/AdvertisementVO.java
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
package com.fly.boot.biz.base.vo;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
public class AdvertisementVO implements Serializable {
|
||||||
|
|
||||||
|
private String mediaUrl;
|
||||||
|
|
||||||
|
private String outUrl;
|
||||||
|
|
||||||
|
private Integer totalDuration;
|
||||||
|
|
||||||
|
private Integer minimumDuration;
|
||||||
|
|
||||||
|
private Integer mediaType;
|
||||||
|
|
||||||
|
private String remark;
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,32 @@
|
|||||||
|
package com.fly.boot.biz.mall.constant;
|
||||||
|
|
||||||
|
import lombok.Getter;
|
||||||
|
|
||||||
|
// 审核结果状态:1=仅退款,2=退货退款,3=换货补发,4=不退货仅退款
|
||||||
|
@Getter
|
||||||
|
public enum AfterApplyTypeEnums {
|
||||||
|
REFUNDS_FOR_NON_RECEIPTS(1, "未收货退款"),
|
||||||
|
REFUNDS_FOR_RETURN(2, "退货退款"),
|
||||||
|
// REFUNDS_FOR_REPLACEMENT(3, "换货补发"),
|
||||||
|
REFUNDS_FOR_ONLY_REFUND(4, "不退货仅退款"),
|
||||||
|
REJECTED(9, "拒绝"),
|
||||||
|
;
|
||||||
|
|
||||||
|
private final int code;
|
||||||
|
private final String description;
|
||||||
|
|
||||||
|
AfterApplyTypeEnums(int code, String description) {
|
||||||
|
this.code = code;
|
||||||
|
this.description = description;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 通过code获取对应的枚举值
|
||||||
|
public static AfterApplyTypeEnums getByCode(int code) {
|
||||||
|
for (AfterApplyTypeEnums status : AfterApplyTypeEnums.values()) {
|
||||||
|
if (status.getCode() == code) {
|
||||||
|
return status;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
throw new IllegalArgumentException("申请售后状态异常 code: " + code);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,26 @@
|
|||||||
|
package com.fly.boot.biz.mall.request;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.experimental.Accessors;
|
||||||
|
|
||||||
|
import javax.validation.constraints.NotNull;
|
||||||
|
import java.io.Serializable;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
@Accessors(chain = true)
|
||||||
|
public class AfterInfoRequest implements Serializable {
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* sku id
|
||||||
|
*/
|
||||||
|
@NotNull(message = "无效商品id")
|
||||||
|
private Long skuId;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 总订单id
|
||||||
|
*/
|
||||||
|
@NotNull(message = "无效订单id")
|
||||||
|
private Long summaryOrderId;
|
||||||
|
|
||||||
|
}
|
||||||
31
src/main/java/com/fly/boot/biz/mall/vo/AddressVO.java
Normal file
31
src/main/java/com/fly/boot/biz/mall/vo/AddressVO.java
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
package com.fly.boot.biz.mall.vo;
|
||||||
|
|
||||||
|
import com.fly.boot.common.util.string.StringPool;
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
public class AddressVO implements Serializable {
|
||||||
|
|
||||||
|
private Long id = -1L;
|
||||||
|
|
||||||
|
private Long regionsInfoId = -1L;
|
||||||
|
|
||||||
|
private String province = StringPool.EMPTY;
|
||||||
|
|
||||||
|
private String city = StringPool.EMPTY;
|
||||||
|
|
||||||
|
private String district = StringPool.EMPTY;
|
||||||
|
|
||||||
|
private String street = StringPool.EMPTY;
|
||||||
|
|
||||||
|
private String detail = StringPool.EMPTY;
|
||||||
|
|
||||||
|
private String consignee = StringPool.EMPTY;
|
||||||
|
|
||||||
|
private String phone = StringPool.EMPTY;
|
||||||
|
|
||||||
|
private Integer isDefault = -1;
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,250 @@
|
|||||||
|
package com.fly.boot.common.util.encrypt;
|
||||||
|
|
||||||
|
import org.apache.commons.codec.binary.Base64;
|
||||||
|
import org.apache.commons.codec.binary.Hex;
|
||||||
|
|
||||||
|
import javax.crypto.Cipher;
|
||||||
|
import javax.crypto.KeyGenerator;
|
||||||
|
import javax.crypto.NoSuchPaddingException;
|
||||||
|
import javax.crypto.SecretKey;
|
||||||
|
import javax.crypto.spec.SecretKeySpec;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
import java.security.InvalidKeyException;
|
||||||
|
import java.security.Key;
|
||||||
|
import java.security.NoSuchAlgorithmException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* AES 加/解密工具类
|
||||||
|
* 使用密钥时请使用 initKey() 方法来生成随机密钥
|
||||||
|
* initKey 方法内部使用 java.crypto.KeyGenerator 密钥生成器来生成特定于 AES 算法参数集的随机密钥
|
||||||
|
*/
|
||||||
|
public class AesEncryptUtil {
|
||||||
|
|
||||||
|
public static final String aesKey = "aa1f872935ad7d715a54c5fecdbc411f";
|
||||||
|
|
||||||
|
private AesEncryptUtil() {
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 密钥算法类型
|
||||||
|
*/
|
||||||
|
public static final String KEY_ALGORITHM = "AES";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 密钥的默认位长度
|
||||||
|
*/
|
||||||
|
public static final int DEFAULT_KEY_SIZE = 128;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 加解密算法/工作模式/填充方式
|
||||||
|
*/
|
||||||
|
private static final String ECB_PKCS_5_PADDING = "AES/ECB/PKCS5Padding";
|
||||||
|
public static final String ECB_NO_PADDING = "AES/ECB/NoPadding";
|
||||||
|
|
||||||
|
public static String base64Encode(byte[] bytes) {
|
||||||
|
return Base64.encodeBase64String(bytes);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static byte[] base64Decode(String base64Code) {
|
||||||
|
return Base64.decodeBase64(base64Code);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static byte[] aesEncryptToBytes(String content, String hexAesKey) throws Exception {
|
||||||
|
return encrypt(content, hexAesKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String aesEncrypt(String content, String hexAesKey) throws Exception {
|
||||||
|
return base64Encode(aesEncryptToBytes(content, hexAesKey));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String aesDecryptByBytes(String encryptBytes, String hexAesKey) throws Exception {
|
||||||
|
byte[] decrypt = decrypt(encryptBytes, hexAesKey);
|
||||||
|
return new String(decrypt, StandardCharsets.UTF_8);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String aesDecrypt(String encryptStr, String hexAesKey) throws Exception {
|
||||||
|
return aesDecryptByBytes(encryptStr, hexAesKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 生成 Hex 格式默认长度的随机密钥
|
||||||
|
* 字符串长度为 32,解二进制后为 16 个字节
|
||||||
|
*
|
||||||
|
* @return String Hex 格式的随机密钥
|
||||||
|
*/
|
||||||
|
public static String initHexKey() {
|
||||||
|
return Hex.encodeHexString(initKey());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 生成默认长度的随机密钥
|
||||||
|
* 默认长度为 128
|
||||||
|
*
|
||||||
|
* @return byte[] 二进制密钥
|
||||||
|
*/
|
||||||
|
public static byte[] initKey() {
|
||||||
|
return initKey(DEFAULT_KEY_SIZE);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 生成密钥
|
||||||
|
* 128、192、256 可选
|
||||||
|
*
|
||||||
|
* @param keySize 密钥长度
|
||||||
|
* @return byte[] 二进制密钥
|
||||||
|
*/
|
||||||
|
public static byte[] initKey(int keySize) {
|
||||||
|
// AES 要求密钥长度为 128 位、192 位或 256 位
|
||||||
|
if (keySize != 128 && keySize != 192 && keySize != 256) {
|
||||||
|
throw new RuntimeException("error keySize: " + keySize);
|
||||||
|
}
|
||||||
|
// 实例化
|
||||||
|
KeyGenerator keyGenerator;
|
||||||
|
try {
|
||||||
|
keyGenerator = KeyGenerator.getInstance(KEY_ALGORITHM);
|
||||||
|
} catch (NoSuchAlgorithmException e) {
|
||||||
|
throw new RuntimeException("no such algorithm exception: " + KEY_ALGORITHM, e);
|
||||||
|
}
|
||||||
|
keyGenerator.init(keySize);
|
||||||
|
// 生成秘密密钥
|
||||||
|
SecretKey secretKey = keyGenerator.generateKey();
|
||||||
|
// 获得密钥的二进制编码形式
|
||||||
|
return secretKey.getEncoded();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 转换密钥
|
||||||
|
*
|
||||||
|
* @param key 二进制密钥
|
||||||
|
* @return Key 密钥
|
||||||
|
*/
|
||||||
|
private static Key toKey(byte[] key) {
|
||||||
|
// 实例化 DES 密钥材料
|
||||||
|
return new SecretKeySpec(key, KEY_ALGORITHM);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 加密
|
||||||
|
*
|
||||||
|
* @param data 待加密数据
|
||||||
|
* @param key 密钥
|
||||||
|
* @return byte[] 加密的数据
|
||||||
|
*/
|
||||||
|
public static byte[] encrypt(String data, String key) {
|
||||||
|
return encrypt(data, key, ECB_PKCS_5_PADDING);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 加密
|
||||||
|
*
|
||||||
|
* @param data 待加密数据
|
||||||
|
* @param key 密钥
|
||||||
|
* @param cipherAlgorithm 算法/工作模式/填充模式
|
||||||
|
* @return byte[] 加密的数据
|
||||||
|
*/
|
||||||
|
public static byte[] encrypt(String data, String key, final String cipherAlgorithm) {
|
||||||
|
|
||||||
|
// 还原密钥
|
||||||
|
//Key k = toKey(key);
|
||||||
|
try {
|
||||||
|
Cipher cipher = getCipher(key, Cipher.ENCRYPT_MODE);
|
||||||
|
// 初始化,设置为加密模式
|
||||||
|
//cipher.init(Cipher.ENCRYPT_MODE, k);
|
||||||
|
|
||||||
|
// 发现使用 NoPadding 时,使用 ZeroPadding 填充
|
||||||
|
// if (ECB_NO_PADDING.equals(cipherAlgorithm)) {
|
||||||
|
// return cipher.doFinal(formatWithZeroPadding(data, cipher.getBlockSize()));
|
||||||
|
// }
|
||||||
|
|
||||||
|
byte[] byteContent = data.getBytes(StandardCharsets.UTF_8);
|
||||||
|
return cipher.doFinal(byteContent);
|
||||||
|
|
||||||
|
// 执行操作
|
||||||
|
//return cipher.doFinal(data);
|
||||||
|
} catch (Exception e) {
|
||||||
|
throw new RuntimeException("AES encrypt error", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 解密
|
||||||
|
*
|
||||||
|
* @param data 待解密数据
|
||||||
|
* @param key 密钥
|
||||||
|
* @return byte[] 解密的数据
|
||||||
|
*/
|
||||||
|
public static byte[] decrypt(String data, String key) {
|
||||||
|
return decrypt(data, key, ECB_PKCS_5_PADDING);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 解密
|
||||||
|
*
|
||||||
|
* @param data 待解密数据
|
||||||
|
* @param key 密钥
|
||||||
|
* @param cipherAlgorithm 算法/工作模式/填充模式
|
||||||
|
* @return byte[] 解密的数据
|
||||||
|
*/
|
||||||
|
public static byte[] decrypt(String data, String key, final String cipherAlgorithm) {
|
||||||
|
// 还原密钥
|
||||||
|
//Key k = toKey(key);
|
||||||
|
try {
|
||||||
|
Cipher cipher = getCipher(key, Cipher.DECRYPT_MODE);
|
||||||
|
// 初始化,设置为解密模式
|
||||||
|
//cipher.init(Cipher.DECRYPT_MODE, k);
|
||||||
|
|
||||||
|
// 发现使用 NoPadding 时,使用 ZeroPadding 填充
|
||||||
|
// if (ECB_NO_PADDING.equals(cipherAlgorithm)) {
|
||||||
|
// return removeZeroPadding(cipher.doFinal(data), cipher.getBlockSize());
|
||||||
|
// }
|
||||||
|
byte[] bytes = base64Decode(data);
|
||||||
|
bytes = cipher.doFinal(bytes);
|
||||||
|
return bytes;
|
||||||
|
|
||||||
|
// 执行操作
|
||||||
|
//return cipher.doFinal(data);
|
||||||
|
} catch (Exception e) {
|
||||||
|
throw new RuntimeException("AES decrypt error", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static byte[] formatWithZeroPadding(byte[] data, final int blockSize) {
|
||||||
|
final int length = data.length;
|
||||||
|
final int remainLength = length % blockSize;
|
||||||
|
|
||||||
|
if (remainLength > 0) {
|
||||||
|
byte[] inputData = new byte[length + blockSize - remainLength];
|
||||||
|
System.arraycopy(data, 0, inputData, 0, length);
|
||||||
|
return inputData;
|
||||||
|
}
|
||||||
|
return data;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static byte[] removeZeroPadding(byte[] data, final int blockSize) {
|
||||||
|
final int length = data.length;
|
||||||
|
final int remainLength = length % blockSize;
|
||||||
|
if (remainLength == 0) {
|
||||||
|
// 解码后的数据正好是块大小的整数倍,说明可能存在补 0 的情况,去掉末尾所有的 0
|
||||||
|
int i = length - 1;
|
||||||
|
while (i >= 0 && 0 == data[i]) {
|
||||||
|
i--;
|
||||||
|
}
|
||||||
|
byte[] outputData = new byte[i + 1];
|
||||||
|
System.arraycopy(data, 0, outputData, 0, outputData.length);
|
||||||
|
return outputData;
|
||||||
|
}
|
||||||
|
return data;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Cipher getCipher(String key, int cipherMode) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException {
|
||||||
|
Cipher cipher = Cipher.getInstance(ECB_PKCS_5_PADDING);
|
||||||
|
SecretKeySpec secretKey = new SecretKeySpec(key.getBytes(), "AES");
|
||||||
|
cipher.init(cipherMode, secretKey);
|
||||||
|
return cipher;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
90
src/test/java/com/fly/AesTest.java
Normal file
90
src/test/java/com/fly/AesTest.java
Normal file
File diff suppressed because one or more lines are too long
17
src/test/java/com/fly/boot/util/AesEncryptUtilTest.java
Normal file
17
src/test/java/com/fly/boot/util/AesEncryptUtilTest.java
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
package com.fly.boot.util;
|
||||||
|
|
||||||
|
import com.fly.boot.common.util.encrypt.AesEncryptUtil;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import static org.junit.Assert.*;
|
||||||
|
|
||||||
|
public class AesEncryptUtilTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testEncrypt() throws Exception {
|
||||||
|
String key = "aa1f872935ad7d715a54c5fecdbc411f";
|
||||||
|
String value = "{ 'phone':\"13800138000\" ,'password':\"1\",'code':\"123123\"}";
|
||||||
|
String v = AesEncryptUtil.aesEncrypt(value, key);
|
||||||
|
System.out.println(v);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user