Spring Boot 암호화, 2024-03-04

sunj - Mar 4 - - Dev Community

configuration.EncryptionConfig.java

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.crypto.factory.PasswordEncoderFactories;
import org.springframework.security.crypto.password.PasswordEncoder;

@Configuration
public class EncryptionConfig {
    @Bean
    public PasswordEncoder encoder(){
        return PasswordEncoderFactories.createDelegatingPasswordEncoder();
    }

}
Enter fullscreen mode Exit fullscreen mode

비밀번호를 암호화하기 위해서 PasswordEncoderFactories를 사용

service.ApiServiceImpl.java

boolean checkPassword = encryptionConfig.encoder().matches(loginDTO.getPassword(),member.getPassword());
Enter fullscreen mode Exit fullscreen mode

비밀번호를 일치하는지 여부를 확인하기 위해서 matches 메소드 사용
비밀번호 외 암호화가 필요한 개인정보, 주민등록번호와 같은 것은 복호화가 필요하므로

provider.EncoderProvider.java

@RequiredArgsConstructor
@Component
public class EncoderProvider {

    public String encrypt(String regNo) {
        byte[] regNoBytes = regNo.getBytes();
        byte[] encodeByte = Base64.encodeBase64(regNoBytes);

        return new String(encodeByte);
    }
    public String decrypt(String regNo) {
        byte[] regNoBytes = regNo.getBytes();
        byte[] decodeByte = Base64.decodeBase64(regNoBytes);

        return new String(decodeByte);
    }

}
Enter fullscreen mode Exit fullscreen mode

Base64로 인코더 후 Base64를 디코딩

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Terabox Video Player