Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/bundle #17

Closed
wants to merge 10 commits into from
Closed

Feature/bundle #17

wants to merge 10 commits into from

Conversation

kitechoi
Copy link
Collaborator

@kitechoi kitechoi commented Feb 7, 2025

Pull Request

๐Ÿ’ก PR ์š”์•ฝ

์ด PR์—์„œ ์ž‘์—…ํ•œ ๋‚ด์šฉ์„ ๊ฐ„๋‹จํžˆ ์„ค๋ช…ํ•ด์ฃผ์„ธ์š”

๋ณด๋”ฐ๋ฆฌ ์ตœ์ดˆ ์ƒ์„ฑ API๋ฅผ ๊ฐœ๋ฐœํ•˜์˜€์Šต๋‹ˆ๋‹ค.

JWT ๊ธฐ๋ฐ˜ ์‚ฌ์šฉ์ž ์ธ์ฆ์„ ์ถ”๊ฐ€ํ•˜์—ฌ, ํ˜„์žฌ ๋กœ๊ทธ์ธํ•œ ์‚ฌ์šฉ์ž ์ •๋ณด๋ฅผ ํ™œ์šฉํ•˜๋„๋ก ํ•˜์˜€์Šต๋‹ˆ๋‹ค.

์ตœ์ดˆ ์ƒ์„ฑ๋œ ๋ณด๋”ฐ๋ฆฌ์—๋Š” ๋ณด๋”ฐ๋ฆฌ๋ช… ์™ธ์—๋„ ์„ ๋ฌผ๊ฐ์ฒด๋“ค, ์„ ๋ฌผ์ด๋ฏธ์ง€ ๊ด€๋ จ ๋ฐ์ดํ„ฐ๋“ค์ด ํฌํ•จ๋ฉ๋‹ˆ๋‹ค.

๐Ÿ” ์ฃผ์š” ๋ณ€๊ฒฝ์‚ฌํ•ญ

๊ตฌ์ฒด์ ์ธ ๋ณ€๊ฒฝ ๋‚ด์šฉ์„ ์„ค๋ช…ํ•ด์ฃผ์„ธ์š”

  • POST /api/v1/bundles ๋ณด๋”ฐ๋ฆฌ ์ตœ์ดˆ ์ƒ์„ฑ API ๊ตฌํ˜„
  • UserService.getCurrentActiveUser() ๋ฉ”์„œ๋“œ ์ ‘๊ทผ ์ œํ•œ์ž ๋ณ€๊ฒฝ (private โ†’ public)
  • BundleService.createBundle() JWT ๊ธฐ๋ฐ˜ ์‚ฌ์šฉ์ž ์ธ์ฆ์„ ํ†ตํ•ด ์œ ์ € ID ์ €์žฅ
  • BundleServiceTest ํ…Œ์ŠคํŠธ ์ฝ”๋“œ ์ž‘์„ฑ ([@mock](https://github.com/mock)์„ ํ™œ์šฉํ•˜์—ฌ ์‚ฌ์šฉ์ž ์ธ์ฆ Mocking)
  • ํ…Œ์ŠคํŠธ์šฉ JWT ํ† ํฐ ์ƒ์„ฑ ์œ ํ‹ธ๋ฆฌํ‹ฐ (TestJwtTokenProvider) ์ถ”๊ฐ€
  • ์„ ๋ฌผ ์—”ํ‹ฐํ‹ฐ, dto ๋“ฑ ๊ธฐ๋ณธ ๊ตฌ์กฐ
  • ์„ ๋ฌผ ์ด๋ฏธ์ง€ ์—”ํ‹ฐํ‹ฐ, dto ๋“ฑ ๊ธฐ๋ณธ ๊ตฌ์กฐ

๐Ÿ”— ์—ฐ๊ด€๋œ ์ด์Šˆ

๊ด€๋ จ ์ด์Šˆ๋ฅผ ๋งํฌํ•ด์ฃผ์„ธ์š” (์˜ˆ: #์ด์Šˆ๋ฒˆํ˜ธ)

๐Ÿ“ธ ์Šคํฌ๋ฆฐ์ƒท (์„ ํƒ)

UI ๋ณ€๊ฒฝ์‚ฌํ•ญ์ด ์žˆ๋‹ค๋ฉด ์Šคํฌ๋ฆฐ์ƒท์„ ์ฒจ๋ถ€ํ•ด์ฃผ์„ธ์š”

โœ… ์ฒดํฌ๋ฆฌ์ŠคํŠธ

  • [๐Ÿ”บ] ํ…Œ์ŠคํŠธ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜์˜€๋‚˜์š”? -> ์„ ๋ฌผ๊ฐ์ฒด๋“ค, ์„ ๋ฌผ์ด๋ฏธ์ง€ ๊ด€๋ จ ๋ฐ์ดํ„ฐ๋“ค ํฌํ•จ ์ „ ํ…Œ์ŠคํŠธ ์ฝ”๋“œ๋งŒ ์ž‘์„ฑ๋จ
  • ๊ด€๋ จ ๋ฌธ์„œ๋ฅผ ์—…๋ฐ์ดํŠธํ•˜์˜€๋‚˜์š”?
  • Breaking Change๊ฐ€ ์žˆ๋‚˜์š”?
  • ์ฝ”๋“œ ํฌ๋งทํŒ…๊ณผ ๋ฆฐํŠธ ๊ฒ€์‚ฌ๋ฅผ ์™„๋ฃŒํ•˜์˜€๋‚˜์š”?

๐Ÿ™ ๋ฆฌ๋ทฐ์–ด ์ฐธ๊ณ ์‚ฌํ•ญ

๋ฆฌ๋ทฐ์–ด๊ฐ€ ํŠน๋ณ„ํžˆ ๋ด์ฃผ์—ˆ์œผ๋ฉด ํ•˜๋Š” ๋ถ€๋ถ„์ด ์žˆ๋‹ค๋ฉด ์ž‘์„ฑํ•ด์ฃผ์„ธ์š”

  • UserService.getCurrentActiveUser() ๋ฉ”์„œ๋“œ๋ฅผ public์œผ๋กœ ๋ณ€๊ฒฝํ•˜์—ฌ ํ˜„์žฌ ๋กœ๊ทธ์ธํ•œ ์‚ฌ์šฉ์ž๋ฅผ ๊ฐ€์ ธ์˜ค๋Š” ๋ฐฉ์‹์„ ์ ์šฉํ–ˆ์Šต๋‹ˆ๋‹ค.์ด ๋ฐฉ์‹์ด ์ ์ ˆํ•œ์ง€, ๋” ์ข‹์€ ๋Œ€์•ˆ์ด ์žˆ๋Š”์ง€ ๋ฆฌ๋ทฐ ์š”์ฒญ๋“œ๋ฆฝ๋‹ˆ๋‹ค.
  • API ์š”์ฒญ ์‹œ JWT ์ธ์ฆ์„ ๊ธฐ๋ณธ์ ์œผ๋กœ ์š”๊ตฌํ•˜๋„๋ก ์„ค์ •ํ–ˆ๋Š”๋ฐ, ๊ทธ๋ž˜์„œ ์Šค์›จ๊ฑฐ์—์„œ๋Š” ํ…Œ์ŠคํŠธ๊ฐ€ ๋ถˆ๊ฐ€ํ•˜๋ฉฐ ๋‹จ์œ„ ํ…Œ์ŠคํŠธ๋งŒ ํ†ต๊ณผํ•œ ์ƒํ™ฉ์ž…๋‹ˆ๋‹ค.
  • ๋ณด๋”ฐ๋ฆฌ์ƒ์„ฑ ๋‹จ์œ„Test์—์„œ jwt ํ† ํฐ๋„ ๋ฐœ๊ธ‰๋ฐ›๊ณ  ์Šค์›จ๊ฑฐ์—์„œ ๊ทธ ํ† ํฐ์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๊ธธ ๋ฐ”๋ž๋Š”๋ฐ, 403 ์—๋Ÿฌ๊ฐ€ ๋œจ๋Š” ๊ฑธ ๋ณด๋‹ˆ ์•ˆ ๋˜๋‚˜ ๋ด…๋‹ˆ๋‹ค. ์•ž์œผ๋กœ ์–ด๋–ป๊ฒŒ ํ…Œ์ŠคํŠธํ•˜๋ฉด ์ข‹์„์ง€ ๊ณ ๋ฏผ์ด๋„ค์š”
  • BundleService์˜ BundleResponse createBundle ์–ด๋–ค ์‘๋‹ต์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ๊ฒŒ ์ ํ•ฉํ• ์ง€. ํ˜„์žฌ๋Š” (๋ณด๋”ฐ๋ฆฌ + ์„ ๋ฌผ + ์ด๋ฏธ์ง€)๋ผ๋Š” ๋ชจ๋“  ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ˜ํ™˜ ์ค‘.

๐Ÿ“‹ ์ถ”๊ฐ€ ์ปจํ…์ŠคํŠธ (์„ ํƒ)

PR์— ๋Œ€ํ•œ ์ถ”๊ฐ€์ ์ธ ์„ค๋ช…์ด๋‚˜ ์ปจํ…์ŠคํŠธ๊ฐ€ ์žˆ๋‹ค๋ฉด ์ž‘์„ฑํ•ด์ฃผ์„ธ์š”

@TaskerJang
Copy link
Collaborator

TaskerJang commented Feb 8, 2025

  1. getCurrentActiveUser() ๋ฉ”์„œ๋“œ ๊ด€๋ จ

ํ˜„์žฌ ๋ฐฉ์‹์€ ์ž‘๋™์€ ํ•˜์ง€๋งŒ, ์ธ์ฆ ๊ด€๋ จ ์ฑ…์ž„์„ ๋ถ„๋ฆฌํ•˜๋Š” ๊ฒƒ์ด ๋” ์ข‹์Šต๋‹ˆ๋‹ค

@Service
@RequiredArgsConstructor
public class AuthenticationService {
    
    /**
     * ํ˜„์žฌ ์ธ์ฆ๋œ ์‚ฌ์šฉ์ž ์ •๋ณด๋ฅผ ๊ฐ€์ ธ์˜ค๋Š” ๋ฉ”์„œ๋“œ
     */
    public User getCurrentUser() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if (authentication == null || !authentication.isAuthenticated()) {
            throw new UnauthorizedException("์ธ์ฆ๋˜์ง€ ์•Š์€ ์‚ฌ์šฉ์ž์ž…๋‹ˆ๋‹ค.");
        }
        
        String userId = authentication.getName();
        return userRepository.findById(Long.valueOf(userId))
            .orElseThrow(() -> new UserNotFoundException("์‚ฌ์šฉ์ž๋ฅผ ์ฐพ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค."));
    }
}

// ์‚ฌ์šฉ ์˜ˆ์‹œ
@Service
@RequiredArgsConstructor
public class BundleService {
    private final AuthenticationService authenticationService;
    
    public BundleResponse createBundle(BundleRequest request) {
        User currentUser = authenticationService.getCurrentUser();
        // ์ด๋Ÿฐ์‹์œผ๋กœ ์จ๋„ ๊ดœ์ฐฎ์„ ๊ฑฐ ๊ฐ™์Šต๋‹ˆ๋‹ค.
    }
}
  1. ํ…Œ์ŠคํŠธ ๊ด€๋ จ

TestJwtTokenProvider๋ฅผ ํ™œ์šฉํ•ด์„œ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๊ฐœ๋ฐœ ํ™˜๊ฒฝ์šฉ ํ† ํฐ ๋ฐœ๊ธ‰ API๋ฅผ ๊ตฌํ˜„ํ•˜๋ฉด ๋ ๊ฑฐ ๊ฐ™์Šต๋‹ˆ๋‹ค.

@Profile("dev")  // ๊ฐœ๋ฐœ ํ™˜๊ฒฝ์—์„œ๋งŒ ํ™œ์„ฑํ™”
@RestController
@RequestMapping("/api/test")
@RequiredArgsConstructor
public class TestAuthController {
    
    @GetMapping("/token")
    public ResponseEntity<Map<String, String>> getTestToken() {
        String token = TestJwtTokenProvider.generateTestToken("test-user");
        return ResponseEntity.ok(Map.of("token", token));
    }
}

๊ทธ๋ฆฌ๊ณ  SecurityConfig์—์„œ ์ด ํ…Œ์ŠคํŠธ ์—”๋“œํฌ์ธํŠธ์— ๋Œ€ํ•œ ์ ‘๊ทผ์„ ํ—ˆ์šฉํ•ด์ฃผ๋ฉด ๋ฉ๋‹ˆ๋‹ค:

.requestMatchers("/api/test/token").permitAll()

์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ๊ฐœ๋ฐœ ํ™˜๊ฒฝ์—์„œ /api/test/token์„ ํ˜ธ์ถœํ•˜์—ฌ ํ…Œ์ŠคํŠธ์šฉ ํ† ํฐ์„ ๋ฐ›๊ณ 
๋ฐ›์€ ํ† ํฐ์„ Swagger UI์˜ Authorize ๋ฒ„ํŠผ์„ ํ†ตํ•ด ์„ค์ •ํ•œ ํ›„ API ํ…Œ์ŠคํŠธ๋ฅผ ์ง„ํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

  1. BundleResponse ์ตœ์ ํ™”

ํ˜„์žฌ์ฒ˜๋Ÿผ ๋ชจ๋“  ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ๊ฒƒ๋ณด๋‹ค๋Š”, ์šฉ๋„์— ๋งž๊ฒŒ ์‘๋‹ต์„ ๋ถ„๋ฆฌํ•˜๋Š” ๊ฒƒ์„ ์ถ”์ฒœ๋“œ๋ฆฝ๋‹ˆ๋‹ค.

@kitechoi kitechoi closed this Feb 8, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants