Spring Security의 새 암호 인코더 사용 방법
스프링 시큐리티 3.1.4 기준.릴리스, 오래된org.springframework.security.authentication.encoding.PasswordEncoder
을 지지하여 감가상각되었습니다.org.springframework.security.crypto.password.PasswordEncoder
. 아직 제 어플리케이션이 공개되지 않았기 때문에 폐지된 API가 아닌 새로운 API로 변경하기로 했습니다.
지금까지 저는.ReflectionSaltSource
사용자의 등록일을 암호에 대한 사용자별 솔트로 자동으로 사용했습니다.
String encodedPassword = passwordEncoder.encodePassword(rawPassword, saltSource.getSalt(user));
Spring은 로그인 과정에서 사용자가 로그인을 할 수 있는지 없는지 확인하기 위해 내 콩을 사용하기도 했습니다.SHA-1의 기본 구현 때문에 새로운 암호 인코더에서는 이를 달성할 수 없습니다.StandardPasswordEncoder
은(는) 인코더 생성 중에 전역 비밀 솔트를 추가할 수 있는 유일한 기능을 가지고 있습니다.
감가되지 않은 API로 설정하는 합리적인 방법이 있습니까?
실제로 기존 형식의 사용자를 등록하지 않았다면 대신 BCrpt 암호 인코더를 사용하는 것으로 전환하는 것이 가장 좋습니다.
이것은 훨씬 덜 번거롭습니다. 소금에 대해 전혀 걱정할 필요가 없기 때문입니다. 세부 정보는 인코더 내에 완전히 캡슐화되어 있기 때문입니다.Bcrypt를 사용하는 것은 일반 해시 알고리즘을 사용하는 것보다 강력하며 다른 언어를 사용하는 응용 프로그램과 호환되는 표준이기도 합니다.
새로운 응용 프로그램을 위해 다른 옵션을 선택할 이유가 없습니다.
여기 저를 위해 작동하는 B 크립토의 구현이 있습니다.
spring-security.xml에서
<authentication-manager >
<authentication-provider ref="authProvider"></authentication-provider>
</authentication-manager>
<beans:bean id="authProvider" class="org.springframework.security.authentication.dao.DaoAuthenticationProvider">
<beans:property name="userDetailsService" ref="userDetailsServiceImpl" />
<beans:property name="passwordEncoder" ref="encoder" />
</beans:bean>
<!-- For hashing and salting user passwords -->
<beans:bean id="encoder" class="org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder"/>
자바수업중
PasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
String hashedPassword = passwordEncoder.encode(yourpassword);
스프링 보안의 더 자세한 예를 보려면 여기를 클릭하십시오.
이것이 도움이 되길 바랍니다.
감사해요.
저도 비슷한 문제가 있었습니다.사용자가 암호를 변경하거나 재등록하지 않도록 기존 암호화된 암호(Base64/SHA-1/Random salt Encoded)를 유지해야 했습니다.하지만 저는 앞으로도 BCrypt 인코더를 사용하고 싶었습니다.
나의 해결책은 일치하기 전에 어떤 암호화 방법이 먼저 사용되었는지 확인하는 맞춤형 디코더를 작성하는 것이었습니다(BC암호화된 것부터 시작).$
).
salt 문제를 해결하기 위해 수정된 사용자 개체를 통해 연결된 String of salt + 암호화된 암호를 디코더에 전달합니다.
디코더
@Component
public class LegacyEncoder implements PasswordEncoder {
private static final String BCRYP_TYPE = "$";
private static final PasswordEncoder BCRYPT = new BCryptPasswordEncoder();
@Override
public String encode(CharSequence rawPassword) {
return BCRYPT.encode(rawPassword);
}
@Override
public boolean matches(CharSequence rawPassword, String encodedPassword) {
if (encodedPassword.startsWith(BCRYP_TYPE)) {
return BCRYPT.matches(rawPassword, encodedPassword);
}
return sha1SaltMatch(rawPassword, encodedPassword);
}
@SneakyThrows
private boolean sha1SaltMatch(CharSequence rawPassword, String encodedPassword) {
String[] saltHash = encodedPassword.split(User.SPLIT_CHAR);
// Legacy code from old system
byte[] b64salt = Base64.getDecoder().decode(saltHash[0].getBytes());
byte[] validHash = Base64.getDecoder().decode(saltHash[1]);
byte[] checkHash = Utility.getHash(5, rawPassword.toString(), b64salt);
return Arrays.equals(checkHash, validHash);
}
}
사용자 개체
public class User implements UserDetails {
public static final String SPLIT_CHAR = ":";
@Id
@Column(name = "user_id", nullable = false)
private Integer userId;
@Column(nullable = false, length = 60)
private String password;
@Column(nullable = true, length = 32)
private String salt;
.
.
@PostLoad
private void init() {
username = emailAddress; //To comply with UserDetails
password = salt == null ? password : salt + SPLIT_CHAR + password;
}
또한 후크를 추가하여 암호를 새 B 암호 형식으로 다시 인코딩하고 대체할 수 있습니다.따라서 기존 방식을 단계적으로 폐지합니다.
방금 인터넷을 돌아다니면서 이것과 봄의 옵션에 대해 읽은 후에 루크의 답을 다시 찾고 싶습니다. B 크립토(봄의 소스 코드에 언급됨)를 사용하십시오.
해시/솔트를 사용하는 이유와 BCrpt를 사용하는 것이 좋은 선택인 이유를 설명하기 위해 찾은 가장 좋은 자료는 다음과 같습니다. 솔티드 패스워드 해시 - 올바르게 실행하기.
언급URL : https://stackoverflow.com/questions/17444258/how-to-use-new-passwordencoder-from-spring-security
'programing' 카테고리의 다른 글
Laravel Valet Linux: MariaDB/Mysql을 연결할 수 없습니다. (0) | 2023.10.09 |
---|---|
다른 셀의 데이터를 기반으로 엑셀에서 URL을 생성하려면 어떻게 해야 합니까? (0) | 2023.10.09 |
파워셸에서 문자열을 문자열로 분할하는 방법 (0) | 2023.10.04 |
양식 제출 jQuery가 작동하지 않습니다. (0) | 2023.10.04 |
새로 고침과 같은 표준 Android 메뉴 아이콘 (0) | 2023.10.04 |