加密字段模糊查询

This commit is contained in:
周源 2021-08-09 16:09:07 +08:00
parent c246fd759d
commit 689dd5088b
15 changed files with 152 additions and 42 deletions

View File

@ -7,6 +7,7 @@ import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.DynamicParameter;
import com.github.xiaoymin.knife4j.annotations.DynamicResponseParameters;
import com.ydool.boot.api.util.CodecUtils;
import com.ydool.boot.api.util.Kv;
import com.ydool.boot.common.Ydool;
import com.ydool.boot.common.cache.ConfigUtils;
@ -200,4 +201,10 @@ public class ApiUserController extends ApiBaseController {
renderJson(Ret.fail("该用户没有此身份"));
}
public static void main(String[] args) throws Exception{
String secretStr = CodecUtils.encrypt("admin");
System.out.println(secretStr);
System.out.println(CodecUtils.decrypt(secretStr));
}
}

View File

@ -1,8 +1,8 @@
package com.ydool.boot.api.util;
import cn.hutool.core.util.StrUtil;
import org.apache.commons.codec.DecoderException;
import org.apache.commons.codec.binary.Hex;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
@ -22,13 +22,14 @@ public class CodecUtils {
private static Cipher encryptCipher; // 加密cipher
private static Cipher decryptChipher; // 解密chipher
private static final String ENCODING = "UTF-8";
public static final String KEY = "ydool123ydool123";
static {
try {
encryptCipher = Cipher.getInstance("AES");
decryptChipher = Cipher.getInstance("AES");
encryptCipher.init(Cipher.ENCRYPT_MODE, generateAESKey("ydool", ENCODING));
decryptChipher.init(Cipher.DECRYPT_MODE, generateAESKey("ydool", ENCODING));
encryptCipher.init(Cipher.ENCRYPT_MODE, generateAESKey(KEY, ENCODING));
decryptChipher.init(Cipher.DECRYPT_MODE, generateAESKey(KEY, ENCODING));
} catch (Exception e) {
e.printStackTrace();
}
@ -36,11 +37,8 @@ public class CodecUtils {
public static SecretKeySpec generateAESKey(final String key, final String encoding) {
try {
final byte[] finalKey = new byte[16];
int i = 0;
for (byte b : key.getBytes(encoding))
finalKey[i++ % 16] ^= b;
return new SecretKeySpec(finalKey, "AES");
byte[] raw = key.getBytes(encoding);
return new SecretKeySpec(raw, "AES");
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
@ -55,7 +53,7 @@ public class CodecUtils {
public static String encrypt(String encryptString) {
try {
if (StrUtil.isBlank(encryptString)) return "";
return new String(Hex.encodeHex(encryptCipher.doFinal(encryptString.getBytes(ENCODING)))).toUpperCase();
return new BASE64Encoder().encode(encryptCipher.doFinal(encryptString.getBytes(ENCODING)));
} catch (BadPaddingException e) {
throw new RuntimeException(e);
} catch (UnsupportedEncodingException e) {
@ -75,12 +73,10 @@ public class CodecUtils {
if (StrUtil.isBlank(decryptString)) return "";
try {
return new String(decryptChipher.doFinal(Hex.decodeHex(decryptString.toCharArray())));
} catch (DecoderException nspe) {
return decryptString;
} catch (BadPaddingException nsae) {
return decryptString;
} catch (IllegalBlockSizeException ike) {
byte[] encryptedByte = new BASE64Decoder().decodeBuffer(decryptString);
return new String(decryptChipher.doFinal(encryptedByte));
} catch (Exception e) {
e.printStackTrace();
return decryptString;
}
}

View File

@ -0,0 +1,14 @@
package com.ydool.boot.modules.rddb.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.ydool.boot.modules.sys.entity.User;
import org.apache.ibatis.annotations.Mapper;
/**
* @author zhujingang
* @date 2020/5/19 4:25 PM
*/
@Mapper
public interface TestMapper extends BaseMapper<User> {
}

View File

@ -0,0 +1,66 @@
package com.ydool.boot.modules.rddb.service;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ydool.boot.api.util.CodecUtils;
import com.ydool.boot.core.service.BaseService;
import com.ydool.boot.modules.rddb.entity.Db;
import com.ydool.boot.modules.rddb.entity.Office;
import com.ydool.boot.modules.rddb.mapper.TestMapper;
import com.ydool.boot.modules.sys.entity.User;
import com.ydool.boot.modules.sys.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.ArrayList;
import java.util.List;
/**
* <p>
* 临时service
* </p>
*
* @author zhouyuan
* @since 2021年08月09日10:07:42
*/
@Service
public class TestService extends BaseService<TestMapper, User> {
@Autowired
private UserService userService;
@Autowired
private DbService dbService;
@Autowired
private OfficeService officeService;
@Transactional
public void encrypt() {
List<User> userList = userService.list();
List<User> addUserList = new ArrayList<>(userList.size());
List<Db> addDbList = new ArrayList<>(userList.size());
List<Office> addOfficeList = new ArrayList<>(userList.size());
userList.forEach(user -> {
user.setLoginName(CodecUtils.encrypt(user.getLoginName()));
user.setPhone(CodecUtils.encrypt(user.getPhone()));
addUserList.add(user);
Db db = dbService.getOne(new LambdaQueryWrapper<Db>().eq(Db::getUserId, user.getId()));
if (db != null) {
db.setPhone(CodecUtils.encrypt(db.getPhone()));
addDbList.add(db);
}
Office office = officeService.getOne(new LambdaQueryWrapper<Office>().eq(Office::getUserId, user.getId()));
if (office != null) {
office.setPhone(CodecUtils.encrypt(office.getPhone()));
addOfficeList.add(office);
}
});
userService.updateBatchById(addUserList);
dbService.updateBatchById(addDbList);
officeService.updateBatchById(addOfficeList);
System.out.println("手机号加密完成");
}
}

View File

@ -4,13 +4,14 @@ import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ydool.boot.api.util.CodecUtils;
import com.ydool.boot.common.result.Ret;
import com.ydool.boot.core.web.BaseController;
import com.ydool.boot.modules.rddb.entity.ActivityAuditUser;
import com.ydool.boot.modules.rddb.wrapper.UserDtoWrapper;
import com.ydool.boot.modules.rddb.service.ActivityAuditUserService;
import com.ydool.boot.modules.rddb.service.ActivityService;
import com.ydool.boot.modules.rddb.wrapper.ActivityAuditUserWrapper;
import com.ydool.boot.modules.rddb.wrapper.UserDtoWrapper;
import com.ydool.boot.modules.sys.entity.User;
import com.ydool.boot.modules.sys.service.UserService;
import org.springframework.stereotype.Controller;
@ -72,7 +73,7 @@ public class ActivityAuditUserController extends BaseController {
//县级工作人员
userQueryWrapper.eq("account_type", "admin");
if(StrUtil.isNotBlank(userName)) userQueryWrapper.like("user_name",userName);
if(StrUtil.isNotBlank(loginName)) userQueryWrapper.like("login_name",loginName);
userQueryWrapper.like(StrUtil.isNotBlank(loginName),"AES_DECRYPT(from_base64 (login_name),'"+ CodecUtils.KEY +"')",loginName);
Page page = userService.page(new Page(getPageNum(), getPageSize()), userQueryWrapper);
render(Ret.ok().paged(UserDtoWrapper.build().pageVO(page)));
}

View File

@ -5,10 +5,11 @@ import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ydool.boot.api.controller.ApiBaseController;
import com.ydool.boot.api.util.CodecUtils;
import com.ydool.boot.common.result.Ret;
import com.ydool.boot.modules.rddb.entity.AuditUser;
import com.ydool.boot.modules.rddb.wrapper.UserDtoWrapper;
import com.ydool.boot.modules.rddb.service.AuditUserService;
import com.ydool.boot.modules.rddb.wrapper.UserDtoWrapper;
import com.ydool.boot.modules.sys.entity.User;
import com.ydool.boot.modules.sys.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
@ -64,7 +65,7 @@ public class AuditUserController extends ApiBaseController {
//县级工作人员审核
userQueryWrapper.eq("account_type", "admin");
if(StrUtil.isNotBlank(userName)) userQueryWrapper.like("user_name",userName);
if(StrUtil.isNotBlank(loginName)) userQueryWrapper.like("login_name",loginName);
userQueryWrapper.like(StrUtil.isNotBlank(loginName),"AES_DECRYPT(from_base64 (login_name),'"+ CodecUtils.KEY +"')",loginName);
Page page = userService.page(new Page(getPageNum(), getPageSize()), userQueryWrapper);
render(Ret.ok().paged(UserDtoWrapper.build().pageVO(page)));
}

View File

@ -4,11 +4,12 @@ package com.ydool.boot.modules.rddb.web;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ydool.boot.api.util.CodecUtils;
import com.ydool.boot.common.result.Ret;
import com.ydool.boot.core.web.BaseController;
import com.ydool.boot.modules.rddb.entity.ConferenceUser;
import com.ydool.boot.modules.rddb.wrapper.UserDtoWrapper;
import com.ydool.boot.modules.rddb.service.ConferenceUserService;
import com.ydool.boot.modules.rddb.wrapper.UserDtoWrapper;
import com.ydool.boot.modules.sys.entity.User;
import com.ydool.boot.modules.sys.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
@ -64,7 +65,7 @@ public class ConferenceUserController extends BaseController {
//人大代表开会
userQueryWrapper.eq("account_type", "rddb");
if(StrUtil.isNotBlank(userName)) userQueryWrapper.like("user_name",userName);
if(StrUtil.isNotBlank(loginName)) userQueryWrapper.like("login_name",loginName);
userQueryWrapper.like(StrUtil.isNotBlank(loginName),"AES_DECRYPT(from_base64 (login_name),'"+ CodecUtils.KEY +"')",loginName);
Page page = userService.page(new Page(getPageNum(), getPageSize()), userQueryWrapper);
render(Ret.ok().paged(UserDtoWrapper.build().pageVO(page)));
}

View File

@ -9,6 +9,7 @@ import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ydool.boot.api.util.CodecUtils;
import com.ydool.boot.common.excel.ImportDataListener;
import com.ydool.boot.common.result.Ret;
import com.ydool.boot.common.utils.SpringUtils;
@ -80,7 +81,8 @@ public class DbController extends BaseAdminController {
@PostMapping("/list")
@ResponseBody
public void list(DbVO dbVO) {
QueryWrapper<Db> qw = getQueryWrapper(dbVO.getName(), dbVO.getPhone(), dbVO.getSex(), dbVO.getShowScreen());
QueryWrapper<Db> qw = getQueryWrapper(dbVO.getName(), dbVO.getSex(), dbVO.getShowScreen());
qw.like(StrUtil.isNotBlank(dbVO.getPhone()),"AES_DECRYPT(from_base64 (phone),'"+ CodecUtils.KEY +"')",dbVO.getPhone());
IPage<Db> paged = dbService.page(new Page<>(getPageNum(), getPageSize()), qw);
renderJson(Ret.ok().paged(DbWrapper.build().pageVO(paged)));
}
@ -163,7 +165,8 @@ public class DbController extends BaseAdminController {
@PostMapping("/export")
@ResponseBody
public void export(DbVO dbVO, String[] ids) {
QueryWrapper qw = getQueryWrapper(dbVO.getName(), dbVO.getPhone(), dbVO.getSex(), dbVO.getShowScreen());
QueryWrapper qw = getQueryWrapper(dbVO.getName(), dbVO.getSex(), dbVO.getShowScreen());
qw.like(StrUtil.isNotBlank(dbVO.getPhone()),"AES_DECRYPT(from_base64 (phone),'"+ CodecUtils.KEY +"')",dbVO.getPhone());
if (ArrayUtil.isNotEmpty(ids)) {
qw.in("id", Arrays.asList(ids));
}
@ -193,10 +196,9 @@ public class DbController extends BaseAdminController {
render(Ret.ok().data(officeList));
}
private QueryWrapper<Db> getQueryWrapper(String name, String phone, String sex, Integer showScreen) {
private QueryWrapper<Db> getQueryWrapper(String name, String sex, Integer showScreen) {
QueryWrapper<Db> qw = new QueryWrapper<>();
Condition.appendIfNotEmpty(name, "name#like", qw);
Condition.appendIfNotEmpty(phone, "phone#like", qw);
Condition.appendIfNotEmpty(sex, "sex", qw);
Condition.appendIfNotEmpty(showScreen, "show_screen", qw);
qw.orderByDesc("sort_no");

View File

@ -2,8 +2,10 @@ package com.ydool.boot.modules.rddb.web;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ydool.boot.api.util.CodecUtils;
import com.ydool.boot.common.result.Ret;
import com.ydool.boot.core.auth.PreAuth;
import com.ydool.boot.modules.rddb.entity.Office;
@ -49,8 +51,8 @@ public class OfficeController extends BaseAdminController {
public void list(Office office) {
QueryWrapper<Office> wrapper = new QueryWrapper<>();
if (StringUtils.isNotBlank(office.getName())) wrapper.like("name", office.getName());
if (StringUtils.isNotBlank(office.getPhone())) wrapper.like("phone", office.getPhone());
if (StringUtils.isNotBlank(office.getDuty())) wrapper.like("duty", office.getDuty());
wrapper.like(StrUtil.isNotBlank(office.getPhone()),"AES_DECRYPT(from_base64 (phone),'"+ CodecUtils.KEY +"')",office.getPhone());
wrapper.orderByDesc("created_at");
Page page = officeService.page(new Page(getPageNum(), getPageSize()), wrapper);
render(Ret.ok().paged(OfficeWrapper.build().pageVO(page)));

View File

@ -4,10 +4,11 @@ package com.ydool.boot.modules.rddb.web;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ydool.boot.api.util.CodecUtils;
import com.ydool.boot.common.result.Ret;
import com.ydool.boot.modules.rddb.entity.ReadFileUser;
import com.ydool.boot.modules.rddb.wrapper.UserDtoWrapper;
import com.ydool.boot.modules.rddb.service.ReadFileUserService;
import com.ydool.boot.modules.rddb.wrapper.UserDtoWrapper;
import com.ydool.boot.modules.sys.entity.User;
import com.ydool.boot.modules.sys.service.UserService;
import com.ydool.boot.modules.sys.web.BaseAdminController;
@ -64,7 +65,7 @@ public class ReadFileUserController extends BaseAdminController {
//县级工作人员
userQueryWrapper.eq("account_type", "admin");
if(StrUtil.isNotBlank(userName)) userQueryWrapper.like("user_name",userName);
if(StrUtil.isNotBlank(loginName)) userQueryWrapper.like("login_name",loginName);
userQueryWrapper.like(StrUtil.isNotBlank(loginName),"AES_DECRYPT(from_base64 (login_name),'"+ CodecUtils.KEY +"')",loginName);
Page page = userService.page(new Page(getPageNum(), getPageSize()), userQueryWrapper);
render(Ret.ok().paged(UserDtoWrapper.build().pageVO(page)));
}

View File

@ -4,11 +4,12 @@ package com.ydool.boot.modules.rddb.web;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ydool.boot.api.util.CodecUtils;
import com.ydool.boot.common.result.Ret;
import com.ydool.boot.core.web.BaseController;
import com.ydool.boot.modules.rddb.entity.SuperviseThingUser;
import com.ydool.boot.modules.rddb.wrapper.UserDtoWrapper;
import com.ydool.boot.modules.rddb.service.SuperviseThingUserService;
import com.ydool.boot.modules.rddb.wrapper.UserDtoWrapper;
import com.ydool.boot.modules.sys.entity.User;
import com.ydool.boot.modules.sys.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
@ -64,7 +65,7 @@ public class SuperviseThingUserController extends BaseController {
//人大代表督事
userQueryWrapper.eq("account_type", "rddb");
if(StrUtil.isNotBlank(userName)) userQueryWrapper.like("user_name",userName);
if(StrUtil.isNotBlank(loginName)) userQueryWrapper.like("login_name",loginName);
userQueryWrapper.like(StrUtil.isNotBlank(loginName),"AES_DECRYPT(from_base64 (login_name),'"+ CodecUtils.KEY +"')",loginName);
Page page = userService.page(new Page(getPageNum(), getPageSize()), userQueryWrapper);
render(Ret.ok().paged(UserDtoWrapper.build().pageVO(page)));
}

View File

@ -13,6 +13,7 @@ import com.ydool.boot.core.auth.PreAuth;
import com.ydool.boot.core.mybatis.Condition;
import com.ydool.boot.core.mybatis.Query;
import com.ydool.boot.modules.rddb.entity.dto.UserDto;
import com.ydool.boot.modules.rddb.service.TestService;
import com.ydool.boot.modules.rddb.wrapper.UserDtoWrapper;
import com.ydool.boot.modules.sys.entity.Role;
import com.ydool.boot.modules.sys.entity.User;
@ -41,6 +42,8 @@ public class VoterController extends BaseAdminController {
private RoleService roleService;
@Autowired
private UserRoleService userRoleService;
@Autowired
private TestService testService;
@ModelAttribute
public User getUser(String id) {
@ -56,7 +59,9 @@ public class VoterController extends BaseAdminController {
@PreAuth("sys:rddb:list")
public void list(User user, Query query) {
user.setLoginName(CodecUtils.decrypt(user.getLoginName()));
QueryWrapper<User> wrapper = Condition.getQueryWrapper(user, "loginName#like", "accountType");
QueryWrapper<User> wrapper = new QueryWrapper<User>()
.eq(StrUtil.isNotBlank(user.getAccountType()),"account_type", user.getAccountType())
.like(StrUtil.isNotBlank(user.getLoginName()),"AES_DECRYPT(from_base64 (login_name),'"+ CodecUtils.KEY +"')",user.getLoginName());
IPage<User> paged = userService.page(Condition.getPage(query), wrapper.orderByDesc("created_at"));
render(Ret.ok().paged(UserDtoWrapper.build().pageVO(paged)));
}

View File

@ -4,12 +4,13 @@ import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ydool.boot.api.util.CodecUtils;
import com.ydool.boot.common.result.Ret;
import com.ydool.boot.core.web.BaseController;
import com.ydool.boot.modules.rddb.entity.VoterSuggestSolve;
import com.ydool.boot.modules.rddb.wrapper.UserDtoWrapper;
import com.ydool.boot.modules.rddb.service.VoterSuggestService;
import com.ydool.boot.modules.rddb.service.VoterSuggestSolveService;
import com.ydool.boot.modules.rddb.wrapper.UserDtoWrapper;
import com.ydool.boot.modules.rddb.wrapper.VoterSuggestSolveWrapper;
import com.ydool.boot.modules.sys.entity.User;
import com.ydool.boot.modules.sys.service.UserService;
@ -72,7 +73,7 @@ public class VoterSuggestSolveController extends BaseController {
//县级工作人员
// userQueryWrapper.eq("account_type", "admin");
if(StrUtil.isNotBlank(userName)) userQueryWrapper.like("user_name",userName);
if(StrUtil.isNotBlank(loginName)) userQueryWrapper.like("login_name",loginName);
userQueryWrapper.like(StrUtil.isNotBlank(loginName),"AES_DECRYPT(from_base64 (login_name),'"+ CodecUtils.KEY +"')",loginName);
Page page = userService.page(new Page(getPageNum(), getPageSize()), userQueryWrapper);
render(Ret.ok().paged(UserDtoWrapper.build().pageVO(page)));
}

View File

@ -1,16 +1,28 @@
package com.ydool.boot.modules.rddb.web;
import com.ydool.boot.modules.rddb.service.TestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
@RequestMapping(value = "${ydool.path}/welcome")
public class WelcomeController {
@Autowired
private TestService testService;
@GetMapping
public String index() {
return "welcome.html";
}
@GetMapping("encrypt")
@ResponseBody
public void encrypt() {
testService.encrypt();
}
}

View File

@ -1,14 +1,14 @@
#online
db.user = ydool_rd
db.pass = MfaSPabGrDmPtET@123
db.url = jdbc:mysql://122.112.138.17:3306/ydool_rd?useSSL=false&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&serverTimezone=GMT%2B8
db.driver = com.mysql.cj.jdbc.Driver
#db.user = ydool_rd
#db.pass = MfaSPabGrDmPtET@123
#db.url = jdbc:mysql://122.112.138.17:3306/ydool_rd?useSSL=false&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&serverTimezone=GMT%2B8
#db.driver = com.mysql.cj.jdbc.Driver
#local
#db.user = root
#db.pass = 123456
#db.url = jdbc:mysql://127.0.0.1:3306/ydool_rd?useSSL=false&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&serverTimezone=GMT%2B8
#db.driver = com.mysql.cj.jdbc.Driver
db.user = root
db.pass = 123456
db.url = jdbc:mysql://127.0.0.1:3306/ydool_rd?useSSL=false&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&serverTimezone=GMT%2B8
db.driver = com.mysql.cj.jdbc.Driver
spring.servlet.multipart.max-file-size=500MB
spring.servlet.multipart.max-request-size=500MB