본문 바로가기

Server Programming/Spring Boot Full-Stack Programming

[스프링 풀스택 클론 코딩] 로그인 여부에 따른 네비게이션 메뉴 구성

반응형

의존성 추가 후 네임스페이스 설정

 <dependency>
			<groupId>org.springframework.security</groupId>
			<artifactId>spring-security-test</artifactId>
			<scope>test</scope>
		</dependency>
 
 xmlns:sec="http://www.thymeleaf.org/extras/spring-security"

 

index

<!DOCTYPE html>
<!-- 회원가입 뷰 -> sign-up form을 만들어야 한다. 닉네임 이메일 비밀번호-->
<!-- 타임리프 네임스페이스 설정-->
<html lang="en" xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.thymeleaf.org/extras/spring-security">

<head>
	<meta charset="UTF-8">
	<title>Demo</title>
	<!-- CSS only -->
	<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.0/dist/css/bootstrap.min.css" rel="stylesheet"
		integrity="sha384-gH2yIJqKdNHPEq0n4Mqa/HGKIhSkIHeL5AyhkYV8i59U5AR6csBvApHHNl/vI1Bx" crossorigin="anonymous">
	<style>
		.container {
			max-width: 100%;
		}
	</style>
</head>

<body class="bg-light">
	<!-- 네비바 만들기-->
	<nav class="navbar navbar-expand-sm navbar-dark bg-dark">

		<!-- 배너-->
		<a class="navbar-brand" href="/" th:href="@{/}">
			<!--@는 경로설정인데 이 경우엔 root경로 -->
			<img src="/images/logo_sm.png" width="30" height="30">
		</a>

		<!-- 네비게이션 아이템 검색 창-->
		<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent">
			<span class="navbar-toggler-icon"> </span>
		</button>

		<div class="collapse navbar-collapse" id="navbarSupportedContent">
			<ul class="navbar-nav mr-auto">
				<li class="nav-item">
					<!-- 스터디 찾기 버튼 href="#" th:href="은 타임리프 동작 X와 동작시-->
					<form th:action="@{/search/study}" class="form-inline" method="get" action="#">
						<input class="form-control mr-sm-2" name="keyword" placeholder="스터디 찾기" type="search" />
					</form>
				</li>
			</ul>
			<!-- 로그인, 가입 버튼 href="#" th:href="은 타임리프 동작 X와 동작시-->
			<ul class="navbar-nav justify-content-end">
				<li class="nav-item" sec:authorize="!isAuthenticated()">
					<a class="nav-link" th:href="@{/login}">로그인</a>
				</li>
				<li class="nav-item" sec:authorize="!isAuthenticated()">
					<a class="nav-link" th:href="@{/sign-up}">가입</a>
				</li>
				<li class="nav-item" sec:authorize="isAuthenticated()">
					<a class="nav-link" th:href="@{/notifications}">
						알림
					</a>
				</li>
				<li class="nav-item" sec:authorize="isAuthenticated()">
					<a class="nav-link btn btn-outline-primary" th:href="@{/new-study}">
						스터디 개설
					</a>
				</li>
				<li class="nav-item dropdown" sec:authorize="isAuthenticated()">
					<a class="nav-link dropdown-toggle" href="#" role="button" data-bs-toggle="dropdown"
						aria-expanded="false">
						프로필
					</a>
					<div class="dropdown-menu dropdown-menu-sm-right" aria-labelledby="userDropdown">
						<ul>
							<h6 class="dropdown-header">
								<span sec:authentication="name">Username</span>
							</h6>
							<a class="dropdown-item" th:href="@{'/profile/' + ${#authentication.name}}">프로필</a>
							<a class="dropdown-item">스터디</a>
							<div class="dropdown-divider"></div>
							<a class="dropdown-item" th:href="@{'/settings/profile'}">설정</a>
							<form class="form-inline my-2 my-lg-0" action="#" th:action="@{/logout}" method="post">
								<button class="dropdown-item" type="submit">로그아웃</button>
							</form>
						</ul>
					</div>
				</li>
			</ul>
		</div>
	</nav>

	<!-- index html-->
	<div class="container">
		<div class="py-5 text-center">
			<h2>데모</h2>
		</div>


		<footer th:fragment="footer">
			<div class="row justify-content-center">
				<img class="mb-2" src="/images/logo_long_kr.jpg" alt="" width="100">
				<small class="d-block mb-3 text-muted">&copy; 2020</small>
			</div>
		</footer>
	</div>


	<!-- JavaScript Bundle with Popper -->
	<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.0/dist/js/bootstrap.bundle.min.js"
		integrity="sha384-A3rJD856KowSb7dwlZdYEkO39Gagi7vIsF0jrRAoQmDKKtQBHUuLZ9AsSv4jD4Xa"
		crossorigin="anonymous"></script>

	<!--needs-validation을 가지는 폼을 가져와 submit 이벤트 발생시 유효여부 검증, 유효하지 않으면, submit안되도록 처리 -->
	<!--유효하면 was-validated에 추가 -> input의 required을 기반으로 처리-->
	<script type="application/javascript" th:fragment="form-validation">

	</script>

</body>

</html>

 

 

<li class="nav-item" sec:authorize="!isAuthenticated()">
    <a class="nav-link" th:href="@{/login}">로그인</a>
</li>
<li class="nav-item" sec:authorize="!isAuthenticated()">
    <a class="nav-link" th:href="@{/sign-up}">가입</a>
</li>
<li class="nav-item" sec:authorize="isAuthenticated()">
    <a class="nav-link" th:href="@{/notifications}">
        알림
    </a>
</li>
<li class="nav-item" sec:authorize="isAuthenticated()">
    <a class="nav-link btn btn-outline-primary" th:href="@{/new-study}">
        스터디 개설
    </a>
</li>
<li class="nav-item dropdown" sec:authorize="isAuthenticated()">
    <a class="nav-link dropdown-toggle" href="#" role="button" data-bs-toggle="dropdown"
        aria-expanded="false">
        프로필
    </a>

로그인 상태 일때 : 알림, 스터디 개설, 프로필 메뉴

로그아웃 상태 일때 : 로그인, 가입 메뉴

 

<ul>
    <h6 class="dropdown-header">
        <span sec:authentication="name">Username</span>
    </h6>
    <a class="dropdown-item" th:href="@{'/profile/' + ${#authentication.name}}">프로필</a>
    <a class="dropdown-item">스터디</a>
    <div class="dropdown-divider"></div>
    <a class="dropdown-item" th:href="@{'/settings/profile'}">설정</a>
    <form class="form-inline my-2 my-lg-0" action="#" th:action="@{/logout}" method="post">
        <button class="dropdown-item" type="submit">로그아웃</button>
    </form>
</ul>

로그인 상태일때, 로그인된 아이디의 이름을 출력한다.

<span sec:authentication="name">Username</span>
반응형