๐Ÿ”ฅ #7 ์Šคํ”„๋ง ์‹œํ๋ฆฌํ‹ฐ ์ดˆ๊ธฐ ์„ค์ •

๋กœ๊ทธ์ธ ๊ธฐ๋Šฅ์— ์Šคํ”„๋ง ์‹œํ๋ฆฌํ‹ฐ ์ ์šฉํ•˜๊ธฐ

  • ๋กœ๊ทธ์ธ ์š”์ฒญ์„ ํ•˜๋ฉด ์‹œํ๋ฆฌํ‹ฐ๊ฐ€ ์ง€์ผœ๋ณด๋‹ค๊ฐ€ ๋กœ๊ทธ์ธ ์š”์ฒญ์„ ๊ฐ€๋กœ์ฑˆ๋‹ค.
  • ๋กœ๊ทธ์ธ ์š”์ฒญ์œผ๋กœ ๋“ค์–ด์˜จ ํŒŒ๋ผ๋ฏธํ„ฐ(username, password)๋ฅผ ๊ฐ€์ง€๊ณ  ์‹œํ๋ฆฌํ‹ฐ๊ฐ€ ๋กœ๊ทธ์ธ์„ ์ง„ํ–‰ํ•œ๋‹ค.
  • ๋กœ๊ทธ์ธ ์ง„ํ–‰์„ ์™„๋ฃŒํ•œ ํ›„ ์‹œํ๋ฆฌํ‹ฐ ์ „์šฉ(?) ์„ธ์…˜์— ์œ ์ € ์ •๋ณด๋ฅผ ๋„ฃ์–ด๋†“๋Š”๋‹ค. (์ด ๋˜ํ•œ ์Šคํ”„๋ง์ด IoC๋กœ ๊ด€๋ฆฌ)
  • ์ด๋•Œ, ์‹œํ๋ฆฌํ‹ฐ ์„ธ์…˜ ์•ˆ์—๋Š” User ๊ฐ์ฒด๋ฅผ ์ €์žฅ์‹œํ‚ฌ ์ˆœ ์—†๊ณ , UserDetails ํƒ€์ž…์œผ๋กœ ์ €์žฅ์‹œํ‚จ๋‹ค.
  • ์‹œํ๋ฆฌํ‹ฐ ์„ธ์…˜ ์•ˆ์— ์žˆ๋Š” ์œ ์ € ์ •๋ณด๋ฅผ DI ๋ฐ›์•„์„œ ์“ฐ๋ฉด ๋œ๋‹ค.

< pom.xml >

  • JSP์—์„œ ์‹œํ๋ฆฌํ‹ฐ๋ฅผ ์“ฐ๊ธฐ ์œ„ํ•œ ํƒœ๊ทธ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ
<!-- ์‹œํ๋ฆฌํ‹ฐ ํƒœ๊ทธ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ -->
<dependency>
	<groupId>org.springframework.security</groupId>
	<artifactId>spring-security-taglibs</artifactId>
</dependency>
  • ์‹œํ๋ฆฌํ‹ฐ๋ฅผ ์œ„ํ•œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-security</artifactId>
</dependency>

์œ„์˜ ์„ค์ •์ด ๋ชจ๋“  ํŽ˜์ด์ง€๋ฅผ ์ž ๊ถˆ๋ฒ„๋ฆฌ๊ฒŒ ๋œ๋‹ค.

์ฆ‰, ์ด๋ ‡๊ฒŒ ์Šคํ”„๋ง ์‹œํ๋ฆฌํ‹ฐ๋ฅผ ์ ์šฉํ•˜๊ณ , Controller์— ๋”ฐ๋กœ ์„ค์ •ํ•˜์ง€ ์•Š์œผ๋ฉด

ํŽ˜์ด์ง€๊ฐ€ ๋ชจ๋‘ ์ด๋ ‡๊ฒŒ ๋‹ซํžˆ๊ฒŒ ๋œ๋‹ค. ์‹œํ๋ฆฌํ‹ฐ ์„ค์ •์œผ๋กœ ์ธํ•ด ๋ชจ๋“  ๋Œ€๋ฌธ(?)์ด ๋‹ซํ˜€๋ฒ„๋ฆฐ ๊ฒƒ์ด๋‹ค.


๊ธฐ๋ณธ Username์€ user์ด๊ณ , ์ฝ˜์†”์„ ๋ณด๋ฉด ์ด๋ ‡๊ฒŒ ๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ์ค€๋‹ค.
์Šคํ”„๋ง ์‹œํ๋ฆฌํ‹ฐ๊ฐ€ ๊ฐ€๋กœ์ฑ„์„œ ๋กœ๊ทธ์ธ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.

์ด๊ฑธ ์ž…๋ ฅํ•˜๊ณ  ๋กœ๊ทธ์ธ ํ•ด์•ผ์ง€ ํŽ˜์ด์ง€์— ๋“ค์–ด๊ฐˆ ์ˆ˜ ์žˆ๊ฒŒ ๋œ๋‹ค.

์ด๋กท๊ฒŒ ๋“ค์–ด์™”๋‹ค !!
์ด๋•Œ๋ถ€ํ„ฐ๋Š” session์ด ์ž๋™์œผ๋กœ ์ƒ๊ธด๋‹ค.


ํƒœ๊ทธ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์‚ฌ์šฉํ•˜๊ธฐ!!


๋งจ ์œ„์— pom.xml์— ํƒœ๊ทธ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์„ค์ •ํ•œ ๊ฒƒ์„ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ ์‚ฌ์ดํŠธ์— ๋“ค์–ด๊ฐ€์„œ ์•„๋ž˜ taglib๋ฅผ ๊ฐ€์ ธ์˜จ๋‹ค.

๊ทธ ๋‹ค์Œ header ๋งจ ์œ„์— ๋„ฃ์–ด์ค€๋‹ค. jstl ์ฒ˜๋Ÿผ !!

<sec:authorize access="isAuthenticated()">
    <sec:authentication property="principal" var="principal"/>
</sec:authorize>

principal ์ด๋ผ๋Š” var๋ฅผ principal๋กœ ๋งŒ๋“ค๊ณ  ๋‹ด๋Š”๋‹ค. (๋งคํ•‘ํ•œ๋‹ค)
๊ทธ๋Ÿฌ๋ฉด principal์ด ๋น„์–ด์žˆ๋Š”์ง€ ์•„๋‹Œ์ง€ ์—ฌ๋ถ€๋กœ ๋กœ๊ทธ์ธ ํ•œ ์‚ฌ์šฉ์ž์™€ ํ•˜์ง€ ์•Š๋Š” ์‚ฌ์šฉ์ž๋ฅผ ๋‹ค๋ฅด๊ฒŒ ๋ณด์—ฌ์ค„ ์ˆ˜ ์žˆ๋‹ค.

		<c:choose>
        <!--<c:when test="${empty sessionScope.principal}"> ์ด๊ฑฐ ์ง€์šฐ๊ณ -->
            <c:when test="${empty principal}"> <!--์ด๋ ‡๊ฒŒ ๋ณ€๊ฒฝ-->
                <ul class="navbar-nav">
                    <li class="nav-item">
                        <a class="nav-link" href="/loginForm">๋กœ๊ทธ์ธ</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="/joinForm">ํšŒ์›๊ฐ€์ž…</a>
                    </li>
                </ul>
            </c:when>
            <c:otherwise>
                <ul class="navbar-nav">
                    <li class="nav-item">
                        <a class="nav-link" href="/board/form">๊ธ€์“ฐ๊ธฐ</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="/user/form">ํšŒ์›์ •๋ณด</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="/logout">๋กœ๊ทธ์•„์›ƒ</a>
                    </li>
                </ul>
            </c:otherwise>
        </c:choose>

์š”๋กท๊ฒŒ~!


๋กœ๊ทธ์ธ์€ form action์„ ์‚ฌ์šฉํ•  ๊ฒƒ์ด๊ธฐ ๋•Œ๋ฌธ์— input ํƒœ๊ทธ ์•ˆ์— name ๊ฐ’์„ ์„ค์ •ํ•ด์•ผ ํ•œ๋‹ค.

ํšŒ์›๊ฐ€์ž…์ฒ˜๋Ÿผ id๊ฐ’ ๋“ค๊ณ ์™€์„œ ์ œ์ด์ฟผ๋ฆฌ์—์„œ ํ•ด๋‹น id๊ฐ’ ์ฐพ์•„์„œ json์œผ๋กœ ๋งŒ๋“ค์–ด์„œ ์ „์†กํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋‹ค.

์‹œํ๋ฆฌํ‹ฐ ์ ์šฉ

์ข‹์€ ์›นํŽ˜์ด์ง€ ์ฆ๊ฒจ์ฐพ๊ธฐ