Bcrypt ์ธ์ฝ๋ฉ
Bcrypt์ ์ํธํ ๋ฐฉ์์ ์์๋ณด๊ณ ์คํ๋ง ์ํ๋ฆฌํฐ๋ก Bcrypt ์ธ์ฝ๋ฉ์ ์ ์ฉํด๋ณด์.
Bcrypt ์ํธํ ๋ฐฉ์
- Bcrpyt๋ ๋จ๋ฐฉํฅ ํด์ ํจ์๋ฅผ ์ด์ฉํ ์ํธํ์ด๋ค.
- ๋จ๋ฐฉํฅ์ด๊ธฐ ๋๋ฌธ์ Bcrypt ์ธ์ฝ๋ฉ ๋ ์ํธ๋ ๋ณตํธํ๊ฐ ๋ถ๊ฐ๋ฅํ๋ค.
- ๋น๋ฐ๋ฒํธ ์์ฒด๋ง ํด์ ํจ์๋ก ์ํธํ๋ฅผ ํ๋ฉด ํ๋ฌธ๋ค์ ์ธ์ฝ๋ฉํ์ฌ ๋น๊ตํด๋ณผ ์ ์๊ธฐ ๋๋ฌธ์ Bcrypt๋ ์ํธํ ๊ณผ์ ์์ salt(์์์ ์ฒจ๊ฐ ๋ฌธ์์ด)๋ฅผ ์ด์ฉํ๋ค.
ex) ๋น๋ฐ๋ฒํธ : "1234"
์ํธํ("1234" , salt) == ์ํธํ๋ ๋ฌธ์์ด
- ํ๋ฌธ์ผ๋ก ๋น๋ฐ๋ฒํธ๋ฅผ ์กฐํํ ๋ ์กฐํ๋ฅผ ์ํด salt ๋ฌธ์์ด์ ์ ํ์๋ ์๋ค. ์ด๋ ๋ค์๊ณผ ๊ฐ์ ํน์ฑ ๋๋ฌธ์ด๋ค.
์ํธํ("1234" , ์ํธํ๋ ๋ฌธ์์ด) == ์ํธํ๋ ๋ฌธ์์ด
- ์ฆ, ๊ธฐ์กด์ ์ํธํ๋ฅผ ํ๋ ํ๋ฌธ๊ณผ ์ํธํ๋ ๋ฌธ์์ด์ Bcrypt Encode ํ๋ค๋ฉด ์ํธํ๋ ๋ฌธ์์ด๊ณผ ๊ฐ์ ๋ฌธ์์ด์ด ๊ฒฐ๊ณผ๋ก ๋์จ๋ค.
์คํ๋ง ์ํ๋ฆฌํฐ๋ก ๊ตฌํ
์คํ๋ง ์ํ๋ฆฌํฐ์์๋ PasswordEncoderFactories
๋ก Bcrypt Encoding ๊ตฌํ์ ์ ๊ณตํด ์ฃผ๊ณ ์๊ธฐ ๋๋ฌธ์, ๋ค์๊ณผ ๊ฐ์ด @Bean์ ์ ์ธํด์ ์ฌ์ฉํ ์ ์๋ค.
@Bean
public PasswordEncoder passwordEncoder(){
return PasswordEncoderFactories.createDelegatingPasswordEncoder();
}
์ฐธ๊ณ ๋ก ์คํ๋ง ์ํ๋ฆฌํฐ์ Bcrypt Encoding์ ๋ณด์์ ์ํด ์๋์ ์ผ๋ก Encoding ์๊ฐ์ ์ด์ง ๋ฆ์ถฐ์ ์งง์ ์๊ฐ ๋์ ์ฌ๋ฌ ๋ฒ์ ์ํธํ ์๋๋ฅผ ๋ฐฉ์งํ๋ค.
๊ฐ์
ํ๋ ๊ณ์ ์ ๋ํ Dto๋ฅผ ๋ฐ์์ ํ์๊ฐ์
์ฒ๋ฆฌ๋ฅผ ํ๋ ์ํฉ์ด๋ผ๋ฉด, ๋ค์๊ณผ ๊ฐ์ด Encoding ํ ์ ์๋ค.
Account account = Account.builder()
.nickname(dto.getNickname())
.email(dto.getEmail())
.password(passwordEncoder.encode(dto.getPassword()))
.role(Role.ROLE_USER)
.build();
accountRepository.save(account);
.password(passwordEncoder.encode(dto.getPassword()))
๋ก Bcrpyt Encoding ํ ๋น๋ฐ๋ฒํธ๋ฅผ DB์ ๋ฃ์ด์ค๋ค.
Test Code
๊ธฐ์กด์ ํ๋ฌธ๊ณผ DB์ ์ ์ฅ๋ ํจ์ค์๋ ๊ฐ์ด ๋ค๋ฅธ์ง ํ ์คํธํด๋ณด์.
@DisplayName("ํจ์ค์๋ ์ธ์ฝ๋ฉ ํ
์คํธ")
@Test
public void password_enc_test() throws Exception{
//when
SignupDto signupDto = SignupDto.builder()
.nickname("testUser")
.password("12345678")
.email("testUser@test.com")
.build();
//given
mockMvc.perform(post("/sign-up")
.content(objectMapper.writeValueAsString(signupDto))
.contentType(MediaType.APPLICATION_JSON));
Account account = accountRepository.findByEmail("testUser@test.com");
//then
Assertions.assertNotEquals(account.getPassword(),"12345678");
}

ํ๋ฌธ์ธ "12345678"๊ณผ DB์ ์ ์ฅ๋ ํ๋ฌธ์ ์ํธํ ํ ๊ฐ์ ๋ค๋ฅด๋ฏ๋ก ํ ์คํธ ์ฝ๋๋ฅผ ํต๊ณผํ๋ค.
'๋ณด์' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
์ฟ ํค-์ธ์ , JWT ํ ํฐ ์ธ์ฆ (0) | 2022.03.17 |
---|---|
์ํธํ ํด์ ํจ์ (0) | 2022.03.13 |
A5/1 แแ ฎแแ งแซ(C++) (0) | 2022.03.13 |