Spring Boot Board Project_02 로그인 된 상태에 대한 화면 처리, 로그아웃

송지윤·2024년 4월 19일
0

Spring Framework

목록 보기
33/65

1. 로그인이 되어있지 않은 경우

처음에 보여준 화면처럼 보여줌
main.html

          <!-- 로그인이 되어있지 않은 경우 -->
          <th:block th:if="${session.loginMember == null}">

          <!-- 로그인 박스 -->
          <form action="/member/login" method="POST" id="loginForm">
            <fieldset class="id-pw-area">

              <!-- 아이디/비밀번호 입력 -->
              <section>
                <input type="text" 
                      name="memberEmail"
                      placeholder="이메일">

                <input type="password" 
                      name="memberPw" 
                      placeholder="비밀번호">
              </section>

              <!-- 로그인 버튼 -->
              <section>
                <button>로그인</button>
              </section>
            </fieldset>

            <label>
              <!-- label 태그 : input 태그의 제목을 나타내는 태그  -->
              <input type="checkbox" name="saveId">
              아이디 저장
            </label>

            <article class="signup-find-area">

              <a href="/member/signup">회원가입</a>

              <span> | </span>
              <a href="#">ID/PW 찾기</a>
            </article>

          </form>

          </th:block>

2. 로그인이 되어있는 경우

main.html

          <!-- 로그인이 되어있는 경우 -->
          <th:block th:if="${session.loginMember != null}">

th:unless 안에 들어있는 값이 false 일 때 수행됨

위에 th:if 와 같은 뜻

<th:block th:unless="${session.loginMember == null}"></th:block>

회원가입 할 당시 기본으로 쓸 이미지 static images 에 user.png 올려둠
messages.properties 에 작성된 기본 사용자 이미지

            <article class="login-area">
              <a href="/myPage/profile">
                <!-- 프로필 이미지 -->
                <img th:with="user=#{user.default.image}" 
                    th:src="${session.loginMember.profileImg ?: user}"
	                id="memberProfile"
    	            alt="프로필 이미지"
                >
              </a>

session.loginMember.profileImg 가 null이 아니면 session.loginMember.profileImg에 저장된 이미지 경로가 들어가고 null 이면 user가 들어감

th:with 변수 만드는 거 user 라는 변수명에 /images/user.png 넣어준 거

              <!-- 닉네임, 이메일, 로그아웃 버튼 영역 -->
              <div class="my-info" th:object="${session.loginMember}">
                <div>
                  <a href="/myPage/info" id="nickname" th:text="*{memberNickname}">닉네임</a>
                  <a href="/member/logout" id="logoutBtn">로그아웃</a>
                </div>

                <p th:text="*{memberEmail}">이메일</p>
              </div>

            </article>
          </th:block>

로그아웃

main.html

<a href="/member/logout" id="logoutBtn">로그아웃</a>

Controller 에서 받아서 바로 처리해줌

SessionStatus

세션을 완료(없앰) 시키는 역할의 객체
(현재 세션의 상태를 다룰 수 있는 거)

  • @SessionAttributes 로 등록된 세션을 만료
  • 서버에서 기존 세션 객체가 사라짐과 동시에 새로운 세션 객체가 생성되어 클라이언트와 연결 (기존 정보들은 다 파기됨)
    => 파기된 후에 다시 생성될 때 sessionId 도 다시 발급됨

MemberController

	@GetMapping("logout")
	public String logout(SessionStatus status) {
		
		status.setComplete(); // 세션을 완료 시킴(없앰)
		
		// 메인페이지 리다이렉트
		return "redirect:/";
	}

setComplete(); 세션 만료

0개의 댓글