2017-11-18 88 views
0

請原諒我的英文版本。與此同時,我開發了一個電子文檔管理系統,在後端使用springboot,前端使用angular2。直到現在我的數據庫中的下載和stokage通過非常好的反對部分加密和解密與openssl實施者在我的springboot不起作用。如何在springboot/mySql和dropzonejs中使用openssl

類文件上傳

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

類FileController:

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

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

類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] 

你有關於SpringBoot使用OpenSSL與加密下載文件的任何想法dropzonejs存儲在我的Mysql數據庫之前?

回答

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