JwtToken login 시 sha 256 적용

김명래·2023년 4월 5일
0

기존 Login시에 sha256으로 인코딩한 뒤 db에 암호화 되어있는 pwd와 매치시켰던 로직을 jwtToken 로직에 적용하고자 한다.

@Transactional
    public TokenInfo loginMember(String userId, String password)  {
        MessageDigest sha = null;
        try {
            sha = MessageDigest.getInstance("SHA-256");
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        }
        sha.update(password.getBytes());
        byte[] digest = sha.digest();
        password = Base64.getEncoder().encodeToString(digest);

        UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(userId, password);
        Authentication authentication = authenticationManagerBuilder.getObject().authenticate(authenticationToken);
        return jwtTokenProvider.generateToken(authentication);
    }

Service에 있는 loginMember method이고 전달받은 password를 SHA-256알고리즘으로 인코딩 하고 password를 넘겨주면 된다.

profile
독자보다 필자를 위해 포스팅합니다

0개의 댓글