암호화된 자격 증명과 키가 손상된 경우 어떻게 하시겠습니까?

What will you do if your encrypted credentials and key got compromised?



이것은 수사학적 질문입니다.

API 키, 토큰 또는 비밀번호를 사용하여 API를 보호하는 것은 모든 애플리케이션에서 일반적입니다. 기본 인증의 경우 일반적으로 자격 증명을 암호화한 후 저장하려고 합니다. 여기서 md5 또는 SHA-256과 같은 단방향 해시 함수를 사용하여 기본 인증을 달성하는 방법을 살펴보겠습니다.

아이디어



아이디어는 처음으로 또는 가입 중에 해시 함수를 사용하여 자격 증명을 해시 문자열로 변환하는 것입니다. 로그인하는 동안 사용자가 입력한 자격 증명을 해시 문자열로 변환하고 동일한지 확인합니다. 그렇게 간단합니다 :D.



여기서는 암호화된 암호를 저장하지 않으므로 애플리케이션에 대한 공격이 발생하고 데이터가 손상되더라도 자격 증명은 안전합니다.

구현



Java 및 스프링 부트( click here )를 사용하여 동일한 것을 완전히 구현하려면

연습을하겠습니다.

그래서 우리는 최소한의 컨트롤러 인터페이스와 구현을 가지고 있습니다.

@RestController
public interface LoginApi {

    @GetMapping("/user/login")
    @ResponseBody
    String userLogin();
}



@Component
public class LoginApiImpl implements LoginApi{

    /**
     * User login string.
     *
     * @return the string
     */
    @Override
    public String userLogin() {
        return "Login Successful";
    }
}


그런 다음 인증을 확인하기 위한 필터 구현이 있습니다.
Basic Authentication Filter Implementation

서비스 구현은 해시된 문자열과 주어진 자격 증명을 확인하는 곳입니다. 단순화를 위해 암호에 집중하겠습니다.

/**
 * The type Login service.
 */
@Service
public class LoginService {

    /**
     * The constant USER.
     */
    private static final String USER = "ADMIN";

    /**
     * The constant PASSWORD.
     * Actual value is : password
     */
    private static final String PASSWORD = "5e884898da28047151d0e56f8dc6292773603d0d6aabbdd62a11ef721d1542d8";

    /**
     * Check authentication boolean.
     *
     * @param user     the user
     * @param password the password
     * @return the boolean
     * @throws NoSuchAlgorithmException the no such algorithm exception
     */
    public boolean checkAuthentication(String user,String password) throws NoSuchAlgorithmException {
        String generatedHash = generateHash(password);
        if(PASSWORD.equals(generatedHash) && USER.equals(user))
            return true;
        return false;
    }

    /**
     * Generate hash string.
     *
     * @param password the password
     * @return the string
     * @throws NoSuchAlgorithmException the no such algorithm exception
     */
    public  String generateHash(String password) throws NoSuchAlgorithmException {
        MessageDigest md = MessageDigest.getInstance("SHA-256");
        byte[] hash =  md.digest(password.getBytes(StandardCharsets.UTF_8));
        BigInteger number = new BigInteger(1, hash);
        StringBuilder hexString = new StringBuilder(number.toString(16));
        while (hexString.length() < 64)
        {
            hexString.insert(0, '0');
        }
        return hexString.toString();
    }
}



마지막으로 코드를 실행하고 API를 실행해 보겠습니다. {이 예에서 사용자 이름은 ADMIN이고 비밀번호는 password}입니다.

다음은 위 API에 대한 curl입니다. curl --location --request GET ' http://localhost:8443/service/api/v1/user/login '\
--header '인증: 기본 QURNSU46cGFzc3dvcmQ='



지금은 여기까지입니다. 도움이 되었기를 바랍니다.
의견 섹션에서 의견을 공유하십시오.

좋은 웹페이지 즐겨찾기