https://github.com/rcbandit111/profile- ... main/java/ com/profile/protection/admin/service/impl/KeysServiceImpl.java#L27
Мне нужно обработать запрос API на основе конфигурации yml:
Пример:
Код: Выделить всё
profile-protection:
dataClasses:
- name: USER_INFO
activeVersionName: version-1
versions:
- versionName: version-1
algorithm: AES
encryptKeyName: user
- name: SAMPLE_ENCRYPTION
activeVersionName: version-1
versions:
- versionName: version-1
algorithmName: AES
encryptKeyName: user_aes
Код: Выделить всё
@Component
public class EncryptionServiceFactory {
private final ApplicationContext applicationContext;
private final EncryptionServiceProperties encryptionServiceProperties;
@Autowired
public EncryptionServiceFactory(ApplicationContext applicationContext, EncryptionServiceProperties encryptionServiceProperties) {
this.applicationContext = applicationContext;
this.encryptionServiceProperties = encryptionServiceProperties;
}
public EncryptionService getService(String dataClassName) {
return (EncryptionService) applicationContext.getBean(dataClassName);
}
}
.....
// Implementation:
public interface EncryptionService {
String encrypt(String plaintext);
String decrypt(String ciphertext);
}
@Service("USER_INFO")
public class UserInfoEncryptionService implements EncryptionService {
// This would use configuration values for encryption
@Override
public String encrypt(String plaintext) {
// Implement AES/CBC/PKCS5Padding encryption logic here
return "encrypted-" + plaintext;
}
@Override
public String decrypt(String ciphertext) {
// Implement AES/CBC/PKCS5Padding decryption logic here
return "decrypted-" + ciphertext;
}
}
.....
@Service("SAMPLE_ENCRYPTION")
public class SampleEncryptionService implements EncryptionService {
// This would use configuration values for encryption
@Override
public String encrypt(String plaintext) {
// Implement AES/CBC/PKCS5Padding encryption logic here
return "encrypted-" + plaintext;
}
@Override
public String decrypt(String ciphertext) {
// Implement AES/CBC/PKCS5Padding decryption logic here
return "decrypted-" + ciphertext;
}
}
.....
@RestController
@RequestMapping("..........")
public class EncryptionController {
private final EncryptionServiceFactory encryptionServiceFactory;
@Autowired
public EncryptionController(EncryptionServiceFactory encryptionServiceFactory) {
this.encryptionServiceFactory = encryptionServiceFactory;
}
@PostMapping("/{dataClassName}/encrypt")
public String encrypt(@PathVariable String dataClassName, @RequestBody String plaintext) {
EncryptionService encryptionService = encryptionServiceFactory.getService(dataClassName);
return encryptionService.encrypt(plaintext);
}
}
Можно ли спроектировать код таким образом, чтобы я мог добавить больше Имена классов данных со значениями подконфигурации без добавления нового кода Java?
Подробнее здесь: https://stackoverflow.com/questions/790 ... le-configs
Мобильная версия