`package com.fq.entity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import lombok.Data;
@Data
public class Gw {
@TableId(value = "id", type = IdType.AUTO)
private Integer id;
private String uname;//名字
private Integer uid;
private String file;
private String type;
@TableField("key
")
private String key;
private String date;
private String uname2;//发送名字
private Integer uid2;
private String state;
private String file2;
}
`
`package com.fq.entity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import lombok.Data;
//用户
@Data
public class User {
@TableId(value = "id", type = IdType.AUTO)
private Integer id;
private String name;//名字
private String pwd;//密码
private String perms;//权限
// private double balance;
}
`
`package com.fq.vo;
import lombok.Builder;
import lombok.Data;
@Data
@Builder
public class DataVo
private Integer code;
private String msg; //chu
private Long count;//共几条
private T data;
}`
`package com.fq.controller;
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.fq.entity.Gw;
import com.fq.entity.User;
import com.fq.mapper.GwMapper;
import com.fq.mapper.UserMapper;
import com.fq.utils.SM4Decryptor;
import com.fq.utils.SM4Encryptor;
import com.fq.vo.DataVo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.bouncycastle.crypto.InvalidCipherTextException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.text.SimpleDateFormat;
import java.util.Base64;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Base64;
@Controller
@RequestMapping("user")
public class UserController {
@Autowired
UserMapper userMapper;
@Autowired
GwMapper gwMapper;
@RequestMapping("index")
public String index(){
return "user/index";
}
//个人管理
@RequestMapping("my")
public String user(){
return "user/my";
}
@ResponseBody
@RequestMapping("userList")
public DataVo userList(@RequestParam(value = "page",defaultValue = "1") Integer page, @RequestParam(value = "limit",defaultValue = "10") Integer limit){
QueryWrapper<User> ordersQueryWrapper=new QueryWrapper<>();
Subject subject = SecurityUtils.getSubject();
User currentUser=(User)subject.getPrincipal();//当前登录对象
ordersQueryWrapper.eq("id",currentUser.getId());
IPage<User> page1=new Page<>(page,limit);//每页放几条
IPage<User> labelIPage = userMapper.selectPage(page1, ordersQueryWrapper);//页数
return DataVo.builder().code(0).msg("成功").data(labelIPage.getRecords()).count(labelIPage.getTotal()).build();
}
@RequestMapping("toUserEdit")
public String toUserEdit(){
return "user/service/userEdit";
}
@RequestMapping("userEdit")
@ResponseBody
public Map<String,Object> userEdit(@ModelAttribute("User")User user){
SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd 'at' HH:mm:ss");
Date date = new Date(System.currentTimeMillis());
User user1 = userMapper.selectById(user.getId());
user1.setPwd(user.getPwd());
Map<String,Object> map=new HashMap<>();
userMapper.updateById(user1);
map.put("code",1);
return map;
}
//公文
@RequestMapping("gw")
public String gw(){
return "user/gw";
}
@ResponseBody
@RequestMapping("gwList")
public DataVo gwList(@RequestParam(value = "page",defaultValue = "1") Integer page, @RequestParam(value = "limit",defaultValue = "10") Integer limit){
Subject subject = SecurityUtils.getSubject();
User currentUser=(User)subject.getPrincipal();//当前登录对象
QueryWrapper<Gw> ordersQueryWrapper=new QueryWrapper<>();
ordersQueryWrapper.eq("uid",currentUser.getId());
IPage<Gw> page1=new Page<>(page,limit);//每页放几条
IPage<Gw> labelIPage = gwMapper.selectPage(page1, ordersQueryWrapper);//页数
return DataVo.builder().code(0).msg("成功").data(labelIPage.getRecords()).count(labelIPage.getTotal()).build();
}
// @ResponseBody
// @RequestMapping("gw_search")
// public DataVo gw_search(@RequestParam(value = "page",defaultValue = "1") Integer page, @RequestParam(value = "limit",defaultValue = "10") Integer limit,
// String name){
// IPage
// QueryWrapper
// bookQueryWrapper.like(
// "uname",name
// );
// IPage
// return DataVo.builder().code(0).msg("成功").data(resourceIPage.getRecords()).count(resourceIPage.getTotal()).build();
// }
@RequestMapping("toGwAdd")
public String toGwAdd(){
return "user/service/gwAdd";
}
@RequestMapping("gwAdd")
@ResponseBody
public Map<String,Object> gwAdd(@ModelAttribute("Gw")Gw gw) throws InvalidCipherTextException, IOException {
SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd '-' HH:mm:ss");
Date date = new Date(System.currentTimeMillis());
Subject subject = SecurityUtils.getSubject();
User currentUser=(User)subject.getPrincipal();//当前登录对象
gw.setUid(currentUser.getId());
gw.setUname(currentUser.getName());
QueryWrapper<User> ordersQueryWrapper=new QueryWrapper<>();
ordersQueryWrapper.eq("name",gw.getUname2());
User user = userMapper.selectOne(ordersQueryWrapper);
gw.setDate(formatter.format(date));
gw.setUid2(user.getId());
if (gw.getState().equals("加密")){
Map<String, Object> stringObjectMap = saveFile2(gw.getFile());
gw.setKey((String) stringObjectMap.get("key"));
gw.setFile2((String) stringObjectMap.get("file"));
}
Map<String,Object> map=new HashMap<>();
// lt.setDate(formatter.format(date));
gwMapper.insert(gw);
map.put("code",1);
return map;
}
@RequestMapping("toGwEdit")
public String toGwEdit(){
return "user/service/gwEdit";
}
@RequestMapping("gwEdit")
@ResponseBody
public Map<String,Object> gwEdit(@ModelAttribute("Gw")Gw gw) throws InvalidCipherTextException, IOException {
SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd 'at' HH:mm:ss");
Date date = new Date(System.currentTimeMillis());
Gw gw1 = gwMapper.selectById(gw.getId());
if (!ObjectUtils.isEmpty(gw.getFile())){
gw1.setFile(gw.getFile());
}
gw1.setType(gw.getType());
gw1.setState(gw.getState());
if (gw.getState().equals("加密")){
Map<String, Object> stringObjectMap = saveFile2(gw1.getFile());
gw1.setKey((String) stringObjectMap.get("key"));
gw1.setFile2((String) stringObjectMap.get("file"));
}
if (gw.getState().equals("公开")){
gw1.setKey(null);
gw1.setFile2(null);
}
Map<String,Object> map=new HashMap<>();
gwMapper.updateById(gw1);
map.put("code",1);
return map;
}
@PostMapping("deleteGw")
@ResponseBody
public Map<String,Object> deleteGw(@ModelAttribute("Gw") Gw gw){
// SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd 'at' HH:mm:ss");
// Date date = new Date(System.currentTimeMillis());
// user.setDate(formatter.format(date));
Map<String,Object> map=new HashMap<>();
gwMapper.deleteById(gw.getId());
map.put("code",1);
return map;
}
//收件箱
@RequestMapping("gw2")
public String gw2(){
return "user/gw2";
}
@ResponseBody
@RequestMapping("gwList2")
public DataVo gwList2(@RequestParam(value = "page",defaultValue = "1") Integer page, @RequestParam(value = "limit",defaultValue = "10") Integer limit){
Subject subject = SecurityUtils.getSubject();
User currentUser=(User)subject.getPrincipal();//当前登录对象
QueryWrapper<Gw> ordersQueryWrapper=new QueryWrapper<>();
ordersQueryWrapper.eq("uid2",currentUser.getId());
IPage<Gw> page1=new Page<>(page,limit);//每页放几条
IPage<Gw> labelIPage = gwMapper.selectPage(page1, ordersQueryWrapper);//页数
return DataVo.builder().code(0).msg("成功").data(labelIPage.getRecords()).count(labelIPage.getTotal()).build();
}
@ResponseBody
@RequestMapping("gw_search2")
public DataVo gw_search(@RequestParam(value = "page",defaultValue = "1") Integer page, @RequestParam(value = "limit",defaultValue = "10") Integer limit,
String name){
Subject subject = SecurityUtils.getSubject();
User currentUser=(User)subject.getPrincipal();//当前登录对象
IPage<Gw> page1=new Page<>(page,limit);//每页放几条
QueryWrapper<Gw> bookQueryWrapper = new QueryWrapper<>();
bookQueryWrapper.eq("uid2",currentUser.getId());
bookQueryWrapper.like(
"uname",name
);
IPage<Gw> resourceIPage = gwMapper.selectPage(page1, bookQueryWrapper);//页数
return DataVo.builder().code(0).msg("成功").data(resourceIPage.getRecords()).count(resourceIPage.getTotal()).build();
}
@Value("${pro.uploadPath}")
private String uploadPath;
public final static String UPLOAD_PATH_PREFIX = "gw/src/main/resources/static";
//工具
@RequestMapping("upload")
@ResponseBody
public Map<String,Object> upload(@RequestParam("file") MultipartFile file){
Map<String,Object> map=new HashMap<>();
map.put("file",(String)saveFile(file));
map.put("code",1);
return map;
}
private Object saveFile(MultipartFile file){
if (file.isEmpty()){
return "未选择文件";
}
String filename = file.getOriginalFilename(); //获取上传文件原来的名称
//文件名
String name = filename.substring(0, filename.indexOf("."));
//文件后缀
String suffix = filename.substring(filename.lastIndexOf("."));
// File filePath = new File(UPLOAD_PATH_PREFIX + uploadPath + File.separator + "temps");
// File filePath = new File(UPLOAD_PATH_PREFIX + File.separator + uploadPath + File.separator + "temps");
Path uploadPath = Paths.get(UPLOAD_PATH_PREFIX, this.uploadPath, "temps");
File filePath = uploadPath.toFile();
System.out.println("多文件上传的位置: " + filePath);
System.out.println("多文件上传的绝对路径: " + filePath.getAbsolutePath());
System.out.println("文件名: " + file.getOriginalFilename());
// String filePath = "C://Users/付奇/Desktop/工作/school/school1/src/main/resources/static/images/";
// if (!filePath.isDirectory()) {
// filePath.mkdirs();
// }
if (!filePath.exists()) {
filePath.mkdirs();
}
File dest = new File(filePath.getAbsolutePath() + File.separator + file.getOriginalFilename());
int i = 1;
//若文件存在重命名
String newFilename = filename;
while (dest.exists()) {
newFilename = name + "(" + i + ")" + suffix;
filename=newFilename;
String parentPath = dest.getParent();
dest = new File(parentPath + File.separator + newFilename);
i++;
}
try {
file.transferTo(dest);
} catch (IOException e) {
e.printStackTrace();
return file.getOriginalFilename() + "上传失败";
}
// File localFile = new File(filePath+filename);
// try {
// file.transferTo(localFile); //把上传的文件保存至本地
// System.out.println(file.getOriginalFilename()+" 上传成功");
// }catch (IOException e){
// e.printStackTrace();
// return "filename";
// }
return filename;
}
private Map<String,Object> saveFile2(String file) throws InvalidCipherTextException, IOException {
Map<String,Object> map=new HashMap<>();
if (file.isEmpty()){
return map;
}
String filename = file; //获取上传文件原来的名称
// //文件名
String name = filename.substring(0, filename.indexOf("."));
//文件后缀
// String suffix = filename.substring(filename.lastIndexOf("."));
String suffix = ".enc";
// File filePath = new File(UPLOAD_PATH_PREFIX + uploadPath + File.separator + "temps");
// File filePath = new File(UPLOAD_PATH_PREFIX + File.separator + uploadPath + File.separator + "temps");
Path uploadPath = Paths.get(UPLOAD_PATH_PREFIX, this.uploadPath, "temps");
File filePath = uploadPath.toFile();
System.out.println("多文件上传的位置: " + filePath);
System.out.println("多文件上传的绝对路径: " + filePath.getAbsolutePath());
// System.out.println("文件名: " + file.getOriginalFilename());
// String filePath = "C://Users/付奇/Desktop/工作/school/school1/src/main/resources/static/images/";
// if (!filePath.isDirectory()) {
// filePath.mkdirs();
// }
if (!filePath.exists()) {
filePath.mkdirs();
}
File dest = new File(filePath.getAbsolutePath() + File.separator + file);
String inputFilePath =dest.getAbsolutePath();
System.out.println("cc:"+inputFilePath);
int i = 1;
//若文件存在重命名
String newFilename = filename;
while (dest.exists()) {
newFilename = name + "(" + i + ")" + suffix;
filename=newFilename;
String parentPath = dest.getParent();
dest = new File(parentPath + File.separator + newFilename);
i++;
}
byte[] key = SM4Encryptor.generateRandomKey();
String encryptedFilePath = dest.getAbsolutePath();
System.out.println("cc2:"+encryptedFilePath);
// 加密文件
// 创建加密器实例
SM4Encryptor encryptor = new SM4Encryptor(key);
encryptor.encryptFile(inputFilePath, encryptedFilePath);
System.out.println("加密成功");
map.put("file",encryptedFilePath);
map.put("key",Base64.getEncoder().encodeToString(key));
// File localFile = new File(filePath+filename);
// try {
// file.transferTo(localFile); //把上传的文件保存至本地
// System.out.println(file.getOriginalFilename()+" 上传成功");
// }catch (IOException e){
// e.printStackTrace();
// return "filename";
// }
return map;
}
@GetMapping("/download")
public ResponseEntity<?> downloadFile(String base64Key,String file,String file2) {
String filename = file; //获取上传文件原来的名称
// //文件名
String name = filename.substring(0, filename.indexOf("."));
//文件后缀
String suffix = filename.substring(filename.lastIndexOf("."));
Path uploadPath = Paths.get(UPLOAD_PATH_PREFIX, this.uploadPath, "temps");
File filePath = uploadPath.toFile();
if (!filePath.exists()) {
filePath.mkdirs();
}
File dest = new File(filePath.getAbsolutePath() + File.separator + file);
File dest2 = new File(filePath.getAbsolutePath() + File.separator + file2);
String inputFilePath =dest.getAbsolutePath();
System.out.println("cc:"+inputFilePath);
int i = 1;
//若文件存在重命名
String newFilename = filename;
while (dest.exists()) {
newFilename = name + "(" + i + ")" + suffix;
filename=newFilename;
String parentPath = dest.getParent();
dest = new File(parentPath + File.separator + newFilename);
i++;
}
try {
// 将Base64编码的密钥转换为字节
byte[] key = Base64.getDecoder().decode(base64Key);
// 加密文件路径和临时解密文件路径
String encryptedFilePath = file2;
System.out.println("yy:"+encryptedFilePath);
String decryptedFilePath = dest.getAbsolutePath();
System.out.println("yy2:"+decryptedFilePath);
// 解密文件
SM4Decryptor decryptor = new SM4Decryptor(key);
decryptor.decryptFile(encryptedFilePath, decryptedFilePath);
// 读取解密后的文件并返回
Path path = Paths.get(decryptedFilePath);
byte[] data = Files.readAllBytes(path);
// 清理临时文件
Files.delete(path);
return ResponseEntity
.ok()
.contentLength(data.length)
.header("Content-Type", "application/octet-stream")
.header("Content-Disposition", "attachment; filename=\"" + path.getFileName() + "\"")
.body(data);
} catch (Exception e) {
// 密钥不正确或解密失败
return ResponseEntity
.badRequest()
.body("密钥不正确");
}
}
}
`