BAEL-1489: Applying Baeldung code styles and cleaning up

This commit is contained in:
Holger Steinhauer 2018-02-07 21:13:00 +00:00
parent 62d9eed9fd
commit e61b157057
3 changed files with 71 additions and 36 deletions

View File

@ -0,0 +1,33 @@
package com.baeldung.passwordstorage;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.DelegatingPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.crypto.scrypt.SCryptPasswordEncoder;
import java.util.HashMap;
import java.util.Map;
@Configuration
public class BaeldungPasswordEncoderSetup {
@Bean
public PasswordEncoder passwordEncoder() {
// set up the list of supported encoders and their prefixes
String encodingId = "rot13";
Map<String, PasswordEncoder> encoders = new HashMap<>();
encoders.put(encodingId, new Rot13PasswordEncoder());
encoders.put("scrypt", new SCryptPasswordEncoder());
encoders.put("bcrypt", new BCryptPasswordEncoder());
// get an instance of the DelegatingPasswordEncoder, set up to use our instance as default encoder
DelegatingPasswordEncoder delegatingPasswordEncoder = new DelegatingPasswordEncoder(encodingId, encoders);
// configure our instance as default encoder for actual matching
delegatingPasswordEncoder.setDefaultPasswordEncoderForMatches(encoders.get(encodingId));
return delegatingPasswordEncoder;
}
}

View File

@ -9,22 +9,24 @@ import org.springframework.security.crypto.password.PasswordEncoder;
*/
public class Rot13PasswordEncoder implements PasswordEncoder {
@Override
public String encode(CharSequence rawPassword) {
StringBuffer result = new StringBuffer(rawPassword.length());
rawPassword.chars().forEach(charCode -> {
if (charCode >= 65 && charCode <= 77 || charCode >= 97 && charCode <= 109) {
result.append(Character.toChars(charCode + 13));
} else if (charCode >= 78 && charCode <= 90 || charCode >= 110 && charCode <= 133) {
result.append(Character.toChars(charCode - 13));
}
});
@Override
public String encode(CharSequence rawPassword) {
StringBuffer result = new StringBuffer(rawPassword.length());
rawPassword
.chars()
.forEach(charCode -> {
if (charCode >= 65 && charCode <= 77 || charCode >= 97 && charCode <= 109) {
result.append(Character.toChars(charCode + 13));
} else if (charCode >= 78 && charCode <= 90 || charCode >= 110 && charCode <= 133) {
result.append(Character.toChars(charCode - 13));
}
});
return result.toString();
}
return result.toString();
}
@Override
public boolean matches(CharSequence rawPassword, String encodedPassword) {
return encode(rawPassword).equals(encodedPassword);
}
@Override
public boolean matches(CharSequence rawPassword, String encodedPassword) {
return encode(rawPassword).equals(encodedPassword);
}
}

View File

@ -7,30 +7,30 @@ import static org.junit.Assert.assertThat;
public class Rot13PasswordEncoderTest {
private final Rot13PasswordEncoder encoder = new Rot13PasswordEncoder();
private final Rot13PasswordEncoder encoder = new Rot13PasswordEncoder();
@Test
public void given_theEncodedPassword_should_returnTheClearTextPassword() {
String password = "baeldung";
String encoded = encoder.encode(password);
String actualResult = encoder.encode(encoded);
@Test
public void given_theEncodedPassword_should_returnTheClearTextPassword() {
String password = "baeldung";
String encoded = encoder.encode(password);
String actualResult = encoder.encode(encoded);
assertThat(actualResult, is(password));
}
assertThat(actualResult, is(password));
}
@Test
public void given_correctPassword_should_returnTrue() {
String password = "baeldung";
String encoded = encoder.encode(password);
boolean actualResult = encoder.matches(password, encoded);
@Test
public void given_correctPassword_should_returnTrue() {
String password = "baeldung";
String encoded = encoder.encode(password);
boolean actualResult = encoder.matches(password, encoded);
assertThat(actualResult, is(true));
}
assertThat(actualResult, is(true));
}
@Test
public void given_incorrectPassword_should_returnFalse() {
boolean actualResult = encoder.matches("baeldung", "spring");
@Test
public void given_incorrectPassword_should_returnFalse() {
boolean actualResult = encoder.matches("baeldung", "spring");
assertThat(actualResult, is(false));
}
assertThat(actualResult, is(false));
}
}