๊ฐœ๋ฐœ์ผ์ง€-6 [๐Ÿ‘จโ€๐ŸณํŒ๋งค์ž ๋„๋ฉ”์ธ: ํšŒ์›๊ฐ€์ž… ์š”์ฒญ]

๊น€์„ฑ์ธยท2023๋…„ 9์›” 5์ผ
0

ํŒ๋งค์ž ๋„๋ฉ”์ธ ๊ธฐ๋Šฅ ์ •์˜ ํฌ์ŠคํŒ…


๋ณธ์ธ์ธ์ฆ ํ›„ ํšŒ์›๊ฐ€์ž… ํ™”๋ฉด


3. [POST] /jat/sellers

ํšŒ์›๊ฐ€์ž… ์ •๋ณด๋ฅผ ์ด์šฉํ•˜์—ฌ ํšŒ์›๊ฐ€์ž… ์™„๋ฃŒ

  • ๋ณธ์ธ์ธ์ฆ ํ›„ ์‚ฌ์šฉ์ž๊ฐ€ ์„ ํƒํ•œ ์•ฝ๊ด€๋™์˜, ์ด๋ฆ„, ์ƒ๋…„์›”์ผ, ์ „ํ™”๋ฒˆํ˜ธ, ์•„์ด๋””, ๋น„๋ฐ€๋ฒˆํ˜ธ, ์ด๋ฉ”์ผ์„ ๋ชจ๋‘ Body์— ๋‹ด์•„ ์š”์ฒญํ•œ ํ›„,
  • ๊ฐ€์ž…์ด ์™„๋ฃŒ๋˜๋ฉด ๊ฐ€์ž…์— ๋Œ€ํ•œ ์ •๋ณด๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋„๋ก ์„ค๊ณ„ํ•˜์˜€๋‹ค.
  • ํŽ˜์ด์ง€ ๋ณ„๋กœ ๊ฐ’์„ ๋„˜๊ธฐ๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ, ํ™”๋ฉด ์ „ํ™˜ํ•  ๋•Œ ๊ฐ’์„ ์œ ์ง€ํ•˜์—ฌ ๋‹ค์Œ ๋ทฐ๋กœ ๋„˜๊ฒจ์„œ ํ•œ๊บผ๋ฒˆ์— ์„œ๋ฒ„์— ์š”์ฒญํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ํ”„๋ก ํŠธ์™€ ํ˜‘์˜๋ฅผ ๋ณด์•˜๋‹ค.
  • ์•„์ด๋””๋Š” ๊ฐ€์ž…์‹œ์— ์ค‘๋ณต ํ™•์ธ์„ ๋ฐ˜๋“œ์‹œ ํ•˜๋„๋ก ์„ค์ •ํ•˜์˜€๊ณ ,
  • ์˜๋ฌธ์œผ๋กœ ๋ฐ˜๋“œ์‹œ ์‹œ์ž‘, ์ˆซ์ž์™€ ์กฐํ•ฉ๋œ 4~20์ž๋กœ ์ •๊ทœ์‹์„ ์„ค์ •ํ•˜์˜€๋‹ค.
  • ๋น„๋ฐ€๋ฒˆํ˜ธ๋Š” ์˜๋ฌธ+์ˆซ์ž+ํŠน์ˆ˜๊ธฐํ˜ธ 10์ž ์ด์ƒ 30์ž ์ดํ•˜ ์˜ ์ •๊ทœ์‹์„ ์„ค์ •ํ•˜์˜€๊ณ ,
  • ์„œ๋ฒ„์— ์ €์žฅํ•  ๋•Œ๋Š” ์•”ํ˜ธํ™”๋ฅผ ํ†ตํ•ด์„œ ์ €์žฅํ•˜์˜€๋‹ค. (SHA 256)
  • ์•„์ด๋””๋Š” ์ฐพ๊ธฐ๊ฐ€ ๊ฐ€๋Šฅํ•˜์ง€๋งŒ, ๋น„๋ฐ€๋ฒˆํ˜ธ๋Š” ์žฌ์„ค์ •์„ ํ•˜์—ฌ์•ผํ•œ๋‹ค.

(1) ๋กœ์ง ํ”Œ๋กœ์šฐ
1. ์œ ์ € ์•„์ด๋””/์ด๋ฆ„/์ƒ์ผ/์ „ํ™”๋ฒˆํ˜ธ/๋น„๋ฐ€๋ฒˆํ˜ธ/์ด๋ฉ”์ผ, ์„œ๋น„์Šค/๊ฐœ์ธ์ •๋ณด/sms/์ด๋ฉ”์ผ/์ „ํ™” ์ˆ˜์‹  ๋™์˜์— ๋Œ€ํ•œ ์ •๋ณด๋ฅผ ๋ฐ”๋””์— ๋‹ด์•„ POST์— ์š”์ฒญ๋ฐ›์Œ.
2. ์•„์ด๋””, ๋น„๋ฐ€๋ฒˆํ˜ธ, ์ด๋ฆ„์— ๋Œ€ํ•œ ์ •๊ทœ์‹ ํ™•์ธ, ์˜ฌ๋ฐ”๋ฅธ ์ƒ๋…„์›”์ผ/์ „ํ™”๋ฒˆํ˜ธ ์–‘์‹์ธ์ง€ ์ •๊ทœ์‹ ํ™•์ธ
3. ์ค‘๋ณต ์•„์ด๋”” ์ฒดํฌ
4. ๋น„๋ฐ€๋ฒˆํ˜ธ ์•”ํ˜ธํ™”(SHA256 ๋‹จ๋ฐฉํ–ฅ ์•”ํ˜ธํ™” ์•Œ๊ณ ๋ฆฌ์ฆ˜) -> ์•”ํ˜ธํ™” ๊ด€๋ จ ํฌ์ŠคํŒ…
5. ์œ ์ € ๋“ฑ๋ก ํ›„ PK ๋ฐ˜ํ™˜

  • ์š”์ฒญ ๋ฐ”๋”” [PostSignUpReq.java]
  • SellerController.java
  • ValidationRegx.java
public class ValidationRegex {

    // ์•„์ด๋”” ์ •๊ทœ์‹ - ์‹œ์ž‘์€ ์˜๋ฌธ์œผ๋กœ๋งŒ, '_'๋ฅผ ์ œ์™ธํ•œ ํŠน์ˆ˜๋ฌธ์ž ์•ˆ๋˜๋ฉฐ ์˜๋ฌธ, ์ˆซ์ž, '_'์œผ๋กœ๋งŒ ์ด๋ฃจ์–ด์ง„ 4 ~ 20์ž ์ดํ•˜
    public static boolean isRegexUid(String target) {
        String regex = "^[a-zA-Z]{1}[a-zA-Z0-9_]{3,19}$";
        Pattern pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
        Matcher matcher = pattern.matcher(target);
        return matcher.find();
    }
    // ๋น„๋ฐ€๋ฒˆํ˜ธ ์ •๊ทœ์‹
    public static boolean isRegexPassword(String target) {
        boolean result;
        //์ตœ์†Œ 8๊ธ€์ž, ์ตœ๋Œ€16๊ธ€์ž, ๋Œ€๋ฌธ์ž 1๊ฐœ, ์†Œ๋ฌธ์ž 1๊ฐœ, ์ˆซ์ž 1๊ฐœ, ํŠน์ˆ˜๋ฌธ์ž 1๊ฐœ
        //String regex = "^(?=.*[a-z])(?=.*[A-Z])(?=.*\\d)(?=.*[@$!%*?&])[A-Za-z\\d@$!%*?&]{8,16}$";

        //์ตœ๋Œ€ 30๊ธ€์ž, ์†Œ๋ฌธ์ž 1๊ฐœ, ์ˆซ์ž 1๊ฐœ, ํŠน์ˆ˜๋ฌธ์ž 1๊ฐœ : ์˜๋ฌธ + ์ˆซ์ž + ํŠน์ˆ˜๊ธฐํ˜ธ 8 ์ž์ด์ƒ
        String regex1 = "^(?=.*[a-z])(?=.*\\d)(?=.*[@$!%*?&])[A-Za-z\\d@$!%*?&]{8,30}$";

        //์ตœ๋Œ€ 30๊ธ€์ž : ์˜๋ฌธ + ์ˆซ์ž 10์ž ์ด์ƒ
        String regex2 = "^(?=.*[a-zA-z])(?=.*\\d)(?=.*[@$!%*?&])[A-Za-z\\d@$!%*?&]{10,30}$";

        Pattern pattern1 = Pattern.compile(regex1, Pattern.CASE_INSENSITIVE);
        Pattern pattern2 = Pattern.compile(regex2, Pattern.CASE_INSENSITIVE);

        Matcher matcher1 = pattern1.matcher(target);
        Matcher matcher2 = pattern1.matcher(target);

        //result=target.matches(regex1) || target.matches(regex2);
        result = matcher1.find() || matcher2.find();
        return result;
    }

    // ์ƒ๋…„์›”์ผ ์ •๊ทœ์‹ yyyy.mm.dd
    public static boolean isRegexBirth(String target){
        String regex = "^\\d{4}\\.\\d{2}\\.\\d{2}$";
        Pattern pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
        Matcher matcher = pattern.matcher(target);

        return matcher.find();
    }

	// ํ•ธ๋“œํฐ ๋ฒˆํ˜ธ ์ •๊ทœ์‹ (์ˆซ์ž๋กœ๋งŒ ์ด๋ฃจ์–ด์ง„ ์ „ํ™”๋ฒˆํ˜ธ)
    public static boolean isRegexPhone(String target){
        String regex = "^\\d{3}\\d{4}\\d{4}$";
        Pattern pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
        Matcher matcher = pattern.matcher(target);

        return matcher.find();
    }
}
  • SellerService.java
@Transactional(rollbackFor = BaseException.class)
    public PostSignUpRes signUp(PostSignUpReq postSignUpReq) throws BaseException {
        String salt;
        // ์•„์ด๋””, ๋น„๋ฐ€๋ฒˆํ˜ธ, ๋‹‰๋„ค์ž„ ์ •๊ทœ์‹ ์ฒ˜๋ฆฌ
        if(postSignUpReq.getUid().length() == 0 || postSignUpReq.getPassword().length() == 0 || postSignUpReq.getName().length() == 0){
            throw new BaseException(REQUEST_ERROR); // 2000 : ์ž…๋ ฅ๊ฐ’ ์ „์ฒด ๋นˆ ๊ฐ’์ผ๋•Œ
        }
        if(!isRegexUid(postSignUpReq.getUid())){
            throw new BaseException(POST_USERS_INVALID_UID); // 2010 : ์•„์ด๋”” ์ •๊ทœ ํ‘œํ˜„์‹ ์˜ˆ์™ธ
        }
        if(!isRegexPassword(postSignUpReq.getPassword())){
            throw new BaseException(POST_USERS_INVALID_PASSWORD); // 2011 : ๋น„๋ฐ€๋ฒˆํ˜ธ ์ •๊ทœ ํ‘œํ˜„์‹ ์˜ˆ์™ธ
        }
        // ์˜ฌ๋ฐ”๋ฅธ ์ƒ๋…„์›”์ผ ์–‘์‹์ธ์ง€?
        if(!isRegexBirth(postSignUpReq.getBirthday())){
            throw new BaseException(POST_USERS_INVALID_BIRTHDAY);
        }
        if(!isRegexPhone(postSignUpReq.getPhone())){
            throw new BaseException(POST_USERS_INVALID_PHONENUM);
        }
        // ์ค‘๋ณต ์•„์ด๋”” ์ฒดํฌ
        if(sellerDao.checkUid(postSignUpReq.getUid()) == 1){
            throw new BaseException(POST_USERS_EXISTS_ID); // 2018 : ์ค‘๋ณต ์•„์ด๋””
        }


        // ๋น„๋ฐ€๋ฒˆํ˜ธ ์•”ํ˜ธํ™”
        try{
            salt = SHA256.createSalt(postSignUpReq.getPassword()); // ๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ์ด์šฉํ•˜์—ฌ salt ์ƒ์„ฑ
            String pwd = new SHA256().encrypt(postSignUpReq.getPassword(), salt); // ๋น„๋ฐ€๋ฒˆํ˜ธ ์•”ํ˜ธํ™”
            postSignUpReq.setPassword(pwd);
        } catch (Exception ignored) {
            throw new BaseException(PASSWORD_ENCRYPTION_ERROR); // 4011 : ๋น„๋ฐ€๋ฒˆํ˜ธ ์•”ํ˜ธํ™”์— ์‹คํŒจํ•˜์˜€์Šต๋‹ˆ๋‹ค
        }
        try{
            // ์œ ์ € ๊ณ ์œ ์‹๋ณ„๋ฒˆํ˜ธ
            int sellerIdx = sellerDao.signUp(postSignUpReq, salt);

            PostSignUpRes postSignUpres = sellerDao.signUpComplete(sellerIdx);
            postSignUpres.setCompleteDate(postSignUpres.getCompleteDate().substring(0,10));

            return postSignUpres;
        } catch (Exception exception) {
            throw new BaseException(SIGNUP_FAILED);
        }
    }
  1. ํšŒ์›๊ฐ€์ž… ์š”์ฒญ์—์„œ ์•„์ด๋””, ๋น„๋ฐ€๋ฒˆํ˜ธ, ์ด๋ฆ„์˜ ๊ธธ์ด๊ฐ€ ๋นˆ ๋ฌธ์ž์—ด(๊ธธ์ด๊ฐ€ 0 ์ธ)์ธ์ง€ ํ™•์ธ
  2. ์•„์ด๋””๊ฐ€ ์‹œ์ž‘์€ ์˜๋ฌธ์œผ๋กœ๋งŒ, ''๋ฅผ ์ œ์™ธํ•œ ํŠน์ˆ˜๋ฌธ์ž ์•ˆ๋˜๋ฉฐ ์˜๋ฌธ, ์ˆซ์ž, ''์œผ๋กœ๋งŒ ์ด๋ฃจ์–ด์ง„ 4 ~ 20์ž ์ดํ•˜ ๋ฅผ ๋งŒ์กฑํ•˜๋Š”์ง€ ์ •๊ทœ์‹ ํ™•์ธ
  3. ๋น„๋ฐ€๋ฒˆํ˜ธ๊ฐ€ ์ตœ๋Œ€ 30๊ธ€์ž, ์†Œ๋ฌธ์ž 1๊ฐœ, ์ˆซ์ž 1๊ฐœ, ํŠน์ˆ˜๋ฌธ์ž 1๊ฐœ : ์˜๋ฌธ + ์ˆซ์ž + ํŠน์ˆ˜๊ธฐํ˜ธ 8 ์ž์ด์ƒ ๋˜๋Š” ์˜๋ฌธ + ์ˆซ์ž 10์ž ์ด์ƒ ๋ฅผ ๋งŒ์กฑํ•˜๋Š”์ง€ ์ •๊ทœ์‹ ํ™•์ธ
  4. ์˜ฌ๋ฐ”๋ฅธ ์ƒ๋…„์›”์ผ ์–‘์‹ (yyyy.mm.dd) ์„ ๋งŒ์กฑํ•˜๋Š”์ง€ ์ •๊ทœ์‹ ํ™•์ธ
  5. ์˜ฌ๋ฐ”๋ฅธ ํ•ธ๋“œํฐ ๋ฒˆํ˜ธ ์–‘์‹ (์ˆซ์ž๋กœ๋งŒ ์ด๋ค„์ง„ 10๊ธ€์ž ํœด๋Œ€๋ฒˆํ˜ธ)๋ฅผ ๋งŒ์กฑํ•˜๋Š”์ง€ ์ •๊ทœ์‹ ํ™•์ธ
  6. ์•„์ด๋””๊ฐ€ ์ค‘๋ณต๋˜๋Š”์ง€ DB๋ฅผ ์กฐํšŒํ•˜์—ฌ ํ™•์ธ -> ์•„์ด๋”” ์ค‘๋ณต์‹œ ์˜ˆ์™ธ ์ฒ˜๋ฆฌ
  7. ๋น„๋ฐ€๋ฒˆํ˜ธ ์•”ํ˜ธํ™” (SHA 256)์„ ์ด์šฉํ•˜์—ฌ ์ž…๋ ฅ๋ฐ›์€ ๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ๋ณ€๊ฒฝ -> ๋น„๋ฐ€๋ฒˆํ˜ธ ์•”ํ˜ธํ™” ํฌ์ŠคํŒ…
  8. ์•”ํ˜ธํ™” ์„ฑ๊ณต ํ›„, ์œ ์ € ํšŒ์›๊ฐ€์ž… ์ •๋ณด๋ฅผ DB์— ์ €์žฅ, ์œ ์ € PK DB์—์„œ ํ™•์ธ
  9. ๊ฐ’ ์‚ฝ์ž… ํ›„ ์ƒ์„ฑ๋œ PK๋กœ ๋‹ค์‹œ ์กฐํšŒํ•˜์—ฌ ํšŒ์›์ •๋ณด๋ฅผ ๋ฐ˜ํ™˜ํ•  ์ค€๋น„
  10. ์œ ์ €์—๊ฒŒ ๋ณด์—ฌ์งˆ ํšŒ์› ๊ฐ€์ž… ์ •๋ณด๋ฅผ Response

SellerDao.java

  • ์•„์ด๋”” ์ค‘๋ณต ์ฒดํฌ
    - ์œ ํšจํ•œ ํŒ๋งค์ž ํšŒ์› ์ •๋ณด ์ค‘ (status = 'A') ์•„์ด๋””๊ฐ€ ์ž…๋ ฅ๋œ ๊ฒƒ์ด ์กด์žฌํ•˜๋Š”์ง€ ํ™•์ธ.
public int checkUid(String uid) {
        String query = "SELECT EXISTS(SELECT sellerIdx FROM Merchandisers WHERE uid = ? and status = 'A')";
        return this.jdbcTemplate.queryForObject(query, int.class, uid);
    }
  • ํšŒ์› ์ •๋ณด Merchandisers(ํŒ๋งค์ž) ํ…Œ์ด๋ธ”์— ์‚ฝ์ž…
public int signUp(PostSignUpReq postSignUpReq, String salt){
        String query = "INSERT INTO Merchandisers(name, birthday, phone, uid, salt, password, email, first_login, service_check, personal_check, sms_check, email_check, call_check, role)\n" +
                "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);";
        Object[] params = new Object[]{
                postSignUpReq.getName(),
                postSignUpReq.getBirthday(),
                postSignUpReq.getPhone(),
                postSignUpReq.getUid(),
                salt, // ๋น„๋ฐ€๋ฒˆํ˜ธ ์•”ํ˜ธํ™”์— ์‚ฌ์šฉ๋˜๋Š” ์ถ”๊ฐ€ ๋ฌธ์ž์—ด
                postSignUpReq.getPassword(),
                postSignUpReq.getEmail(),
                1, // ์ตœ์ดˆ ๋กœ๊ทธ์ธ ์—ฌ๋ถ€ (1=์ตœ์ดˆ ๋กœ๊ทธ์ธ, 0=ํ•œ๋ฒˆ ์ด์ƒ์˜ ๋กœ๊ทธ์ธ)
                postSignUpReq.getServiceCheck(),
                postSignUpReq.getPersonalCheck(),
                postSignUpReq.getSmsCheck(),
                postSignUpReq.getEmailCheck(),
                postSignUpReq.getCallCheck(),
                "ROLE_SELLER" // Security ์ธ๊ฐ€์— ์‚ฌ์šฉ๋˜๋Š” ์—ญํ• 
        };

        this.jdbcTemplate.update(query, params);

        String lastInsertIdQuery = "select last_insert_id()";
        return this.jdbcTemplate.queryForObject(lastInsertIdQuery,int.class);
    }

4. [POST] /jat/sellers/login

๊ฐ€์ž…ํ•œ ์•„์ด๋””/๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ์ด์šฉํ•œ ์„œ๋น„์Šค ๋กœ๊ทธ์ธ

  • ํšŒ์›๊ฐ€์ž… ์‹œ์— ๋ณด๋ƒˆ๋˜ ์•„์ด๋””, ๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ์„œ๋ฒ„์— ์š”์ฒญํ•˜๋ฉด,
  • ์„œ๋ฒ„์—์„œ ๋กœ์ง ์ฒ˜๋ฆฌ ํ›„ ํ•ด๋‹น ๊ฐ€์ž…์ •๋ณด์˜ ๊ธฐ๋ณธ ๊ฐ’๋“ค๊ณผ JWT ์ธ๊ฐ€ ํ† ํฐ์„ ๋ฐœํ–‰ํ•˜์—ฌ ํด๋ผ์ด์–ธํŠธ์—๊ฒŒ Response
  • ๋ฐœํ–‰๋œ ์ด JWT ํ† ํฐ์€ ๋กœ๊ทธ์ธ ํ›„์— ์š”์ฒญํ•˜๋Š” ๋ชจ๋“  API์˜ ํ—ค๋”์— ๋‹ด์•„์„œ ๋ณด๋‚ด์•ผ ํ•œ๋‹ค.

(1) ๋กœ์ง ํ”Œ๋กœ์šฐ
1. ๊ฐ€์ž…๋œ ์•„์ด๋””/ ๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ๋ฐ”๋””์— ๋‹ด์•„์„œ POST๋กœ ์š”์ฒญ์„ ๋ณด๋‚ธ๋‹ค.
2. ์•„์ด๋””, ๋น„๋ฐ€๋ฒˆํ˜ธ์˜ ๊ธธ์ด๊ฐ€ 0 (๋นˆ ๊ฐ’)์œผ๋กœ ๋ณด๋‚ด์กŒ๋Š”์ง€ ํ™•์ธ
3. ์•„์ด๋””๋ฅผ ๊ฐ€์ง€๊ณ  ๊ฐ€์ž…๋œ ์ •๋ณด๋ฅผ ๋ชจ๋‘ DB์—์„œ ๊ฐ€์ ธ์˜ด
4. ๊ฐ€์ž…๋œ ์ •๋ณด๊ฐ€ ์กด์žฌํ•œ๋‹ค๋ฉด, ํ•ด๋‹น ํŒ๋งค์ž ๊ณ„์ •์— ๊ฐ€๊ฒŒ ์„œ๋น„์Šค๊ฐ€ ๋“ฑ๋ก๋˜์–ด ์žˆ๋Š”์ง€ ํ™•์ธ
5. ๊ฐ€๊ฒŒ ์„œ๋น„์Šค -> ๋ณธ์ธ์˜ ํŒ๋งค์  ๋“ฑ๋ก, ํŒ๋งค์ ์˜ ๋ฉ”๋‰ด ๋“ฑ๋ก ์ด ๋ชจ๋‘ ์™„๋ฃŒ๋˜์—ˆ๋Š”์ง€ ํ™•์ธ ํ•˜์—ฌ ํ”„๋ก ํŠธ์—์„œ ๋ณด์—ฌ์ค„ ํ™”๋ฉด์„ ์‹๋ณ„ํ•  ์ˆ˜ ์žˆ๋Š” ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
6. DB์— ์กด์žฌํ•˜๋Š” ์•”ํ˜ธํ™”๋œ ๋น„๋ฐ€๋ฒˆํ˜ธ์™€ ์ž…๋ ฅ๋œ ๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ์•”ํ˜ธํ™”ํ•˜์—ฌ ๋น„๊ตํ–ˆ์„ ๋•Œ ์ผ์น˜ํ•œ๋‹ค๋ฉด, ๋กœ๊ทธ์ธ ์„ฑ๊ณต
7. ๋กœ๊ทธ์ธ ์„ฑ๊ณต ์‹œ ์œ ์ €๊ฐ€ ์„œ๋น„์Šค๋ฅผ ์ด์šฉํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉํ•  JWTํ† ํฐ์„ ๋ฐœํ–‰ํ•˜์—ฌ Response

-> JWT๊ด€๋ จ ํฌ์ŠคํŒ…
-> ์•”ํ˜ธํ™” ๊ด€๋ จ ํฌ์ŠคํŒ…

  • ์š”์ฒญ ๋ฐ”๋”” [PostSignUpReq.java]
  • SellerController.java
  • SellerService.java
 @Transactional(rollbackFor = BaseException.class)
    public PostLoginRes login(PostLoginReq postLoginReq) throws BaseException{
        if(postLoginReq.getUid().length() == 0 || postLoginReq.getPassword().length() == 0){
            throw new BaseException(REQUEST_ERROR); // 2000 : ์ž…๋ ฅ๊ฐ’ ์ „์ฒด ๋นˆ ๊ฐ’์ผ๋•Œ
        }

        // 1) ์•„์ด๋””๊ฐ€ ์กด์žฌํ•˜๋Š”์ง€ ํ™•์ธ, ํšŒ์›์ •๋ณด ์šฐ์„  ์กฐํšŒ
        Seller seller;
        try{
            seller = sellerDao.login(postLoginReq);
        }catch(Exception exception){
            throw new BaseException(FAILED_TO_LOGIN);
        }

        // 1-2) ์ „ ๊ฐ€๊ฒŒ ๋“ฑ๋ก ์—ฌ๋ถ€ ์กด์žฌํ™•์ธ
        int storeRegistered = 0;
        try{
            storeRegistered = storeDao.storeIdxBySellerIdxExists(seller.getSellerIdx());
        }catch(Exception exception){
            throw new BaseException(FAILED_TO_LOGIN);
        }

        // 2) ํšŒ์›๊ฐ€์ž…ํ›„ ๊ฐ€๊ฒŒ์Šน์ธ ๋ฐ ๋ฉ”๋‰ด ๋“ฑ๋ก ์—ฌ๋ถ€ ํ™•์ธ
        StoreNameNStatus storeNameStatus = null;
        try{
            if (storeRegistered == 1){
                // storeName from storeDao
                storeNameStatus = storeDao.storeNameBySellerIdx(seller.getSellerIdx());
            }
        }catch(Exception exception){
            throw new BaseException(FAILED_TO_LOGIN);
        }

        // 3) ๋น„๋ฐ€ ๋ฒˆํ˜ธ ์•”ํ˜ธํ™”
        try{
            String salt = seller.getSalt();
            String pwd = new SHA256().encrypt(postLoginReq.getPassword(), salt);
            if (postLoginReq.getUid().equals(seller.getUid()) && pwd.equals(seller.getPassword())){
                String jwt = jwtTokenProvider.createJwt(seller.getSellerIdx(), "Merchandiser");
                return new PostLoginRes(jwt,
                        seller.getSellerIdx(),
                        seller.getName(),
                        seller.getFirst_login(),
                        seller.getMenu_register(),
                        storeNameStatus != null ? storeNameStatus.getStore_name() : "", // null ์ด ์•„๋‹Œ๊ฒƒ์€ ๊ฐ€๊ฒŒ๋“ฑ๋ก์„ ๋ฌด์กฐ๊ฑด ํ–ˆ๋‹ค๋Š”๊ฒƒ.
                        storeNameStatus != null ? storeNameStatus.getStore_status() : null); // null
            }
            else{
                throw new BaseException(FAILED_TO_LOGIN);
            }
        }catch(Exception exception){
            throw new BaseException(FAILED_TO_LOGIN);
        }
    }
  • 3) ๋น„๋ฐ€ ๋ฒˆํ˜ธ ์•”ํ˜ธํ™” ๋ถ€๋ถ„์—์„œ jwt๋ฅผ ๋งŒ๋“œ๋Š” ํ•จ์ˆ˜์— ํŒ๋งค์ž PK๊ฐ’๊ณผ "Merchandiser"์ด๋ผ๋Š” ๋งค๊ฐœ๋ณ€์ˆ˜๊ฐ€ ๋“ค์–ด๊ฐ„๋‹ค.
  • ์ด๋Š” ํŒ๋งค์ž์™€ ๊ตฌ๋งค์ž์˜ DB ํ…Œ์ด๋ธ”์ด ๋‹ฌ๋ผ์„œ SpringSecurit Filter์—์„œ ํ—ค๋”๋‚ด์— ์กด์žฌํ•˜๋Š” jwtํ† ํฐ ๊ฐ’์„ ์ด์šฉํ•ด userIdx ๊ธฐ๋ณธํ‚ค๋กœ ์กฐํšŒ๋ฅผ ํ• ๋•Œ ์–ด๋–ค ํ…Œ์ด๋ธ”์„ ์ฐธ์กฐํ• ์ง€,
  • ์ฆ‰ ์œ ์ € ์กฐํšŒ๋ฅผ ๊ตฌ๋งค์ž ํ…Œ์ด๋ธ”, ํŒ๋งค์ž ํ…Œ์ด๋ธ” ๋‘˜ ์ค‘ ์–ด๋””์„œ ํ• ์ง€ ๊ตฌ๋ณ„ํ•˜๊ธฐ ์œ„ํ•œ ๊ฐ’์ด๋‹ค.
  • ๊ตฌ๋งค์ž ๊ฐ™์€๊ฒฝ์šฐ ํ•ด๋‹น ๋ถ€๋ถ„์— "Customer"์ด๋ผ๊ณ  ๋งค๊ฐœ๋ณ€์ˆ˜๊ฐ€ ๋“ค์–ต๋‚˜๋‹ค.
  • SellerDao.java
// ID๋ฅผ ์ด์šฉํ•œ ๋กœ๊ทธ์ธ ์ •๋ณด ๋ฐ˜ํ™˜
public Seller login(PostLoginReq postLoginReq) {
        String query = "SELECT * FROM Merchandisers WHERE uid = ?";
        return this.jdbcTemplate.queryForObject(query,
                (rs, rowNum) -> new Seller(
                        rs.getInt("sellerIdx"),
                        rs.getString("name"),
                        rs.getString("birthday"),
                        rs.getString("phone"),
                        rs.getString("uid"),
                        rs.getString("salt"),
                        rs.getString("password"),
                        rs.getString("email"),
                        rs.getInt("first_login"),
                        rs.getInt("menu_register"),
                        null
                ), postLoginReq.getUid());
    }
// ๊ฐ€๊ฒŒ๋“ฑ๋ก ์—ฌ๋ถ€ ํ™•์ธ
public int storeIdxBySellerIdxExists(int sellerIdx){
        String query = "SELECT EXISTS(SELECT storeIdx FROM Stores WHERE sellerIdx = ?)";

        return this.jdbcTemplate.queryForObject(query, int.class, sellerIdx);
}
// ๊ฐ€๊ฒŒ๋“ฑ๋ก ํ–ˆ๋‹ค๋ฉด ํ˜„์žฌ ์ƒํƒœ ๋ฐ˜ํ™˜(๊ด€๋ฆฌ์ž ์Šน์ธ ๊ธฐ๋‹ค๋ฆฌ๊ณ ์žˆ๋Š”์ง€?, ๋ฉ”๋‰ด ๋“ฑ๋กํ–ˆ๋Š”์ง€?)
public StoreNameNStatus storeNameBySellerIdx(int sellerIdx){
        String query = "SELECT store_name, status FROM Stores WHERE sellerIdx = ?";


        return this.jdbcTemplate.queryForObject(query,
                (rs, rowNum) -> new StoreNameNStatus(
                        rs.getString("store_name"),
                        rs.getString("status")
                ), sellerIdx);
}
profile
๊ฐœ๋ฐœ์ž๊ฐ€ ๊ฟˆ์ธ 25์‚ด ๋Œ€ํ•™์ƒ์ž…๋‹ˆ๋‹ค.

0๊ฐœ์˜ ๋Œ“๊ธ€