Spring Security의 Bcrypt 암호화를 사용해 만든 비밀번호를 유저 테이블에 저장할 때
입력한 비밀번호에 랜덤한 salt 값이 더해져 암호화되는 것을 알 수 있다.

(같은 비밀번호로 변경해도 저정되는 값이 다르기 때문)
그렇다면 입력한 비밀번호 외에 암호화에 사용된 salt 값이 어떤 값인지, 그 값은 어디에 저장되는지 알아야 한다.
하지만 Bcrypt 암호화의 salt 값따로 저장되는 게 아닌 해시값에 이어붙여 함께 저장되기 때문에 따로 salt 값 저장이

필요하지 않다.


$2a$10$Yz4die0hPMjS3.6njaxmmOyrwArwsC9NEs5kwOI0Gh04uLjHaON2e
위와 같은 형태로 해시값을 생성하는데 이는 '$'로 구분되는 3가지의 필드라고 보면 된다.

'2a'는 사용된 bcrypt알고리즘의 버전을 의미하며
'10'은 cost factor이다. cost factor가 10이라는 의미는 key derivation 함수가 2^10번 반복 실행된다는 의미로 이 값이 커질 수록 해시값을 구하는 속도가 느려진다.
'Yz4die0hPMjS3.6njaxmmOyrwArwsC9NEs5kwOI0Gh04uLjHaON2e'가 바로 salt 값과 암호화된 비밀번호 값이다.

'Framework > Spring Security' 카테고리의 다른 글

[Framework] Spring Security 세션값 변경  (0) 2023.07.19
AppLoginUser pmUser = null;

if(pmList.isEmpty() == false) {
	PmVO pmVo = pmList.get(0);
    
	List<GrantedAuthority> authList = new ArrayList<>();
    
    	if(pmVo != null) {
        
            List<SecurityRole> roles = new ArrayList<SecurityRole>(); 
            
            roles.add(SecurityRole.USER); // 기본권한
            
            if(pmVo.isLoginAble()) {
            	roles.add(SecurityRole.PM); // PM권한
            }
            
            List<String> adminEmails = new ArrayList<String>();
            adminEmails.add("champcom90@gmail.com");
            
            if(adminEmails.contains(pmVo.getId())) {
            	roles.add(SecurityRole.ADMIN); // 관리자권한
            }
            
            // 권한 ENUM을 시큐리티 ROLE로 변환
            for(SecurityRole r : roles) {
            	authList.add(new SimpleGrantedAuthority(r.getValue())); 
            }
    	}
        
        pmUser = AppLoginUser.ofPm(pmVo, authList);
        
        springUserSupportService.bindProjectScope(pmUser);
        
        pmUser.setLoginIp(UtilRequest.getClientIp()); // 로그인한 IP설정
        
        Authentication authentication = new UsernamePasswordAuthenticationToken(pmUser, null, pmUser.getAuthorities());
        
        SecurityContext securityContext = SecurityContextHolder.getContext();
        
        securityContext.setAuthentication(authentication);
        
        session.invalidate(); // 세션 초기화(로그인 후 세션값 변경되어야 하며 로그인 상태에서의 세션값은 고정이어야 한다.)
        
        session = request.getSession(); // 세션 재발급(로그인 후 세션값 변경되어야 하며 로그인 상태에서의 세션값은 고정이어야 한다.)
        
        // session.setAttribute("SPRING_SECURITY_CONTEXT", securityContext); // 이전에는 SPRING_SECURITY_CONTEXT 속성에 바로 접근해서 세션값 변경하는 게 가능했지만 지금은 불가능

session.invalidate(); // 세션 초기화


session = request.getSession(); // 세션 재발급

+ Recent posts