如何在springboot / mySql和dropzonejs中使用openssl

时间:2017-11-18 10:02:00

标签: spring-boot encryption openssl dropzone.js

对不起,请原谅我。与此同时,我开发了一个电子文档管理,在后端使用springboot,在前面使用angular2。到目前为止,我的数据库中的下载和存储通过了很好的反对部分加密和解密与我的springboot中的openssl实现程序不起作用。

类FileUpload

  @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);     
}

类FileController:

@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);
}
}

类FileLobService

 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);
}

类FileUploadRepository

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]

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

@echo off
if "%1" == "" goto syntax
md C:\%1
xcopy/E A:\%1 C:\%1 
goto end
:syntax
echo Please input a directory after %0
:end

你有没有想过在SpringBoot中使用Openssl来加载带有dropzonejs的下载文件,然后再存储到我的Mysql数据库中?

1 个答案:

答案 0 :(得分:0)

我发现它只是需要的问题将文件的内容作为字节数组返回。加密类

改变时类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);     
}

类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消息摘要算法或更多