为了完成我的最后一年的项目,我编写了一个Java / spring程序,该程序使用java.security.KeyStore和X509Certificate API ....读取在给定目录(公共和私有证书)中找到的所有证书文件。每个证书文件都创建了一个Java证书对象,并将其插入数据库中。现在他们告诉我,我应该使用Spring批处理来执行编写的代码,因此在阅读它之后,我发现最好的方法是使用 FlatFileItemReaderBuilder ,但是问题是我没有这样做。不了解如何将其与上面列出的特定阅读证书API集成。
这是书面代码:
private final CertificatRepository CertificatRepository;
public CertificatService(CertificatRepository certificatRepository) {
super();
this.CertificatRepository = certificatRepository;
}
public void save(Certificat certificat) {
CertificatRepository.save(certificat);
}
public void saveAll(String chemin) {
List <Certificat> certificats = parseCertificateDirectory(chemin);
for (Certificat certificat : certificats) {
save(certificat);
}
}
public List<Certificat> findAll() {
return this.CertificatRepository.findAll();
}
public List<Certificat> listCertificates(String chemin) {
return parseCertificateDirectory(chemin);
}
public Certificat findOne (String id) {
return this.CertificatRepository.findOne(id);
}
private Certificat retrieveCertificate(String chemin, String name) {
FileInputStream fils;
Certificat certificat = null;
try {
fils = new FileInputStream(chemin + name);
String extensionCertificat = name.substring(name.indexOf(".") + 1, name.length());
if ("cer".equals(extensionCertificat)) {
certificat = createCertificate(CertificateType.PUBLIC, (X509Certificate) getPublicCertificate(fils));
}
else {
if ("p12".equals(extensionCertificat)) {
certificat = createCertificate(CertificateType.PRIVATE, (X509Certificate) getPrivateCertificate(fils));
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return certificat;
}
//Creer un objet de type Certificat
private Certificat createCertificate(CertificateType typeCle, Certificate cert) {
Certificat certificat;
X509Certificate x509Certificate = (X509Certificate) cert;
certificat = new Certificat();
certificat.setDn(x509Certificate.getIssuerDN().toString());
certificat.setCn(x509Certificate.getSubjectX500Principal().toString());
certificat.setSatrtdate(x509Certificate.getNotBefore());
certificat.setEnddate(x509Certificate.getNotAfter());
certificat.setCle_public("");
certificat.setCle_prive("");
certificat.setPublicPrive(typeCle.toString());
certificat.setType("V"+x509Certificate.getVersion());
return certificat;
}
//Retourne un certificat publique
private Certificate getPublicCertificate(FileInputStream fils) {
CertificateFactory cf;
Certificate certificate = null;
try {
cf = CertificateFactory.getInstance("X.509");
certificate = cf.generateCertificate(fils);
} catch (CertificateException e) {
e.printStackTrace();
}
return certificate;
}
// Retourne un certificat prive
private X509Certificate getPrivateCertificate(FileInputStream fils) {
KeyStore p12;
X509Certificate x509Certificate = null;
try {
p12 = KeyStore.getInstance("pkcs12");
p12.load(fils, "password".toCharArray());
x509Certificate = (X509Certificate) p12.getCertificate("1");
}
catch (KeyStoreException e1) {
e1.printStackTrace();
}
catch (NoSuchAlgorithmException e1) {
e1.printStackTrace();
}
catch (CertificateException e1) {
e1.printStackTrace();
}
catch (IOException e1) {
e1.printStackTrace();
}
return x509Certificate;
}
// parcours le chemin qui contient tous les certificats
public List <Certificat> parseCertificateDirectory (String chemin) {
System.out.println("chemin : " + chemin);
List <Certificat> certifs = new ArrayList<>();
File folder = new File(chemin);
File[] listOfFiles = folder.listFiles();
for (File file : listOfFiles) {
if (file.isFile()) {
System.out.println(file.getName());
certifs.add(retrieveCertificate(chemin, file.getName()));
}
}
return certifs;
}
}
任何帮助将不胜感激。
答案 0 :(得分:3)
在您的情况下,项目是文件(类型为File
),而不是平面文件中的行。因此FlatFileItemReader
不适合您的要求。
对于读者而言,您可以使用IteratorItemReader
并在构造时传递文件列表(parseCertificateDirectory
方法的结果)。
对于编写者,可以使用ItemWriterAdapter
来修改CertificatRepository
并将其用作编写者(将save
方法设置为targetMethod
)。
现在有了读者和作家,您可以在这里查看示例之一:https://github.com/spring-projects/spring-batch/tree/master/spring-batch-samples,了解如何使用面向块的步骤配置Spring Batch作业。