220310 (2) : 유저 기능 구현
구현 기능
- 로그인/로그아웃 및 회원가입
- 글 수정 권한 관리 (본인이 작성한 글만 수정 가능)
- 익명 모드
로그인
유저가 로그인을 할 경우, 로그인한 유저의 데이터를 유저가 웹페이지를 나가지 전까지 유지해야 한다. 이때 사용하는 보관소가 HttpSession이다. 프런트 컨트롤러에서 HttpSession을 생성하여, 로그인시 HttpSession에 로그인 정보를 저장해 놓은다. 로그아웃을 할 경우, 세션을 무효화한다.
페이지 컨트롤러는 UserDao의 exist() 메서드를 호출해 id/pw에 해당하는 유저가 있는지 여부를 확인하고, 있을 경우, 해당 유저 객체를 리턴받는다. 그후, 해당 유저의 name 필드를 세션에 저장한다.
public class LogInController implements Controller {
MySqlUserDao userDao;
public LogInController setUserDao(MySqlUserDao userDao) {
this.userDao = userDao;
return this;
}
@Override
public String execute(Map<String, Object> model) throws Exception {
if (model.get("id") == null) {
return "/auth/LoginForm.jsp";
} else {
String id = (String) model.get("id");
String password = (String) model.get("password");
int no = userDao.exist(id, password);
if (no != -1) {
// TODO register user to HttpSession
HttpSession session = (HttpSession) model.get("session");
User loginUser = userDao.selectOne(no);
session.setAttribute("loginUser", loginUser.getName());
return "redirect:../feed/main.do";
} else {
return "redirect:login.do";
}
}
}
}
exist() 메서드는 id, password에 해당하는 유저DB가 존재할 경우 해당 유저의 고유번호(UNO)를, 없을 경우 -1을 리턴한다.
로그아웃
클라이언트가 로그아웃을 요청할 경우, 세션을 초기화한다.
LogoutController.java
public class LogOutController implements Controller {
@Override
public String execute(Map<String, Object> model) throws Exception {
HttpSession session = (HttpSession) model.get("session");
session.invalidate();
return "redirect:login.do";
}
}
로그인을 하지 않고, 메인 피드에 접속했을 경우
로그인 페이지 외의 페이지에 로그인을 하지 않고 접속했을 경우, 무조건 로그인 페이지를 리다이랙션한다. 각각의 페이지 컨트롤러들은 세션을 확인 후, 세션에 로그인 정보(loginUser)가 등록되어있지 않은 경우, 로그인 페이지를 리다이랙션하는 메세지를 리턴한다.
FeedListController.java
public class FeedListController implements Controller {
MySqlFeedDao feedDao;
public FeedListController setFeedDao(MySqlFeedDao feedDao) {
this.feedDao = feedDao;
return this;
}
@Override
public String execute(Map<String, Object> model) throws Exception {
HttpSession session = (HttpSession) model.get("session");
if (session.getAttribute("loginUser") == null) {
return "redirect:../auth/login.do";
}
model.put("loginUser", session.getAttribute("loginUser"));
model.put("feeds", feedDao.selectList());
return "/feed/FeedList.jsp";
}
}
글 작성자에게만 수정 권한 부여
피드 컨텐츠 페이지에 접속하면 피드의 글쓴이와 세션에 등록된 로그인 유저의 정보를 비교하여 같을 경우, 수정 버튼을 활성화(abled), 다를 경우 비활성화(disabled) 시켰다.
public class FeedContentController implements Controller {
MySqlFeedDao feedDao;
public FeedContentController setFeedDao(MySqlFeedDao feedDao) {
this.feedDao = feedDao;
return this;
}
@Override
public String execute(Map<String, Object> model) throws Exception {
if (model.get("no") != null) {
Integer no = (Integer) model.get("no");
HttpSession session = (HttpSession) model.get("session");
Feed feed = feedDao.selectOne(no);
String loginUser = (String) session.getAttribute("loginUser");
// check authority to edit
if (!loginUser.equals("익명") && loginUser.equals(feed.getWriter())) {
model.put("authority", "");
} else {
model.put("authority", "disabled");
}
// feed content
model.put("feed", feedDao.selectOne(no));
return "/feed/FeedContent.jsp";
} else {
return "error.jsp";
}
}
}
model에 "athority"를 key로 하는 데이터를 삽입한다. 만약 로그인 유저와 글쓴이가 같은 경우는 공백 문자열을, 다른 경우는 "disabled"를 value로 한다.
<a href="edit.do?no=${requestScope.feed.getNo()}" class="btn btn-danger ${requestScope.authority}">수정</a>
jsp 파일에서 수정 버튼을 구현한 부분이다. model에 삽입한 authority 값이 만약 disabled일 경우 class 속성이 비활성화 돼 버튼 클릭이 안되게 된다.
익명 모드
로그인을 하지 않고 익명으로 게시판을 이용하는 기능이다. 로그인 페이지에서 '익명으로 시작'을 체크할 경우 익명모드가 시작되게 된다.
'익명으로 시작'을 클릭하면 클라이언트는 'anonymous' 값을 'true'로 설정한다. LoginController는 해당 값을 확인하여 'true' 일 경우, id 비번과 관계없이 익명 모드를 시작한다. (세션의 유저정보를 '익명'으로 등록)
public class LogInController implements Controller {
MySqlUserDao userDao;
public LogInController setUserDao(MySqlUserDao userDao) {
this.userDao = userDao;
return this;
}
@Override
public String execute(Map<String, Object> model) throws Exception {
if (model.get("anonymous") != null) {
HttpSession session = (HttpSession) model.get("session");
session.setAttribute("loginUser", "익명");
return "redirect:../feed/main.do";
}
if (model.get("id") == null) {
return "/auth/LoginForm.jsp";
} else {
String id = (String) model.get("id");
String password = (String) model.get("password");
int no = userDao.exist(id, password);
if (no != -1) {
// TODO register user to HttpSession
HttpSession session = (HttpSession) model.get("session");
User loginUser = userDao.selectOne(no);
session.setAttribute("loginUser", loginUser.getName());
return "redirect:../feed/main.do";
} else {
return "redirect:login.do";
}
}
}
}