BAEL-1489: Applying Baeldung code styles and cleaning up
This commit is contained in:
parent
62d9eed9fd
commit
e61b157057
@ -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;
|
||||||
|
}
|
||||||
|
}
|
@ -9,22 +9,24 @@ import org.springframework.security.crypto.password.PasswordEncoder;
|
|||||||
*/
|
*/
|
||||||
public class Rot13PasswordEncoder implements PasswordEncoder {
|
public class Rot13PasswordEncoder implements PasswordEncoder {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String encode(CharSequence rawPassword) {
|
public String encode(CharSequence rawPassword) {
|
||||||
StringBuffer result = new StringBuffer(rawPassword.length());
|
StringBuffer result = new StringBuffer(rawPassword.length());
|
||||||
rawPassword.chars().forEach(charCode -> {
|
rawPassword
|
||||||
if (charCode >= 65 && charCode <= 77 || charCode >= 97 && charCode <= 109) {
|
.chars()
|
||||||
result.append(Character.toChars(charCode + 13));
|
.forEach(charCode -> {
|
||||||
} else if (charCode >= 78 && charCode <= 90 || charCode >= 110 && charCode <= 133) {
|
if (charCode >= 65 && charCode <= 77 || charCode >= 97 && charCode <= 109) {
|
||||||
result.append(Character.toChars(charCode - 13));
|
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
|
@Override
|
||||||
public boolean matches(CharSequence rawPassword, String encodedPassword) {
|
public boolean matches(CharSequence rawPassword, String encodedPassword) {
|
||||||
return encode(rawPassword).equals(encodedPassword);
|
return encode(rawPassword).equals(encodedPassword);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -7,30 +7,30 @@ import static org.junit.Assert.assertThat;
|
|||||||
|
|
||||||
public class Rot13PasswordEncoderTest {
|
public class Rot13PasswordEncoderTest {
|
||||||
|
|
||||||
private final Rot13PasswordEncoder encoder = new Rot13PasswordEncoder();
|
private final Rot13PasswordEncoder encoder = new Rot13PasswordEncoder();
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void given_theEncodedPassword_should_returnTheClearTextPassword() {
|
public void given_theEncodedPassword_should_returnTheClearTextPassword() {
|
||||||
String password = "baeldung";
|
String password = "baeldung";
|
||||||
String encoded = encoder.encode(password);
|
String encoded = encoder.encode(password);
|
||||||
String actualResult = encoder.encode(encoded);
|
String actualResult = encoder.encode(encoded);
|
||||||
|
|
||||||
assertThat(actualResult, is(password));
|
assertThat(actualResult, is(password));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void given_correctPassword_should_returnTrue() {
|
public void given_correctPassword_should_returnTrue() {
|
||||||
String password = "baeldung";
|
String password = "baeldung";
|
||||||
String encoded = encoder.encode(password);
|
String encoded = encoder.encode(password);
|
||||||
boolean actualResult = encoder.matches(password, encoded);
|
boolean actualResult = encoder.matches(password, encoded);
|
||||||
|
|
||||||
assertThat(actualResult, is(true));
|
assertThat(actualResult, is(true));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void given_incorrectPassword_should_returnFalse() {
|
public void given_incorrectPassword_should_returnFalse() {
|
||||||
boolean actualResult = encoder.matches("baeldung", "spring");
|
boolean actualResult = encoder.matches("baeldung", "spring");
|
||||||
|
|
||||||
assertThat(actualResult, is(false));
|
assertThat(actualResult, is(false));
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user