实现根据角色到出数据
This commit is contained in:
parent
bb53fbf10f
commit
92b5fe9365
|
@ -270,6 +270,11 @@
|
|||
<groupId>cn.hutool</groupId>
|
||||
<artifactId>hutool-crypto</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.github.ulisesbocchio</groupId>
|
||||
<artifactId>jasypt-spring-boot-starter</artifactId>
|
||||
<version>3.0.3</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
|
@ -8,7 +8,9 @@ import lombok.extern.slf4j.Slf4j;
|
|||
import org.apache.commons.beanutils.PropertyUtils;
|
||||
import org.apache.shiro.SecurityUtils;
|
||||
import org.jeecg.common.api.vo.Result;
|
||||
import org.jeecg.common.system.base.service.SysRoleService;
|
||||
import org.jeecg.common.system.query.QueryGenerator;
|
||||
import org.jeecg.common.system.util.JasyptUtil;
|
||||
import org.jeecg.common.system.vo.LoginUser;
|
||||
import org.jeecg.common.util.oConvertUtils;
|
||||
import org.jeecg.config.JeecgBaseConfig;
|
||||
|
@ -27,6 +29,7 @@ import javax.annotation.Resource;
|
|||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
|
@ -42,26 +45,63 @@ public class JeecgController<T, S extends IService<T>> {
|
|||
protected S service;
|
||||
@Resource
|
||||
private JeecgBaseConfig jeecgBaseConfig;
|
||||
@Autowired
|
||||
private SysRoleService sysRoleService;
|
||||
|
||||
/**
|
||||
* 导出excel
|
||||
*
|
||||
* @param request
|
||||
*/
|
||||
protected ModelAndView exportXls(HttpServletRequest request, T object, Class<T> clazz, String title) {
|
||||
protected ModelAndView exportXls(HttpServletRequest request, T object, Class<T> clazz, String title){
|
||||
// Step.1 组装查询条件
|
||||
QueryWrapper<T> queryWrapper = QueryGenerator.initQueryWrapper(object, request.getParameterMap());
|
||||
LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
|
||||
|
||||
String roleCode = sysRoleService.getRoleCodesByUserId(sysUser.getId());
|
||||
// 过滤选中数据
|
||||
String selections = request.getParameter("selections");
|
||||
if(roleCode.length() == 1) {
|
||||
queryWrapper.eq("major_id", roleCode);
|
||||
}
|
||||
if (oConvertUtils.isNotEmpty(selections)) {
|
||||
List<String> selectionList = Arrays.asList(selections.split(","));
|
||||
queryWrapper.in("id",selectionList);
|
||||
}
|
||||
// Step.2 获取导出数据
|
||||
List<T> exportList = service.list(queryWrapper);
|
||||
|
||||
exportList.forEach(item -> {
|
||||
try {
|
||||
// 检查并解密银行卡字段
|
||||
Field pyCardField = null;
|
||||
try {
|
||||
pyCardField = item.getClass().getDeclaredField("pyCard");
|
||||
} catch (NoSuchFieldException e) {
|
||||
// 字段不存在,跳过解密操作
|
||||
}
|
||||
if (pyCardField != null) {
|
||||
pyCardField.setAccessible(true); // 确保可以访问私有字段
|
||||
String encryptedValuePycard = (String) pyCardField.get(item);
|
||||
String decryptedValuePycard = JasyptUtil.decrypt(encryptedValuePycard, "bigdata"); // 替换为你的密码
|
||||
pyCardField.set(item,decryptedValuePycard); // 将解密后的值存储到 decryptedField
|
||||
}
|
||||
// 检查并解密身份证字段
|
||||
Field identityIdField = null;
|
||||
try {
|
||||
identityIdField = item.getClass().getDeclaredField("identityId");
|
||||
} catch (NoSuchFieldException e) {
|
||||
// 字段不存在,跳过解密操作
|
||||
}
|
||||
if (identityIdField != null) {
|
||||
//解密身份证
|
||||
identityIdField.setAccessible(true); // 确保可以访问私有字段
|
||||
String encryptedIdentityId = (String) identityIdField.get(item);
|
||||
String decryptedIdentityId = JasyptUtil.decrypt(encryptedIdentityId, "bigdata"); // 替换为你的密码
|
||||
identityIdField.set(item,decryptedIdentityId); // 将解密后的值存储到 decryptedField
|
||||
}
|
||||
} catch (IllegalAccessException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
});
|
||||
// Step.3 AutoPoi 导出Excel
|
||||
ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
|
||||
//此处设置的filename无效 ,前端会重更新设置一下
|
||||
|
|
|
@ -0,0 +1,8 @@
|
|||
package org.jeecg.common.system.base.service;
|
||||
|
||||
public interface SysRoleService {
|
||||
/**
|
||||
* 根据用户ID获取角色编码集合
|
||||
*/
|
||||
String getRoleCodesByUserId(String userId);
|
||||
}
|
|
@ -0,0 +1,101 @@
|
|||
package org.jeecg.common.system.util;
|
||||
|
||||
|
||||
import org.jasypt.encryption.pbe.StandardPBEStringEncryptor;
|
||||
import org.jasypt.encryption.pbe.config.EnvironmentPBEConfig;
|
||||
|
||||
public class JasyptUtil {
|
||||
|
||||
/**
|
||||
* PBE 算法
|
||||
*/
|
||||
public static final String PBE_ALGORITHMS_MD5_DES = "PBEWITHMD5ANDDES";
|
||||
public static final String PBE_ALGORITHMS_MD5_TRIPLEDES = "PBEWITHMD5ANDTRIPLEDES";
|
||||
public static final String PBE_ALGORITHMS_SHA1_DESEDE = "PBEWITHSHA1ANDDESEDE";
|
||||
public static final String PBE_ALGORITHMS_SHA1_RC2_40 = "PBEWITHSHA1ANDRC2_40";
|
||||
|
||||
private JasyptUtil() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Jasypt 加密
|
||||
*
|
||||
* @param encryptedStr 加密字符串
|
||||
* @param password 盐值
|
||||
* @return
|
||||
*/
|
||||
public static String encrypt(String encryptedStr, String password) {
|
||||
return encrypt(encryptedStr, PBE_ALGORITHMS_MD5_DES, password);
|
||||
}
|
||||
|
||||
/**
|
||||
* Jasypt 加密
|
||||
*
|
||||
* @param encryptedStr 加密字符串
|
||||
* @param algorithm 加密算法
|
||||
* PBE ALGORITHMS: [PBEWITHMD5ANDDES, PBEWITHMD5ANDTRIPLEDES, PBEWITHSHA1ANDDESEDE, PBEWITHSHA1ANDRC2_40]
|
||||
* @param password 盐值
|
||||
* @return
|
||||
*/
|
||||
public static String encrypt(String encryptedStr, String algorithm, String password) {
|
||||
// StandardPBEStringEncryptor、StandardPBEBigDecimalEncryptor、StandardPBEBigIntegerEncryptor、StandardPBEByteEncryptor
|
||||
StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor();
|
||||
EnvironmentPBEConfig config = new EnvironmentPBEConfig();
|
||||
|
||||
// 指定加密算法
|
||||
config.setAlgorithm(algorithm);
|
||||
// 加密盐值
|
||||
config.setPassword(password);
|
||||
//config.setIvGeneratorClassName("org.jasypt.iv.NoIvGenerator");
|
||||
encryptor.setConfig(config);
|
||||
|
||||
// 加密
|
||||
return encryptor.encrypt(encryptedStr);
|
||||
}
|
||||
|
||||
/**
|
||||
* Jasypt 解密
|
||||
*
|
||||
* @param decryptStr 解密字符串
|
||||
* @param password 盐值
|
||||
* @return
|
||||
*/
|
||||
public static String decrypt(String decryptStr, String password) {
|
||||
return decrypt(decryptStr, PBE_ALGORITHMS_MD5_DES, password);
|
||||
}
|
||||
|
||||
/**
|
||||
* Jasypt 解密
|
||||
*
|
||||
* @param decryptStr 解密字符串
|
||||
* @param algorithm 指定解密算法:解密算法要与加密算法一一对应
|
||||
* PBE ALGORITHMS: [PBEWITHMD5ANDDES, PBEWITHMD5ANDTRIPLEDES, PBEWITHSHA1ANDDESEDE, PBEWITHSHA1ANDRC2_40]
|
||||
* @param password 盐值
|
||||
* @return
|
||||
*/
|
||||
public static String decrypt(String decryptStr, String algorithm, String password) {
|
||||
// StandardPBEStringEncryptor、StandardPBEBigDecimalEncryptor、StandardPBEBigIntegerEncryptor、StandardPBEByteEncryptor
|
||||
StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor();
|
||||
EnvironmentPBEConfig config = new EnvironmentPBEConfig();
|
||||
|
||||
// 指定解密算法:解密算法要与加密算法一一对应
|
||||
config.setAlgorithm(algorithm);
|
||||
// 加密秘钥
|
||||
config.setPassword(password);
|
||||
//config.setIvGeneratorClassName("org.jasypt.iv.NoIvGenerator");
|
||||
encryptor.setConfig(config);
|
||||
|
||||
// 解密
|
||||
return encryptor.decrypt(decryptStr);
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
String encryptedStr = "I am the string to be encrypted";
|
||||
String algorithm = PBE_ALGORITHMS_SHA1_RC2_40;
|
||||
String password = "salt";
|
||||
|
||||
String str = JasyptUtil.encrypt(encryptedStr, algorithm, password);
|
||||
System.out.println("加密后的字符串:" + str);
|
||||
System.out.println("解密后的字符串:" + JasyptUtil.decrypt(str, algorithm, password));
|
||||
}
|
||||
}
|
|
@ -0,0 +1,35 @@
|
|||
package org.jeecg.modules.system.service.impl;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.jeecg.common.system.base.service.SysRoleService;
|
||||
import org.jeecg.modules.system.entity.SysRole;
|
||||
import org.jeecg.modules.system.entity.SysUserRole;
|
||||
import org.jeecg.modules.system.mapper.SysRoleMapper;
|
||||
import org.jeecg.modules.system.mapper.SysUserRoleMapper;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Slf4j
|
||||
@Service
|
||||
public class CoreSysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements SysRoleService {
|
||||
@Autowired
|
||||
private SysUserRoleMapper sysUserRoleMapper;
|
||||
@Autowired
|
||||
private SysRoleMapper sysRoleMapper;
|
||||
@Override
|
||||
public String getRoleCodesByUserId(String userId) {
|
||||
|
||||
LambdaQueryWrapper<SysUserRole> queryUserRole = new LambdaQueryWrapper<>();
|
||||
queryUserRole.eq(SysUserRole::getUserId, userId);
|
||||
SysUserRole sysUserRole = sysUserRoleMapper.selectOne(queryUserRole);
|
||||
String roleId = sysUserRole.getRoleId();
|
||||
//获取对应角色数据
|
||||
LambdaQueryWrapper<SysRole> queryRole = new LambdaQueryWrapper<>();
|
||||
queryRole.eq(SysRole::getId, roleId);
|
||||
SysRole sysRole = sysRoleMapper.selectOne(queryRole);
|
||||
String roleCode = sysRole.getRoleCode();
|
||||
return roleCode;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue