๊ด€๋ฆฌ ๋ฉ”๋‰ด

gi_dor

[error] update ๋น„๋ฐ€๋ฒˆํ˜ธ ์•”ํ˜ธํ™” ๋ฌธ์ œ ๋ณธ๋ฌธ

Back_End/Issue

[error] update ๋น„๋ฐ€๋ฒˆํ˜ธ ์•”ํ˜ธํ™” ๋ฌธ์ œ

๊ธฐ๋Œ 2024. 4. 29. 17:59

 

๐Ÿ”’ ์ƒํ™ฉ

๊ณ„์ • ๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ๊นŒ๋จน์–ด ์ด๋ฉ”์ผ๋กœ ์ž„์‹œ๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ๋ฐœ๊ธ‰ ๋ฐ›์•˜๋‹ค 
ํ•˜์ง€๋งŒ ์•”ํ˜ธํ™”๋˜์–ด๋ฒ„๋ฆฐ ๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ๋ฐ›๊ฒŒ๋˜์—ˆ๋‹ค ๋˜ํ•œ DB์—๋Š” ๋ฐ˜๋Œ€๋กœ ์•”ํ˜ธํ™”๋˜์ง€ ์•Š์€ ๋น„๋ฐ€๋ฒˆํ˜ธ๊ฐ€ ์ €์žฅ๋˜์–ด์žˆ๋‹ค

 

https://gi-dor.tistory.com/245

 

์žƒ์–ด๋ฒ„๋ฆฐ ๋น„๋ฐ€๋ฒˆํ˜ธ ์ฐพ๊ธฐ + ์ด๋ฉ”์ผ๋กœ ์ž„์‹œ๋น„๋ฐ€๋ฒˆํ˜ธ ๋ฐ›๊ธฐ

DB์—๋Š” ์—ฌ๋Ÿฌ ์‚ฌ์šฉ์ž์˜ ๊ณ„์ •๊ณผ ๋น„๋ฐ€๋ฒˆํ˜ธ๊ฐ€ ์ €์žฅ๋˜์–ด์žˆ๋‹ค๋น„๋ฐ€๋ฒˆํ˜ธ ๋…ธ์ถœ๊ฐ™์€ ๋ฌธ์ œ ๋•Œ๋ฌธ์— DB ์—์„œ ์•”ํ˜ธํ™” ์ฒ˜๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•ด ์ €์žฅ๋˜์–ด์žˆ๋‹ค์ด๋ฒˆ ํ”„๋กœ์ ํŠธ ๋˜ํ•œ ์Šคํ”„๋ง์‹œํ๋ฆฌํ‹ฐ์— PasswordEncoder๋ฅผ ํ†ตํ•ด ๋น„

gi-dor.tistory.com

  // ์˜ˆ์™ธ ๋ฐœ์ƒํ•˜๋ฉด ๋กค๋ฐฑ๋˜์„œ ์ด์ „ ์ƒํƒœ๋กœ ๊ฐ€๊ฒŒํ•˜๋ ค๊ณ  - ACID
    @Transactional
    public String resetPassword(String id , String email) {

        User user = userMapper.selectUserByIdAndEmail(id, email);

        if (user == null) {
            throw new IllegalArgumentException("์ฃผ์–ด์ง„ ์ •๋ณด์— ํ•ด๋‹นํ•˜๋Š” ์‚ฌ์šฉ์ž ์ •๋ณด๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค.");
        } else if (id == null || id.isEmpty() || email == null || email.isEmpty()) {
            throw new IllegalArgumentException("์•„์ด๋””์™€ ์ด๋ฉ”์ผ์„ ๋ชจ๋‘ ์ž…๋ ฅํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.");
        }

        // ์ž„์‹œ ๋น„๋ฐ€๋ฒˆํ˜ธ ๋งŒ๋“ค๊ธฐ
        String resetPassword = RandomPassword.generatePassword(8,16);

        // ๋น„๋ฐ€๋ฒˆํ˜ธ ๋ณ€๊ฒฝ - ์•”ํ˜ธํ™” ์•ˆ๋˜์„œ DB์— ๊ทธ๋ƒฅ ๋น„๋ฐ€๋ฒˆํ˜ธ ์ €์žฅ๋˜๋ฒ„๋ฆผ
        userMapper.updateResetPassword(id,resetPassword);

        return resetPassword;
    }

 

๐Ÿ”’ ์›ํ•˜๋Š” ๊ฒฐ๊ณผ 

DB์—๋Š” ์•”ํ˜ธํ™”๋œ ๊ฐ’์ด ์ €์žฅ๋˜์–ด์•ผํ•˜๊ณ  ์ด๋ฉ”์ผ์—๋Š” ์•”ํ˜ธํ™” ์ฒ˜๋ฆฌํ•˜์ง€ ์•Š์€ ๊ฐ’์ด ๋ณด๋‚ด์ ธ์•ผํ•œ๋‹ค

 

๐Ÿ”’ ํ•ด๊ฒฐ๋ฐฉ๋ฒ•

1๏ธโƒฃ  service ์— ์Šคํ”„๋ง ์‹œํ๋ฆฌํ‹ฐ ์•”ํ˜ธํ™” ์ฒ˜๋ฆฌ์ธ passwordEncoder ์‚ฌ์šฉ

    // ์˜ˆ์™ธ ๋ฐœ์ƒํ•˜๋ฉด ๋กค๋ฐฑ๋˜์„œ ์ด์ „ ์ƒํƒœ๋กœ ๊ฐ€๊ฒŒํ•˜๋ ค๊ณ  - ACID
    @Transactional
    public String resetPassword(String id , String email) {

        User user = userMapper.selectUserByIdAndEmail(id, email);

        if (user == null) {
            throw new IllegalArgumentException("์ฃผ์–ด์ง„ ์ •๋ณด์— ํ•ด๋‹นํ•˜๋Š” ์‚ฌ์šฉ์ž ์ •๋ณด๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค.");
        } else if (id == null || id.isEmpty() || email == null || email.isEmpty()) {
            throw new IllegalArgumentException("์•„์ด๋””์™€ ์ด๋ฉ”์ผ์„ ๋ชจ๋‘ ์ž…๋ ฅํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.");
        }

        // ์ž„์‹œ ๋น„๋ฐ€๋ฒˆํ˜ธ ๋งŒ๋“ค๊ธฐ
        String resetPassword = RandomPassword.generatePassword(8,16);

        // ๋น„๋ฐ€๋ฒˆํ˜ธ ๋ณ€๊ฒฝ - ์•”ํ˜ธํ™” ์•ˆ๋˜์„œ DB์— ๊ทธ๋ƒฅ ๋น„๋ฐ€๋ฒˆํ˜ธ ์ €์žฅ๋˜๋ฒ„๋ฆผ
        userMapper.updateResetPassword(id,passwordEncoder.encode(resetPassword));

        return resetPassword;
    }

 

2๏ธโƒฃ  ์•”ํ˜ธํ™”ํ•˜์ง€ ์•Š์€ ๋‚œ์ˆ˜๋กœ ๋งŒ๋“  ์ž„์‹œ๋น„๋ฐ€๋ฒˆํ˜ธ htmlTemplate๋กœ ์ „๋‹ฌ

String email = email1 + "@" + email2;
User user = userService.selectUserByIdAndEmail(id,email);

 // ์ž„์‹œ๋น„๋ฐ€๋ฒˆํ˜ธ ๋งŒ๋“ค๊ธฐ +  DB์— update  
String resetPassword = userService.resetPassword(id,email);
// ์ž„์‹œ ๋น„๋ฐ€๋ฒˆํ˜ธ๊ฐ’ resetPassword
model.addAttribute("resetPassword",resetPassword);

// ์‚ฌ์šฉ์ž ์กฐํšŒํ•œ ๊ฐ’์œผ๋กœ eamil ๋ณด๋‚ด๊ธฐ
String to = user.getEmail();
String subject = "BookHub ์ž„์‹œ๋น„๋ฐ€๋ฒˆํ˜ธ ๋ฐœ๊ธ‰.";
String html = mailService.resetPasswordTemplate(user); 
mailService.sendEmail(to, subject, html);

 

String html = mailService.resetPasswordTemplate(resetPassword);

userServcie.resetPassword(id,email); ์— resetPassword๋Š” ์•”ํ˜ธํ™” ํ•˜์ง€ ์•Š์€ Random์œผ๋กœ ๋งŒ๋“  ๋ฌด์ž‘์œ„ ์ž„์‹œ๋น„๋ฐ€๋ฒˆํ˜ธ

 

public String resetPasswordTemplate(String password) throws Exception{

        ClassPathResource resource = new ClassPathResource("templates/user/mail/resetPassword.html");
        String htmlTemplate = null;
        try{
            htmlTemplate = StreamUtils.copyToString(resource.getInputStream(), StandardCharsets.UTF_8);

        } catch (IOException ex) {
            System.err.println("์ด๋ฉ”์ผ ํ…œํ”Œ๋ฆฟ์„ ๋กœ๋“œํ•˜๋Š” ๋„์ค‘ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ–ˆ์Šต๋‹ˆ๋‹ค.");
            ex.printStackTrace();
        }
        return htmlTemplate.replace("PASSWORD", password);
    }
 <tr>
        <td align="center" style="padding: 20px; font-size: 24px; font-weight: bold;">
            ์ž„์‹œ ๋น„๋ฐ€๋ฒˆํ˜ธ๊ฐ€ ๋ฐœ๊ธ‰ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.
        </td>
    </tr>
    <tr>
        <td align="center" style="padding: 10px; font-size: 32px; font-weight: bold; color: #333; background-color: #ffffff; border-radius: 5px; margin: 10px;">
            ์ž„์‹œ๋น„๋ฐ€๋ฒˆํ˜ธ : <span >PASSWORD</span>
        </td>

    </tr>
    <tr>
        <td align="center" style="font-size: 16px; padding: 30px;">
            <p>์ œ๊ณตํ•ด๋“œ๋ฆฐ ๋น„๋ฐ€๋ฒˆํ˜ธ๋Š” ์ž„์‹œ ๋น„๋ฐ€๋ฒˆํ˜ธ์ž…๋‹ˆ๋‹ค</p>
            <p>๋กœ๊ทธ์ธ ํ•˜์‹ ํ›„์— ๋งˆ์ด ํŽ˜์ด์ง€์—์„œ ๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ๋ณ€๊ฒฝํ•œ ๋’ค์— ์‚ฌ์ดํŠธ๋ฅผ ์ด์šฉํ•ด์ฃผ์„ธ์š”</p>
            <p>๋ฌธ์˜ ์‚ฌํ•ญ์ด ์žˆ์œผ์‹œ๋ฉด ์–ธ์ œ๋“ ์ง€ ์ €ํฌ์—๊ฒŒ ์—ฐ๋ฝ ์ฃผ์„ธ์š”.</p>
        </td>
    </tr>

 

 

๐Ÿ“Œ ๊ฒฐ๊ณผ

 

1๏ธโƒฃ ๋น„๋ฐ€๋ฒˆํ˜ธ ์ฐพ๊ธฐ์ „ 

 

2๏ธโƒฃ ์ž„์‹œ ๋น„๋ฐ€๋ฒˆํ˜ธ ๋ฐœ๊ธ‰ ์™„๋ฃŒ 

 

3๏ธโƒฃ ๋ฐœ๊ธ‰๋œ ๋น„๋ฐ€๋ฒˆํ˜ธ๋กœ ๋กœ๊ทธ์ธ

 

 

4๏ธโƒฃ ์ž„์‹œ๋น„๋ฐ€๋ฒˆํ˜ธ ์•”ํ˜ธํ™” DB์ €์žฅ ํ™•์ธ

728x90