-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ajustes Metodo de extracao CNPJ e CPF
- Loading branch information
1 parent
2dfa5b9
commit 92ead68
Showing
2 changed files
with
60 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -48,8 +48,9 @@ public static void inicializaCertificado(Certificado certificado, InputStream ca | |
Protocol.registerProtocol("https", protocol); | ||
|
||
log.info(String.format("JAVA-CERTIFICADO | Samuel Oliveira | [email protected] " + | ||
"| VERSAO=%s | CNPJ/CPF=%s | VENCIMENTO=%s | ALIAS=%s | TIPO=%s | CAMINHO=%s | CACERT=%s | SSL=%s", | ||
"3.1", | ||
"| VERSAO=%s | DATA_VERSAO=%s | CNPJ/CPF=%s | VENCIMENTO=%s | ALIAS=%s | TIPO=%s | CAMINHO=%s | CACERT=%s | SSL=%s", | ||
"3.2", | ||
"04/03/2024", | ||
certificado.getCnpjCpf(), | ||
certificado.getDataHoraVencimento(), | ||
certificado.getNome().toUpperCase(), | ||
|
@@ -90,7 +91,7 @@ private static void setDadosCertificado(Certificado certificado, KeyStore keySto | |
} | ||
|
||
X509Certificate certificate = getCertificate(certificado, keyStore); | ||
certificado.setCnpjCpf(getDocumentoFromCertificado(certificate)); | ||
certificado.setCnpjCpf(getDocumentoFromCertificado(certificate.getSubjectX500Principal().getName())); | ||
Date dataValidade = dataValidade(certificate); | ||
certificado.setVencimento(dataValidade.toInstant().atZone(ZoneId.systemDefault()).toLocalDate()); | ||
certificado.setDataHoraVencimento(dataValidade.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime()); | ||
|
@@ -259,20 +260,27 @@ public static Certificado getCertificadoByCnpjCpf(String cnpjCpf) throws Certifi | |
cnpjCpf)); | ||
} | ||
|
||
public static String getDocumentoFromCertificado(X509Certificate cert) { | ||
public static String getDocumentoFromCertificado(String texto) { | ||
|
||
String texto = cert.getSubjectX500Principal().getName(); | ||
// Primeiro, tenta encontrar CNPJs | ||
Pattern patternCNPJ = Pattern.compile("(?<!\\d)\\d{14}(?!\\d)"); | ||
Matcher matcherCNPJ = patternCNPJ.matcher(texto); | ||
|
||
// Padrão ajustado para buscar CPF (11 dígitos) ou CNPJ (14 dígitos) | ||
Pattern pattern = Pattern.compile("(?<!\\d)(\\d{14}|\\d{11})(?!\\d)"); | ||
Matcher matcher = pattern.matcher(texto); | ||
if (matcherCNPJ.find()) { | ||
// Se encontrar um CNPJ, retorna ele | ||
return matcherCNPJ.group(); | ||
} else { | ||
// Se não encontrar CNPJs, tenta encontrar CPFs | ||
Pattern patternCPF = Pattern.compile("(?<!\\d)\\d{11}(?!\\d)"); | ||
Matcher matcherCPF = patternCPF.matcher(texto); | ||
|
||
while (matcher.find()) { | ||
String documento = matcher.group(); | ||
if (documento.length() == 14 || documento.length() == 11) { | ||
return documento; | ||
if (matcherCPF.find()) { | ||
// Se encontrar um CPF, retorna ele | ||
return matcherCPF.group(); | ||
} | ||
} | ||
|
||
// Se não encontrar nenhum documento, retorna string vazia | ||
return ""; | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters