티스토리 뷰

앞에 포스팅에서 sha256을 이용한 비밀번호 암호화에 대해 알아 봤다.

다른 암호화도 대부분 마찬가지지만, 비밀 번호를 암호화 했다고 해도 DB에 저장 됐을때 같은 암호면 

암호화된 형태(hash값이)가 같다. 

 

그로 인해 하나의 암호화된 값을 알아 내면 같은 암호를 쉽게 찾을수 있다. 이를 방지하기 위한 방법이 salt암호화 방식이다. 별다른건 아니고 사용자가 입력한 비밀번호에 사용자마다 중복되지 않는 특정 문자를 추가하여 암호화 하는 방식이다. 

예를 들면 회원 가입시 중복되지 않는 아이디를 (아이디 + 비밀번호) 또는 (비밀번호 + 아이디) 형태로 붙이고 암호화를 하면된다.

 

spring security에서 지원하는 간단한 방식을 통해 username을 사용한 salt 방식을 알아보겠다.

 

1.security-context.xml 의 password-encoder 에 <salt-source user-property="username"/>을 추가 한다.

ex)

 
<authentication-manager>
<authentication-provider user-service-ref="userService">
<password-encoder ref="passwordEncoder">
<salt-source user-property="username"/>
</password-encoder>
</authentication-provider>
</authentication-manager>

 

2.이전에 암호화를 위해 생성한 ShaEncoder에 Method를 추가한다.

 

public String saltEncoding(String str,String salt){

return encoder.encodePassword(str,salt);

} 

 

ex)

 
package com.min.study.core.util;

import javax.annotation.Resource;
import org.springframework.security.authentication.encoding.ShaPasswordEncoder;
import org.springframework.stereotype.Service;

@Service("shaEncoder")
public class ShaEncoder {
 
 @Resource(name="passwordEncoder")
 private ShaPasswordEncoder encoder;
 
 public String encoding(String str){
  return encoder.encodePassword(str,null);
 }

 public String saltEncoding(String str,String salt){
  return encoder.encodePassword(str,salt);
 }
}

3.사용자 가입시 saltEncoding을 이용하여 비밀번호를 암호화 한다.

String dbpw = encoder.saltEncoding(passwd, email);

ex)

 
@RequestMapping(value = "/user/insertUser",method=RequestMethod.POST)
public String insertUser(@RequestParam("email")String email, 
						@RequestParam("passwd")String passwd, 
						@RequestParam("authority")String authority){
 String dbpw = encoder.saltEncoding(passwd, email);
 Map<String, String> paramMap = new HashMap<String, String>();
 paramMap.put("email", email);
 paramMap.put("passwd", dbpw);
 paramMap.put("authority", authority);
 int result = dao.insertUser(paramMap);
 logger.info("result ===> {}", result);
 return "/user/loginPage";
} 

 

4.새로운 회원 가입 후 암호화 테스트(기존의 사용자는 모두 비밀번호 재생성을 해야한다.)

-----------------------------------------------------------------------------------------

소스 확인 : https://github.com/ParkMinKyu/security

댓글