๋ก๊ทธ์ธํ ์ฌ์ฉ์๋ง ์ ๊ทผ ๊ฐ๋ฅํ API์ ๋ถ์ด๋ ์ด๋
ธํ
์ด์
์
๋๋ค.
๋ด๋ถ์ ์ผ๋ก Spring Security์ @PreAuthorize("isAuthenticated()")๋ฅผ ๊ฐ์ธ๊ณ ์์ต๋๋ค.
๋ฉ์๋ ๋๋ ํด๋์ค ๋จ์์ ์ ์ฉํ ์ ์์ต๋๋ค.
// ๋ฉ์๋ ๋จ์
@Authenticated
@GetMapping("/me")
public ResponseEntity<AccountDto> getMyProfile(...) { ... }
// ํด๋์ค ๋จ์ โ ํด๋น ์ปจํธ๋กค๋ฌ์ ๋ชจ๋ ์๋ํฌ์ธํธ์ ์ธ์ฆ ์ ์ฉ
@Authenticated
@RestController
@RequestMapping("/api/posts")
public class PostController { ... }์ธ์ฆ๋ ์ฌ์ฉ์์ ์ ๋ณด๋ฅผ ์ปจํธ๋กค๋ฌ ํ๋ผ๋ฏธํฐ๋ก ์ฃผ์
๋ฐ๋ ์ด๋
ธํ
์ด์
์
๋๋ค.
AccountDto(id, nickname) ํ์
ํ๋ผ๋ฏธํฐ์๋ง ์ฌ์ฉํ ์ ์์ต๋๋ค.
@Authenticated์ ํจ๊ป ์ฌ์ฉํ๋ ๊ฒ์ด ์ผ๋ฐ์ ์
๋๋ค.
@Authenticated
@GetMapping("/me")
public ResponseEntity<AccountDto> getMyProfile(
@CurrentAccount AccountDto account
) {
return ResponseEntity.ok(account);
}์ฃผ์: ์ธ์ฆ๋์ง ์์ ์ํ์์
@CurrentAccount๋ฅผ ์ฌ์ฉํ๋ฉดUnauthorizedException์ด ๋ฐ์ํฉ๋๋ค.
๋ฐ๋์ ServerApplicationTests๋ฅผ ์คํํ ๋ค pushํ์ธ์.
./gradlew test --tests "com.mada.server.ServerApplicationTests"์ด ํ
์คํธ๋ Spring Modulith๋ฅผ ํตํด ๋ชจ๋ ๊ฐ ์์กด์ฑ ๊ท์น ์๋ฐ์ ๊ฒ์ฆํ๊ณ , PlantUML ๋ค์ด์ด๊ทธ๋จ๊ณผ ๋ฌธ์๋ฅผ ์๋ ์์ฑํฉ๋๋ค.
๋ชจ๋ ๊ท์น์ ์ด๊ธฐ๋ ์ฝ๋๊ฐ ์์ผ๋ฉด ์ด ํ
์คํธ์์ ์คํจํฉ๋๋ค.
์ด ํ๋ก์ ํธ๋ Spring Modulith๋ฅผ ์ฌ์ฉํ์ฌ ๋ชจ๋ ๊ฐ ์์กด์ฑ์ ๊ฐ์ ํฉ๋๋ค.
com.mada.server ํ์์ ๊ฐ ํจํค์ง(์: account, auth, common, notification)๊ฐ ํ๋์ ๋
๋ฆฝ ๋ชจ๋์
๋๋ค.
๊ฐ ๋ชจ๋์ package-info.java์ @ApplicationModule์ ์ ์ธํ์ฌ ๋ฑ๋กํฉ๋๋ค.
com.mada.server
โโโ account โ ๋ชจ๋
โโโ auth โ ๋ชจ๋
โโโ common โ ๋ชจ๋
โโโ notification โ ๋ชจ๋
๋ชจ๋์ ๋ฃจํธ ํจํค์ง์ ์์นํ public ํ์ ๋ง ๋ค๋ฅธ ๋ชจ๋์์ ์ฐธ์กฐํ ์ ์์ต๋๋ค.
account/
โโโ Account.java โ
๋ค๋ฅธ ๋ชจ๋์์ ์ฐธ์กฐ ๊ฐ๋ฅ
โโโ AccountDto.java โ
๋ค๋ฅธ ๋ชจ๋์์ ์ฐธ์กฐ ๊ฐ๋ฅ
โโโ AccountQueryService.java โ
๋ค๋ฅธ ๋ชจ๋์์ ์ฐธ์กฐ ๊ฐ๋ฅ
โโโ internal/
โโโ AccountRepository.java โ ์ฐธ์กฐ ๋ถ๊ฐ (๋ด๋ถ ์๋ธํจํค์ง)
internal ๋ฑ ์๋ธํจํค์ง์ ์์นํ ํด๋์ค๋ ํด๋น ๋ชจ๋ ๋ด๋ถ ๊ตฌํ์ผ๋ก ์ทจ๊ธ๋์ด, ๋ค๋ฅธ ๋ชจ๋์์ ์ง์ ์ฐธ์กฐํ ์ ์์ต๋๋ค.
// โ ๋ค๋ฅธ ๋ชจ๋์์ internal ํจํค์ง ์ง์ ์ฐธ์กฐ โ ๋ชจ๋ ๊ท์น ์๋ฐ
import com.mada.server.account.internal.AccountRepository;์๋ธํจํค์ง๋ฅผ ๋ค๋ฅธ ๋ชจ๋์์ ์ฐธ์กฐํ ์ ์๊ฒ ํ๋ ค๋ฉด, ํด๋น ์๋ธํจํค์ง์ package-info.java์ @NamedInterface๋ฅผ ์ ์ธํฉ๋๋ค.
// common/error/package-info.java
@NamedInterface(name = "error")
package com.mada.server.common.error;
import org.springframework.modulith.NamedInterface;์ ์ธ ํ ๋ค๋ฅธ ๋ชจ๋์ @ApplicationModule์์ ํด๋น named interface๋ฅผ ๋ช
์์ ์ผ๋ก ํ์ฉํด์ผ ํฉ๋๋ค.
// ๋ค๋ฅธ ๋ชจ๋์ package-info.java
@ApplicationModule(allowedDependencies = "common::error")
package com.mada.server.auth;
import org.springframework.modulith.ApplicationModule;์ด๋ ๊ฒ ํ๋ฉด common.error ํจํค์ง์ ํ์
(BusinessException, ErrorResponse ๋ฑ)์ auth ๋ชจ๋์์ ์ฌ์ฉํ ์ ์์ต๋๋ค.
| ์์น | ๋ค๋ฅธ ๋ชจ๋์์ ์ฐธ์กฐ |
|---|---|
| ๋ชจ๋ ๋ฃจํธ ํจํค์ง์ public ํ์ | ๊ฐ๋ฅ |
๋ชจ๋ ๋ด ์๋ธํจํค์ง (internal/ ๋ฑ) |
๋ถ๊ฐ (๊ธฐ๋ณธ) |
@NamedInterface ์ ์ธ๋ ์๋ธํจํค์ง |
๊ฐ๋ฅ (allowedDependencies ์ค์ ํ์) |