Search
Duplicate

๐ŸŒ•ย  [์Šคํ”„๋ง ๊ฒŒ์‹œํŒ] ๋กœ๊ทธ์ธ ์—ฌ๋ถ€์— ๋”ฐ๋ฅธ ํ—ค๋” ๋ฒ„ํŠผ ๋ณ€๊ฒฝ

@3/19/2023

๊ฐœ์š”

โ€ข
ํ—ค๋”๋ฅผ ๋ ˆ์ด์•„์›ƒ(Thymeleaf fragment)์„ ํ†ตํ•ด ๊ณตํ†ตํ™” ์‹œํ‚จ ์ƒํ™ฉ์—์„œ, ๋กœ๊ทธ์ธ ์—ฌ๋ถ€๋‚˜ ํŽ˜์ด์ง€์— ๋”ฐ๋ผ ํ—ค๋” ๋ฒ„ํŠผ์ด ๋‹ฌ๋ผ์ ธ์•ผ ํ•จ
โ—ฆ
๋น„ ๋กœ๊ทธ์ธ ์ƒํƒœ - ๋กœ๊ทธ์ธ, ํšŒ์›๊ฐ€์ž…
โ—ฆ
๋กœ๊ทธ์ธ ์ƒํƒœ - ๋กœ๊ทธ์•„์›ƒ, ํšŒ์› ์ •๋ณด
โ—ฆ
๋กœ๊ทธ์ธ ํ™”๋ฉด - ์•„๋ฌด๊ฒƒ๋„ ์—†์Œ
โ€ข
์Šคํ”„๋ง ๋ถ€ํŠธ 3.0๋ถ€ํ„ฐ ๋ณด์•ˆ์ƒ ์ด์œ ๋กœ #session ๊ฐ์ฒด๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†์–ด์ง. ๋”ฐ๋ผ์„œ ๋‚˜๋Š” 2๋ฒ„์ „์ด์ง€๋งŒ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ  ๊ตฌํ˜„ํ•˜๊ธฐ๋กœ ํ•จ
โ—ฆ
ํ•˜์ง€๋งŒ ์ด๋ ‡๊ฒŒ ๋  ๊ฒฝ์šฐ ์ปจํŠธ๋กค๋Ÿฌ๋งˆ๋‹ค model์— ๋ฒ„ํŠผ์„ ๋ณด์—ฌ์ค„ ์กฐ๊ฑด์— ๊ด€ํ•œ ์†์„ฑ์„ ์„ค์ •ํ•ด์ค˜์•ผ ํ•จ
โ—ฆ
์ฐพ์•„๋ณด๋‹ˆ ์ด๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด ์Šคํ”„๋ง ์‹œํ๋ฆฌํ‹ฐ๋ฅผ ๋งŽ์ด ์‚ฌ์šฉํ•จ
โ—ฆ
๋‚˜๋Š” ์Šคํ”„๋ง ์ธํ„ฐ์…‰ํ„ฐ๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๊ตฌํ˜„ํ•˜๊ธฐ๋กœ ํ•จ

์ธํ„ฐ์…‰ํ„ฐ

โ€ข
๋ชจ๋“  ์š”์ฒญ๋ณ„๋กœ ์„ธ์…˜์„ ๋’ค์ ธ์„œ ๋กœ๊ทธ์ธ ์ •๋ณด ํ™•์ธ
โ—ฆ
์ถ”ํ›„์— ๋น„ ๋กœ๊ทธ์ธ ์‚ฌ์šฉ์ž๋„ ๋ณผ ์ˆ˜ ์žˆ๋Š” ํ™”๋ฉด์—๋งŒ ์„ค์ • ์˜ˆ์ •
โ–ช
๋กœ๊ทธ์ธ ์‚ฌ์šฉ์ž๋งŒ ๋ณผ ์ˆ˜ ์žˆ๋Š” ํ™”๋ฉด์€ ๋ณ„๋„ ์ธํ„ฐ์…‰ํ„ฐ ์„ค์ •
โ€ข
์ด ๋กœ์ง์€ ๋กœ๊ทธ์ธ์„ ์š”๊ตฌํ•˜๋Š” ํŽ˜์ด์ง€์—์„œ ๋กœ๊ทธ์ธ ์—ฌ๋ถ€๋ฅผ ํ™•์ธํ•˜๋Š” ๋กœ์ง๊ณผ ๋น„์Šทํ•˜์ง€๋งŒ, ๋น„ ๋กœ๊ทธ์ธ ์œ ์ €๋„ ๋ณผ ์ˆ˜ ์žˆ๋Š” ํŽ˜์ด์ง€์— ๋Œ€ํ•ด์„œ ์ ์šฉ๋œ๋‹ค๋Š” ์ฐจ์ด์ ์ด ์žˆ์Œ. ์ „์ž๋Š” postHandle, ํ›„์ž๋Š” preHandle
โ—ฆ
๋กœ๊ทธ์ธ ์ •๋ณด๊ฐ€ ์—†์œผ๋ฉด isLoggedIn ์†์„ฑ์— false ์•„๋‹ˆ๋ฉด true
public class LoginInterceptor implements HandlerInterceptor { @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) { HttpSession session = request.getSession(false); if (session == null || session.getAttribute("loginUser") == null) { request.setAttribute("isLoggedIn", false); } else { request.setAttribute("isLoggedIn", true); } } }
Java
๋ณต์‚ฌ
โ€ข
์ธํ„ฐ์…‰ํ„ฐ ๋“ฑ๋ก(webConfig)
@Configuration public class WebConfig implements WebMvcConfigurer { @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(new LoginInterceptor()) .order(1) .addPathPatterns("/**"); } }
Java
๋ณต์‚ฌ

๋ทฐ(ํƒ€์ž„๋ฆฌํ”„)

<a th:unless="${#objects.nullSafe(hideLoginButtons, false) or isLoggedIn}" type="button" class="btn btn-outline-primary ms-auto" href="/users/login">๋กœ๊ทธ์ธ</a> <a th:unless="${#objects.nullSafe(hideLoginButtons, false) or isLoggedIn}" type="button" class="btn btn-primary ms-2" href="#">ํšŒ์›๊ฐ€์ž…</a> <a th:if="${isLoggedIn}" type="button" class="btn btn-outline-secondary ms-auto" href="/users/logout">๋กœ๊ทธ์•„์›ƒ</a> <a th:if="${isLoggedIn}" type="button" class="btn btn-outline-primary ms-2" href="#">๋‚ด ์ •๋ณด</a>
HTML
๋ณต์‚ฌ
โ€ข
hideLoginButtons

๊ฒฐ๊ณผ ํ™”๋ฉด

โ€ข
๋กœ๊ทธ์ธ ์ „
โ€ข
๋กœ๊ทธ์ธ ํ™”๋ฉด
โ€ข
๋กœ๊ทธ์ธ ํ›„

๊ด€๋ จ ์ปค๋ฐ‹

98afe12e8a611083bf96a3128fdda8a9635872dc
commit