Другое решение, использующее аннотации Spring @Configuration и @ Bean
AbstractEncryptor Abstract Class с параметрами в конструкторе
package com.jmendoza.springboot.crypto.v2.cipher;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
public abstract class AbstractEncryptor {
private byte[] key;
private String algorithm;
public AbstractEncryptor(String key, String algorithm) {
this.key = key.getBytes(StandardCharsets.UTF_8);
this.algorithm = algorithm;
}
public String encrypt(String plainText) throws Exception {
SecretKeySpec secretKey = new SecretKeySpec(key, algorithm);
Cipher cipher = Cipher.getInstance(algorithm);
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
return new String(Base64.getEncoder().encode(cipher.doFinal(plainText.getBytes(StandardCharsets.UTF_8))));
}
public String decrypt(String cipherText) throws Exception {
SecretKeySpec secretKey = new SecretKeySpec(key, algorithm);
Cipher cipher = Cipher.getInstance(algorithm);
cipher.init(Cipher.DECRYPT_MODE, secretKey);
return new String(cipher.doFinal(Base64.getDecoder().decode(cipherText)));
}
}
CertificateEncryptor Класс расширяет AbstractEncryptor
package com.jmendoza.springboot.crypto.v2.cipher;
public class CertificateEncryptor extends AbstractEncryptor {
public CertificateEncryptor(String key, String algorithm) {
super(key, algorithm);
}
}
DestinyEncryptorКласс расширяет AbstractEncryptor
package com.jmendoza.springboot.crypto.v2.cipher;
public class DestinyEncryptor extends AbstractEncryptor {
public DestinyEncryptor(String key, String algorithm) {
super(key, algorithm);
}
}
Класс ConfigEncryptor: создание bean-компонентов, передающих параметры конструктору
package com.jmendoza.springboot.crypto.v2.config;
import com.jmendoza.springboot.crypto.v2.cipher.CertificateEncryptor;
import com.jmendoza.springboot.crypto.v2.cipher.DestinyEncryptor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class ConfigEncryptor {
@Value("${security.encryptor.key.certificate}")
String keyCertificate;
@Value("${security.encryptor.algorithm}")
String algorithm;
@Value("${security.encryptor.key.destiny}")
String keyDestiny;
@Bean
public CertificateEncryptor certificateEncryptor() {
return new CertificateEncryptor(keyCertificate, algorithm);
}
@Bean
public DestinyEncryptor destinyEncryptor() {
return new DestinyEncryptor(keyDestiny, algorithm);
}
}
application.properties
server.port=8082
security.encryptor.algorithm=AES
security.encryptor.key.destiny=L2dvx46dfJMaiJA0
security.encryptor.key.certificate=M5mjd46dfSAaiLP4
Encryptor2Controller Класс: используйте классCertificateEncryptor
package com.jmendoza.springboot.crypto.v2.controller;
import com.jmendoza.springboot.crypto.v2.cipher.CertificateEncryptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/v2/cipher")
public class Encryptor2Controller {
@Autowired
CertificateEncryptor certificateEncryptor;
@GetMapping(value = "encrypt/{value}")
public String encrypt(@PathVariable("value") final String value) throws Exception {
return certificateEncryptor.encrypt(value);
}
@GetMapping(value = "decrypt/{value}")
public String decrypt(@PathVariable("value") final String value) throws Exception {
return certificateEncryptor.decrypt(value);
}
}
Пример
http://localhost:8082/v2/cipher/encrypt/jonathan
nrWRgt1CRb9AUYZQ6Ut0EA ==
http://localhost:8082/v2/cipher/decrypt/nrWRgt1CRb9AUYZQ6Ut0EA==
Джонатан
Github: https://github.com/JonathanM2ndoza/Spring-Boot-Crypto
Просмотр пакета / com / jmendoza / springboot / crypto / v2 /