로그인/로그아웃 기능 타입리프 연동 > 스프링 부트

본문 바로가기

[개발] 로그인/로그아웃 기능 타입리프 연동

필기자
2022-11-01 12:47 5,845 0

본문

20221101133559_58c21c62e2e6f71b42558e4bc6ecc1e2_vtkn.png

 

20221101123556_58c21c62e2e6f71b42558e4bc6ecc1e2_em0x.png

 

타입리프에서 스프링 부트 security 정보 가져오기(session)

pom.xml



        <!--thymeleaf-extras-springsecurity5 의존성 추가-->
        <dependency>
            <groupId>org.thymeleaf.extras</groupId>
            <artifactId>thymeleaf-extras-springsecurity5</artifactId>
        </dependency>
        <!--thymeleaf-extras-springsecurity5 의존성 추가-->

 

templates/fragments/header.html 내용 추가



<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
      xmlns:sec="http://www.thymeleaf.org/extras/spring-security">

<div th:fragment="header">
   <nav class="navbar navbar-expand-sm bg-primary navbar-dark">
      <button class="navbar-toggler" type="button" data-toggle="collapse"
              data-target="#navbarTogglerDemo03" aria-controls="navbarTogglerDemo03"
              aria-expanded="false" aria-label="Toggle navigation">
         <span class="navbar-toggler-icon"></span>
      </button>
      <a class="navbar-brand" href="/">Shop</a>

      <div class="collapse navbar-collapse" id="navbarTogglerDemo03">
         <ul class="navbar-nav mr-auto mt-2 mt-lg-0">
            <li class="nav-item" sec:authorize="hasAnyAuthority('ROLE_ADMIN')"> <!--ROLE ADMIN인 경우 노출-->
               <a class="nav-link" href="/admin/item/new">상품 등록</a>
            </li>
            <li class="nav-item" sec:authorize="hasAnyAuthority('ROLE_ADMIN')"> <!--ROLE ADMIN인 경우 노출-->
               <a class="nav-link" href="/admin/items">상품 관리</a>
            </li>
            <li class="nav-item" sec:authorize="isAuthenticated()"> <!--인증 회원-->
               <a class="nav-link" href="/cart">장바구니</a>
            </li>
            <li class="nav-item" sec:authorize="isAuthenticated()"> <!--인증 회원-->
               <a class="nav-link" href="/orders">구매이력</a>
            </li>
            <li class="nav-item" sec:authorize="isAnonymous()"> <!--비인증 회원-->
               <a class="nav-link" href="/members/login">로그인</a>
            </li>
            <li class="nav-item" sec:authorize="isAuthenticated()"> <!--인증 회원-->
               <a class="nav-link" href="/members/logout">로그아웃</a>
            </li>
         </ul>
         <form class="form-inline my-2 my-lg-0" th:action="@{/}" method="get">
            <input name="searchQuery" class="form-control mr-sm-2" type="search" placeholder="Search" aria-label="Search">
            <button class="btn btn-outline-success my-2 my-sm-0" type="submit">Search</button>
         </form>
      </div>
   </nav>
</div>
</html>

 

결과 : http://localhost:8080/members/login

 

20221101133654_58c21c62e2e6f71b42558e4bc6ecc1e2_silv.png

 

20221101133016_58c21c62e2e6f71b42558e4bc6ecc1e2_bvwm.png

댓글목록0

등록된 댓글이 없습니다.
게시판 전체검색