From f73d0b7f0ea0b959755e1de015824d795dcde6a5 Mon Sep 17 00:00:00 2001 From: greeng00se Date: Sat, 2 Sep 2023 20:32:42 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20=EB=A1=9C=EA=B7=B8=EC=9D=B8=EC=8B=9C=20?= =?UTF-8?q?=EC=84=B8=EC=85=98=EC=97=90=20=EC=82=AC=EC=9A=A9=EC=9E=90=20?= =?UTF-8?q?=EC=A0=80=EC=9E=A5=20=EA=B8=B0=EB=8A=A5=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 13 +- .../apache/coyote/http11/Http11Processor.java | 40 +++- tomcat/src/main/resources/static/index.html | 192 +++++++++--------- 3 files changed, 139 insertions(+), 106 deletions(-) diff --git a/README.md b/README.md index b1ecd411e0..70be385f28 100644 --- a/README.md +++ b/README.md @@ -5,10 +5,13 @@ - [x] QueryString 파싱 기능을 추가한다. - [x] 로그인 기능(/login)을 구현한다. - [x] 로그인의 성공하면 http status 302를 반환한다. + - [x] 로그인에 성공하면 세션에 사용자를 저장한다. + - [x] 로그인에 성공하면 JSESSIONID를 Set-Cookie 헤더에 담아 보낸다. + - [x] 로그인에 성공한 상태에서 로그인 페이지로 접근하면 index.html로 리다이렉트한다. - [x] location header에 리다이렉트할 url을 추가한다. - - [x] 로그인에 실패하는 경우 401.html로 리다리엑트한다. -- [ ] 회원가입 기능(/register)을 구현한다. - - [ ] 회원가입 페이지의 경우 GET을 사용하여 보여준다. - - [ ] 회원가입의 경우 POST를 사용한다. - - [ ] 회원가입을 완료하는 경우 index.html로 리다이렉트한다. + - [x] 로그인에 실패하는 경우 401.html로 리다이렉트한다. +- [x] 회원가입 기능(/register)을 구현한다. + - [x] 회원가입 페이지의 경우 GET을 사용하여 보여준다. + - [x] 회원가입의 경우 POST를 사용한다. + - [x] 회원가입을 완료하는 경우 index.html로 리다이렉트한다. diff --git a/tomcat/src/main/java/org/apache/coyote/http11/Http11Processor.java b/tomcat/src/main/java/org/apache/coyote/http11/Http11Processor.java index be82fde9e4..48a5369a8f 100644 --- a/tomcat/src/main/java/org/apache/coyote/http11/Http11Processor.java +++ b/tomcat/src/main/java/org/apache/coyote/http11/Http11Processor.java @@ -13,8 +13,10 @@ import nextstep.jwp.exception.UncheckedServletException; import nextstep.jwp.model.User; import org.apache.coyote.Processor; +import org.apache.coyote.http11.common.HttpCookie; import org.apache.coyote.http11.common.HttpMethod; import org.apache.coyote.http11.common.HttpStatus; +import org.apache.coyote.http11.common.Session; import org.apache.coyote.http11.common.SessionManager; import org.apache.coyote.http11.request.RequestBody; import org.apache.coyote.http11.request.RequestHeader; @@ -27,6 +29,9 @@ public class Http11Processor implements Runnable, Processor { private static final Logger LOG = LoggerFactory.getLogger(Http11Processor.class); + private static final String INDEX_PAGE = "/index.html"; + private static final String REGISTER_PAGE = "/register.html"; + private static final String LOGIN_PAGE = "/login.html"; private final Socket connection; private final HttpResponseGenerator httpResponseGenerator = new HttpResponseGenerator(); @@ -93,7 +98,7 @@ private ResponseEntity handleRequest( LOG.info("request uri: {}", requestLine.getUri()); final String path = requestLine.parseUriWithOutQueryString(); if (path.equals("/login")) { - return login(requestLine, requestBody); + return login(requestLine, requestHeader, requestBody); } if (path.equals("/register")) { return register(requestLine, requestBody); @@ -101,25 +106,40 @@ private ResponseEntity handleRequest( return new ResponseEntity(HttpStatus.OK, path); } - private ResponseEntity login(final RequestLine requestLine, final RequestBody requestBody) { + private ResponseEntity login( + final RequestLine requestLine, + final RequestHeader requestHeader, + final RequestBody requestBody + ) { if (requestLine.getHttpMethod() == HttpMethod.GET) { - return new ResponseEntity(HttpStatus.OK, "/login.html"); + final HttpCookie httpCookie = requestHeader.parseCookie(); + final Session session = sessionManager.findSession(httpCookie.get("JSESSIONID")); + if (session != null) { + return new ResponseEntity(HttpStatus.FOUND, INDEX_PAGE); + } + return new ResponseEntity(HttpStatus.OK, LOGIN_PAGE); } final String account = requestBody.get("account"); final String password = requestBody.get("password"); return InMemoryUserRepository.findByAccount(account) .filter(user -> user.checkPassword(password)) - .map(user -> { - final ResponseEntity responseEntity = new ResponseEntity(HttpStatus.FOUND, "/index.html"); - responseEntity.setCookie("JSESSIONID", UUID.randomUUID().toString()); - return responseEntity; - }) + .map(this::loginSuccess) .orElseGet(() -> new ResponseEntity(HttpStatus.UNAUTHORIZED, "/401.html")); } + private ResponseEntity loginSuccess(final User user) { + final String uuid = UUID.randomUUID().toString(); + final ResponseEntity responseEntity = new ResponseEntity(HttpStatus.FOUND, INDEX_PAGE); + responseEntity.setCookie("JSESSIONID", uuid); + final Session session = new Session(uuid); + session.setAttribute("user", user); + sessionManager.add(session); + return responseEntity; + } + private ResponseEntity register(final RequestLine requestLine, final RequestBody requestBody) { if (requestLine.getHttpMethod() == HttpMethod.GET) { - return new ResponseEntity(HttpStatus.OK, "/register.html"); + return new ResponseEntity(HttpStatus.OK, REGISTER_PAGE); } final String account = requestBody.get("account"); @@ -130,6 +150,6 @@ private ResponseEntity register(final RequestLine requestLine, final RequestBody final String password = requestBody.get("password"); final String email = requestBody.get("email"); InMemoryUserRepository.save(new User(account, password, email)); - return new ResponseEntity(HttpStatus.FOUND, "/index.html"); + return new ResponseEntity(HttpStatus.FOUND, INDEX_PAGE); } } diff --git a/tomcat/src/main/resources/static/index.html b/tomcat/src/main/resources/static/index.html index 18ac924d4e..48ca95e033 100644 --- a/tomcat/src/main/resources/static/index.html +++ b/tomcat/src/main/resources/static/index.html @@ -1,106 +1,116 @@ - - - - - - - 대시보드 - - - - - +
+
+ +
+
+
+
+

대시보드

+ +
+
+
+
+ + Bar Chart
-
-
-
- - Pie Chart -
-
-
+
+
-
- +
+
+ + + + + + + + + + +