앞에서 관리자(Admin)만 접근할 수 있는 회원 목록 페이지(/member/list)를 완성했습니다.
그런데 일반 사용자(User)가 주소창에 직접 http://localhost:3000/member/list 를 입력해 접근하면 보안상 문제가 발생할 수 있습니다.
이를 방지하기 위해 서버에서는 먼저 세션(Session)을 검사합니다.
권한이 'ADMIN'이 아닌 경우 즉시 로그인 페이지로 되돌립니다.
URL에 msg=authority 값을 추가해 "접근 권한이 없습니다."라는 안내 문구를 표시하도록 합니다.
▼ Code
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>로그인</title>
<link rel="stylesheet" href="/static/common.css" />
</head>
<body>
<h2>로그인</h2>
<form method="post">
<input name="userid" placeholder="아이디" required />
<br />
<input name="pwd" type="password" placeholder="비밀번호" required />
<br />
<button type="submit">로그인</button>
<button type="button" onclick="location.href='/member/register'">회원가입</button>
</form>
<% if (typeof msg !== 'undefined' && msg === 'fail') { %>
<p style="color: red">로그인에 실패했습니다.</p>
<% } else if (typeof msg !== 'undefined' && msg === 'logout') { %>
<p style="color: red">로그아웃이 완료되었습니다.</p>
<% } else if (typeof msg !== 'undefined' && msg === 'success') { %>
<p style="color: red">회원가입이 완료되었습니다.</p>
<% } else if (typeof msg !== 'undefined' && msg === 'authority') { %>
<p style="color: red">접근 권한이 없습니다.</p>
<% } %>
</body>
</html>
▼ Explain
<% } else if (typeof msg !== 'undefined' && msg === 'authority') { %> typeof msg !== 'undefined' msg 변수가 존재하는지 확인하는 부분입니다. msg 값이 전달되지 않아도 오류가 발생하지 않도록 안전하게 검사합니다. msg === 'authority' 서버에서 권한이 없는 사용자를 감지했을 때 res.redirect('/member/login?msg=authority') 코드로 전달된 값을 의미합니다. 이 값이 'authority'일 경우 아래의 안내 문구가 화면에 표시됩니다. <p style="color: red">접근 권한이 없습니다.</p> 빨간색 글씨로 "접근 권한이 없습니다."라는 경고 문구를 출력합니다. 사용자는 해당 메뉴가 관리자 전용임을 즉시 알 수 있습니다.
개인정보처리방침 · 이용약관