在Spring Batch中进行解密后,通过MultiResourceItemReader读取文件

时间:2016-03-23 06:22:03

标签: java spring encryption spring-batch

我正在开发一个弹簧批处理来读取文件夹中的多个文件,处理它并将结果写入DB。我正在使用MultiResourceItemReader从文件夹中读取文件。但文件可能是加密的,所以我需要在阅读之前解密文件。做这个的最好方式是什么?

1)我应该在阅读,处理和写作之前使用一个步骤吗? 2)我应该从FlatFileItemReader覆盖doOpen,它被设置为MultiResourceItemReader的委托吗? 3)我应该在MultiResourceItemReader本身中覆盖open吗?

我认为,不建议覆盖现有方法,因为我们最终可能会重复或丢失代码。请指教

1 个答案:

答案 0 :(得分:0)

我猜你在这里使用FlatFileItemReader作为代表?

然后您可以使用自定义的BufferedReaderFactory

进行制作

Java代码(未经过测试!我没有使用Cipher的经验)

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.UnsupportedEncodingException;

import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.IvParameterSpec;

import org.springframework.batch.item.file.BufferedReaderFactory;
import org.springframework.core.io.Resource;

public class EncryptedBufferedItemReaderFactory implements BufferedReaderFactory {

  private String keyFile;
  private String algorithm;

  @Override
  public BufferedReader create(final Resource resource, final String encoding) throws UnsupportedEncodingException, IOException {
    FileInputStream fis = new FileInputStream(resource.getFile());
    CipherInputStream cis = new CipherInputStream(fis, createCipher());
    return new BufferedReader(new InputStreamReader(cis, encoding));
  }

  private Cipher createCipher() throws IOException, FileNotFoundException {
    ObjectInputStream ois = new ObjectInputStream(new FileInputStream(keyFile));
    Cipher cipher;
    try {
      DESKeySpec ks = new DESKeySpec((byte[]) ois.readObject());
      SecretKeyFactory skf = SecretKeyFactory.getInstance("DES");
      SecretKey key = skf.generateSecret(ks);
      cipher = Cipher.getInstance(algorithm);
      cipher.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec((byte[]) ois.readObject()));
    } catch (Exception ex) {
      throw new IOException(ex);
    } finally {
      ois.close();
    }
    return cipher;
  }

  public void setKeyFile(final String keyFile) {
    this.keyFile = keyFile;
  }

  public void setAlgorithm(final String algorithm) {
    this.algorithm = algorithm;
  }
}
算法

请参阅Java standard algorithms

示例bean配置

<bean id="multiResourceItemReader" class="org.springframework.batch.item.file.MultiResourceItemReader" scope="step">
  <property name="resources" value="#{jobParameters['whateverYouChosen']}" />
  <property name="delegate" ref="flatFileItemReader" />
</bean>

<bean id="flatFileItemReader" class="org.springframework.batch.item.file.FlatFileItemReader" scope="step">
  <!-- some properties you already have, like lineMapper -->
  <property name="bufferedReaderFactory" ref ="encryptedBufferedReaderFactory" />
</bean>

<bean id="encryptedBufferedReaderFactory" class="your.com.package.EncryptedBufferedReaderFactory" scope="step">
  <property name="keyFile" value="#{jobParameters['pathToKeyFile']}" />
  <property name="algorithm" value="#{jobParameters['algorithm']}" />
</bean>