๋ด๊ฐ ์ดํดํ ๊ฑฐ ๋ฐํ์ผ๋ก ์งํผํฐํํ ๋ฌผ์ด๋ณด๋ ์ ๋ฐ์ ์ผ๋ก ์์ฃผ ์ ํํ๊ณ ์ ์ดํดํ๊ณ ์๋จ๋ค..
ํนํ DTO / Entity / Filter / Config ๋ฑ ์ญํ ๊ณผ ํ๋ฆ์ ๋ช ํํ ์ก์๋ค๋ ์ ์์ ์ง๊ธ ์์ค์ด๋ฉด ์ด๊ธ์ ๋ฐ์ด๋๊ณ ์๋ ์ค์ด์์. <= ๊ธฐ๋ถ์ด ์ข๊ตฌ๋ง???
1) Application์ผ๋ก ๋๋๋ ํ์ผ = ์ต์๋จ ํ์ผ
๋ด๋ถ์ ์ผ๋ก๋ ํฐ์บฃ์๋ฒ ์คํ, ๋น ๋ฑ๋ก, ์ปจํธ๋กค๋ฌ ๋ฑ๋ก ๋ฑ์ ์๋์ผ๋ก ํด์ฃผ๋ ์ญํ ์ด๋ค.
2) DTO๋ ์ฌ์ฉ์ ์ ๋ ฅ์ ๋ฐ๊ฑฐ๋ ์๋ต์ ์ค ๋ ์ฌ์ฉํ๋ ๊ฐ์ฒด
- RequestDto: ์ฌ์ฉ์์ ์ ๋ ฅ์ ์ ๋ฌ๋ฐ์ ๋ ์ฌ์ฉ
- ResponseDto: ์ฌ์ฉ์์๊ฒ ์๋ตํ ๋ ์ฌ์ฉ
3) Request DTO๋ ์์ฑ์๋ก ๊ฐ ์ ์ฅ
public PostCreateRequestDto(String title, String contents)
- ์์ฑ์๋ฅผ ํตํด title, contents ๊ฐ์ ๊ฐ์ฒด์ ์ ์ฅํ๋ค.
4) Response DTO๋ ๋ฐํํ ๊ฐ์ ๋ด์
public PostResponseDto(String title, String contents)
5) ์์ ์์ฒญ DTO๋ ์์ฑ์๋ฅผ ํตํด ์ ์ฅ
package com.example.springfilter.dto;
import lombok.Getter;
@Getter
public class PostUpdateRequestDto {
private final String title;
public PostUpdateRequestDto(String title) {
this.title = title;
}
}
package com.example.springfilter.dto;
import lombok.Getter;
@Getter
public class PostCreateRequestDto {
private final String title;
private final String contents;
public PostCreateRequestDto(String title, String contents) {
this.title = title;
this.contents = contents;
}
}
์์ ์์๋ ์ฌ์ฉ์๋ก๋ถํฐ ๋ฐ์ดํฐ๋ฅผ ๋ฐ์์ผ ํ๋ฏ๋ก RequestDto๋ฅผ ์ฐ๊ณ , ์์ฑ์๋ฅผ ํตํด ๊ฐ์ ์ ์ฅํ๋ค.
7) Filter๋ ๋ฏธ๋ค์จ์ด์ฒ๋ผ ๋์ํ๊ณ , ๋ฐ๋์ ์ค๋ฒ๋ผ์ด๋ฉ ํ์
Filter๋ doFilter() ๋ฉ์๋๋ฅผ ๋ฐ๋์ ๊ตฌํํด์ผ ํ๊ณ , ์์ฒญ์ด ๋ค์ด์ฌ ๋๋ง๋ค ๋์ํ๋ ์ธํฐ์ ํฐ ์ญํ
8) ์ดํด๊ฐ ์๋๋ ์ฝ๋
package com.example.springfilter.filter;
import jakarta.servlet.*;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.util.PatternMatchUtils;
//ํํฐ๊ฐ IOException์ด๋ ServletException์ ๋์ง ์ ์์ด์ ๋ถ์ธ ์์ธ์ฒ๋ฆฌ ๋ฌธ๋ฒ์.
import java.io.IOException;
public class LoginFilter implements Filter {
private static final String[] WHITE_LIST = {"/", "/user/signup", "/login", "/logout"};
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
//ServletRequest๋ ๊ธฐ๋ฅ์ด ์ ์ด์, ๋ ๋ง์ ๊ธฐ๋ฅ์ ๊ฐ์ง HttpServletRequest๋ก ๋ค์ด์บ์คํ
//์ฌ๊ธฐ์ ๋งํ๋ ์์ฒญ์ ๋ธ๋ผ์ฐ์ ๋ API ํด๋ผ์ด์ธํธ(ํฌ์คํธ๋งจ ๋ฑ)๊ฐ ๋ณด๋ธ HTTP ์์ฒญ์.
HttpServletRequest httpRequest = (HttpServletRequest) servletRequest;
String requestURI = httpRequest.getRequestURI();
}
private boolean isWhiteList(String requestURI) {
// request URI๊ฐ whiteListURL์ ํฌํจ๋๋์ง ํ์ธ
// ํฌํจ๋๋ฉด true ๋ฐํ
// ํฌํจ๋์ง ์์ผ๋ฉด false ๋ฐํ
return PatternMatchUtils.simpleMatch(WHITE_LIST, requestURI);
}
}
CustomFilter vs LoginFilter ์ฐจ์ด์ ?
- CustomFilter: ์์ฒญ ๋ก๊ทธ๋ง ์ฐ๋ ๋จ์ ํํฐ (URL์ถ๋ ฅ)
- LoginFilter: ์ธ์ฆ/์ธ๊ฐ ํ์ธ์ ์ํ ํํฐ (๋ก๊ทธ์ธ ์ํ ํ์ธ)
9) PostController๋ ์ปจํธ๋กค๋ฌ, ๋ทฐ ๋ฐํ์ฉ
REST API์ด๋ฏ๋ก ํ๋ฉด(View)์ ์ฃผ๋ ๊ฑด ์๋๊ณ , JSON ํ์์ ๋ฐ์ดํฐ๋ฅผ ํด๋ผ์ด์ธํธ์ ๋ฐํํ๋ ์ญํ
10) WebConfig๋ ํํฐ ๋ฑ๋ก์ฉ ์ค์ ํ์ผ
- @Configuration์ ์ค์ ํด๋์ค๋ ์๋ฏธ์ด๊ณ ,
- FilterRegistrationBean์ ํตํด ๋ง๋ Filter๋ฅผ ์คํ๋ง ๋ถํธ์ ๋ฑ๋กํ๋ ์์ ์ด๋ค.
11) Entity๋ ๋ฐ์ดํฐ ๊ตฌ์กฐ๋ฅผ ํํํ๋ ํด๋์ค
- DB์ ์ ์ฅํ ์ ๋ณด์ด๊ธฐ๋ ํ๊ณ , Post ๊ฐ์ฒด์ฒ๋ผ ๋น์ฆ๋์ค ๋ฐ์ดํฐ์ ์ค์ฌ ๊ตฌ์กฐ์ ํด๋นํ๋ ํด๋์ค๋ฅผ Entity๋ผ๊ณ ๋ถ๋ฆ
- ๋์ค์ JPA์ ์ฐ๊ฒฐ๋์ด DB ํ ์ด๋ธ๊ณผ ๋งคํ๋๋ ํด๋์ค๊ฐ ๋๊ธฐ๋ ํจ.
'Backend > ๐ฑ Spring' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| [Spring]ํ์ผ๊ตฌ์กฐ (์ค์) (0) | 2025.07.29 |
|---|---|
| [Spring]Filter (0) | 2025.07.29 |
| [Spring] Bean Validation (5) | 2025.07.28 |
| [Spring]์์กด๊ด๊ณ ์ฃผ์ (DI) (2) | 2025.07.28 |
| [Spring]Spring Bean (2) | 2025.07.28 |