Java Code Examples for org.springframework.security.crypto.encrypt.Encryptors#text()

The following examples show how to use org.springframework.security.crypto.encrypt.Encryptors#text() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: EnvironmentDecryptApplicationInitializerTests.java    From spring-cloud-commons with Apache License 2.0 6 votes vote down vote up
@Test
public void errorOnDecrypt() {
	this.listener = new EnvironmentDecryptApplicationInitializer(
			Encryptors.text("deadbeef", "AFFE37"));
	ConfigurableApplicationContext context = new AnnotationConfigApplicationContext();
	TestPropertyValues.of("foo: {cipher}bar").applyTo(context);
	// catch IllegalStateException and verify
	try {
		this.listener.initialize(context);
	}
	catch (Exception e) {
		then(e).isInstanceOf(IllegalStateException.class);
	}
	// Assert logs contain warning even when exception thrown
	String sysOutput = this.outputCapture.toString();
	then(sysOutput).contains("Cannot decrypt: key=foo");
}
 
Example 2
Source File: EncryptionComponent.java    From syndesis with Apache License 2.0 5 votes vote down vote up
public EncryptionComponent(String encryptKey) {
    if (encryptKey != null) {
        this.textEncryptor = Encryptors.text(encryptKey, "deadbeef"); //$NON-NLS-1$
    } else {
        this.textEncryptor = null;
    }
}
 
Example 3
Source File: EncrypterUtil.java    From SMSC with Apache License 2.0 5 votes vote down vote up
/**
 * Method to encrypt fields based on {@link Encrypt} annotation.
 *
 * @param obj entity object
 */
public static void encrypt(Object obj) throws IllegalAccessException {
    CharSequence salt = getSalt(obj);

    TextEncryptor encryptor = Encryptors.text(secretKey, salt);
    for (Field field : obj.getClass().getDeclaredFields()) {
        if (field.isAnnotationPresent(Encrypt.class)) {
            field.setAccessible(true);
            field.set(obj, encryptor.encrypt((String) field.get(obj)));
            field.setAccessible(false);
        }
    }
}
 
Example 4
Source File: EncrypterUtil.java    From SMSC with Apache License 2.0 5 votes vote down vote up
/**
 * Method to decrypt fields based on {@link Encrypt} annotation.
 *
 * @param obj entity object
 */
public static void decrypt(Object obj) throws IllegalAccessException {
    CharSequence salt = getSalt(obj);

    TextEncryptor encryptor = Encryptors.text(secretKey, salt);
    for (Field field : obj.getClass().getDeclaredFields()) {
        if (field.isAnnotationPresent(Encrypt.class)) {
            field.setAccessible(true);
            field.set(obj, encryptor.decrypt((String) field.get(obj)));
            field.setAccessible(false);
        }
    }
}
 
Example 5
Source File: EnvironmentDecryptApplicationInitializerTests.java    From spring-cloud-commons with Apache License 2.0 5 votes vote down vote up
@Test
public void errorOnDecryptWithEmpty() {
	this.listener = new EnvironmentDecryptApplicationInitializer(
			Encryptors.text("deadbeef", "AFFE37"));
	this.listener.setFailOnError(false);
	ConfigurableApplicationContext context = new AnnotationConfigApplicationContext();
	TestPropertyValues.of("foo: {cipher}bar").applyTo(context);
	this.listener.initialize(context);
	// Assert logs contain warning
	String sysOutput = this.outputCapture.toString();
	then(sysOutput).contains("Cannot decrypt: key=foo");
	// Empty is safest fallback for undecryptable cipher
	then(context.getEnvironment().getProperty("foo")).isEqualTo("");
}
 
Example 6
Source File: EncryptionControllerMultiTextEncryptorTests.java    From spring-cloud-config with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldEncryptUsingApplicationAndProfiles() {

	this.controller = new EncryptionController(
			new SingleTextEncryptorLocator(Encryptors.text("application", "11")));

	// when
	String encrypted = this.controller.encrypt(this.application, this.profiles,
			this.data, TEXT_PLAIN);

	// then
	assertThat(this.controller.decrypt(this.application, this.profiles, encrypted,
			TEXT_PLAIN)).isEqualTo(this.data);
}
 
Example 7
Source File: StandardStringEncryptor.java    From summerframework with Apache License 2.0 4 votes vote down vote up
public StandardStringEncryptor(String password, String salt) {
    super(password, salt);
    encryptor = Encryptors.text(super.getPassword(), super.getSalt());
}
 
Example 8
Source File: DvAutoConfiguration.java    From syndesis with Apache License 2.0 4 votes vote down vote up
@Bean
public TextEncryptor getTextEncryptor() {
    return Encryptors.text(encryptKey, "deadbeef");
}
 
Example 9
Source File: Application.java    From syndesis with Apache License 2.0 4 votes vote down vote up
@Bean
public TextEncryptor getTextEncryptor() {
    return Encryptors.text(encryptKey, "deadbeef");
}
 
Example 10
Source File: MigrationConfiguration.java    From syndesis with Apache License 2.0 4 votes vote down vote up
@Bean
public TextEncryptor getTextEncryptor() {
    return Encryptors.text(encryptKey, "deadbeef");
}