如何将 openssl 与 springboot/mySql 和 dropzonejs 一起使用

How to Use openssl with springboot/mySql and dropzonejs

请原谅我的英语。同时开发了一个后台springboot,前台angular2的电子文档管理。到目前为止,我的数据库中的下载和 stokage 通过在我的 springboot 中使用 openssl 实现程序进行的部分加密和解密无法正常工作。

Class 文件上传

@Table(name = "table_doc")
@SuppressWarnings("serial")
@Entity
@Document(indexName = "tabledoc", type = "tabledoc")
public class FileUpload implements Serializable {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String filename;

@Lob
private byte[] file;

private String mimeType;

private Long size;

public FileUpload(String filename, byte[] file, String mimeType, Long size) {
    super();
    this.filename = filename;
    this.file = file;
    this.mimeType = mimeType;
    this.size = size;
}
public FileUpload() {
    super();
}
public Long getId() {
    return id;
}
public void setId(Long id) {
    this.id = id;
}
public String getFilename() {
    return filename;
}
public void setFilename(String filename) {
    this.filename = filename;
}
public byte[] getFile() {
    return file;
}
public void setFile(byte[] file) {
    this.file = file;
}
public String getMimeType() {
    return mimeType;
}
public void setMimeType(String mimeType) {
    this.mimeType = mimeType;
}
public Long getSize() {
    return size;
}
public void setSize(Long size) {
    this.size = size;
}
}

Class 文件控制器:

  @RequestMapping(value ="/uploadLobCrypt",method = RequestMethod.POST) 
public ResponseEntity <String> fileCrypt(MultipartHttpServletRequest request,MultipartFile multiPartFile) throws IOException, InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException, InvalidKeySpecException  {
    Iterator<String> itr = request.getFileNames();
    String uploadedFile = itr.next();
    MultipartFile file = request.getFile(uploadedFile); 
    String mimeType = file.getContentType();
    String filename = file.getOriginalFilename();
    byte[] bytes = file.getBytes();
    Long size = file.getSize();
    FileUpload fileCrypte = new FileUpload(filename, bytes, mimeType,size);
    fileLobService.fileCrypt(multiPartFile,fileCrypte);
    return new ResponseEntity<String>("{}", HttpStatus.OK);     
}

Class FileLobService

@Service("fileLobService")
@Transactional
public class FileLobService {
@Autowired
FileUploadRepository fileUploadRepository;

// Retrouver un fichier
public FileUpload findByFilename(String filename) {
    return fileUploadRepository.findByFilename(filename);
}

public FileUpload findById(Long id) {
    return fileUploadRepository.findById(id);
}

public Long deleteFileById(Long id) {
    return fileUploadRepository.deleteFileById(id);
}

public String deleteFileByFilename(String filename) {
    return fileUploadRepository.deleteFileByFilename(filename);
}

public void File(File file) {
    fileUploadRepository.File(file);
}

// Upload the file
public void uploadFile(FileUpload fileName) {

    fileUploadRepository.saveAndFlush(fileName);
}

public void fileCrypt(MultipartFile multiPartFile, FileUpload fileCrypte)
        throws IOException, InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException,
        IllegalBlockSizeException, BadPaddingException, InvalidKeySpecException {

    // ecriture clee public et public dans un path
    String publicKeyPath = "C:\OpenSSL-Win64\bin\public.der";
    String privateKeyPath = "C:\OpenSSL-Win64\bin\private.pk8";

    fileCrypte.setFile(multiPartFile.getBytes());
    fileCrypte.setFilename(multiPartFile.getOriginalFilename());
    fileCrypte.setMimeType(multiPartFile.getContentType());
    fileCrypte.setSize(multiPartFile.getSize());
    File file = new File(multiPartFile.getOriginalFilename());
    multiPartFile.transferTo(file);
    byte[] dataBytes = FileUtils.readFileToByteArray(file);
    Cryptage cryptage = new Cryptage();
    byte[] encryptedBytes = cryptage.encryptFile(dataBytes, publicKeyPath);
    FileUtils.writeByteArrayToFile(file, encryptedBytes);

    fileUploadRepository.saveCryptedFile(file);
}
}

Class FileUploadRepository

 public interface FileUploadRepository extends JpaRepository<FileUpload, Long> {
FileUpload findByFilename(String filename);

FileUpload findById(Long id);

Long deleteFileById(Long id);

String deleteFileByFilename(String filename);

void File(File file);

void saveCryptedFile(java.io.File file);
}

现在,当我启动服务器时,我得到以下堆栈跟踪:

Caused by: org.springframework.data.mapping.PropertyReferenceException: No 
property saveCryptedFile found for type FileUpload!
at org.springframework.data.mapping.PropertyPath.<init>
(PropertyPath.java:77) ~[spring-data-commons-1.13.7.RELEASE.jar:na]
at 
org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:329) 
~[spring-data-commons-1.13.7.RELEASE.jar:na]
at 
org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:309) 
~[spring-data-commons-1.13.7.RELEASE.jar:na]
at org.springframework.data.mapping.PropertyPath.from(PropertyPath.java:272) 
~[spring-data-commons-1.13.7.RELEASE.jar:na]
at org.springframework.data.mapping.PropertyPath.from(PropertyPath.java:243) 
~[spring-data-commons-1.13.7.RELEASE.jar:na]
at org.springframework.data.repository.query.parser.Part.<init>
(Part.java:76) ~[spring-data-commons-1.13.7.RELEASE.jar:na]

您是否知道在将下载文件存储到我的 Mysql 数据库之前,使用 SpringBoot 中的 Openssl 使用 dropzonejs 加密下载文件?

我发现问题是刚好需要Return文件的内容作为一个字节数组。使用加密 class

Class FileController 更改时:

@RequestMapping(value ="/uploadLobCrypt",method = RequestMethod.POST)

public ResponseEntity <String> fileCrypt(MultipartFile multiPartFile,MultipartHttpServletRequest request) throws IOException, InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException, InvalidKeySpecException  {

    fileLobService.fileCrypt(multiPartFile, request);

    return new ResponseEntity<String>("{}", HttpStatus.OK);     
}

Class FileLobService

public void fileCrypt(MultipartFile 
multiPartFile,MultipartHttpServletRequest 
request) throws IOException, InvalidKeyException, NoSuchAlgorithmException, 
NoSuchPaddingException,IllegalBlockSizeException, 
BadPaddingException,InvalidKeySpecException {

    // ecriture clee public et public dans un path
    String publicKeyPath = "C:\OpenSSL-Win64\bin\public.der";
    //String privateKeyPath = "C:\OpenSSL-Win64\bin\private.pk8";

    Iterator<String> itr = request.getFileNames();

    String uploadedFile = itr.next();
    MultipartFile file = request.getFile(uploadedFile); 

    String mimeType = file.getContentType();
    String filename = file.getOriginalFilename();
    byte[] bytes = file.getBytes();
    Long size = file.getSize();

    Cryptage cryptage = new Cryptage();
    byte[] encryptedBytes = cryptage.encryptFile(bytes, publicKeyPath);

    FileUpload fileUploaded = new FileUpload(filename, encryptedBytes, mimeType,size);

    fileUploadRepository.saveAndFlush(fileUploaded);

}

注意:它只对小文件有用,对于大文件我们必须使用 SHA-256 消息摘要算法或更多