Как это сделать?
Код: Выделить всё
import com.example.em_card_service.data.properties.EncryptionProperties;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.crypto.encrypt.Encryptors;
import org.springframework.security.crypto.encrypt.TextEncryptor;
import java.util.List;
@Configuration
@RequiredArgsConstructor
public class SecurityConfig {
private final EncryptionProperties encryptionProperties;
// more beans
@Bean
public TextEncryptor textEncryptor() {
return Encryptors.text(encryptionProperties.getPassword(), encryptionProperties.getSalt());
}
}
Код: Выделить всё
@Bean
public TextEncryptor textEncryptor() {
AesBytesEncryptor bytesEncryptor = new AesBytesEncryptor(
encryptionProperties.getPassword(),
encryptionProperties.getSalt(),
KeyGenerators.shared(16)
);
return new TextEncryptor() {
@Override
public String encrypt(String text) {
byte[] encrypted = bytesEncryptor.encrypt(text.getBytes(StandardCharsets.UTF_8));
return HexFormat.of().formatHex(encrypted);
}
@Override
public String decrypt(String encryptedText) {
byte[] decrypted = bytesEncryptor.decrypt(HexFormat.of().parseHex(encryptedText));
return new String(decrypted, StandardCharsets.UTF_8);
}
};
}