[피오 & 노리] 웹서버 4단계 - 쿠키를 이용한 로그인 구현#67
Open
NB993 wants to merge 4 commits intocodesquad-members-2022:pio-norifrom
Open
[피오 & 노리] 웹서버 4단계 - 쿠키를 이용한 로그인 구현#67NB993 wants to merge 4 commits intocodesquad-members-2022:pio-norifrom
NB993 wants to merge 4 commits intocodesquad-members-2022:pio-norifrom
Conversation
- 로그인이 성공하면 index.html로 이동하고, 로그인이 실패하면 /user/login_failed.html로 이동시킨다. - 아이디와 비밀번호가 같은지를 확인해서 로그인이 성공하면 응답 header의 Set-Cookie 값을 sessionId로 지정해준다. 값은 UUID로 생성한다.
- 로그아웃 요청이 오면 쿠키를 만료시킨다.
requestLine 정보를 ReqeustLine이 파싱하도록 함.
- 중복된 userId로 회원가입 요청이 들어올시, 예외를 발생시킨 후 /user/form.html로 이동시킨다. - HttpResponse클래스의 리다이렉션 메서드의 재활용성을 높이기 위해 location을 매개변수로 받는다.
ksundong
requested changes
Apr 2, 2022
ksundong
left a comment
There was a problem hiding this comment.
안녕하세요. 피오 & 노리, 리뷰어 dion 입니다.
4단계 미션을 수행하느라 고생하셨습니다.
기능은 문제가 없으나, 보다 좋은 코드를 작성할 필요가 있다는 생각이 들어서 변경요청 드립니다. 고생하셨습니다.
|
|
||
| public class SessionDataBase { | ||
|
|
||
| private static final Map<String, String> SESSIONS = new HashMap<>(); |
Comment on lines
+10
to
+16
| public static void save(String sessionId, String userId) { | ||
| SESSIONS.put(sessionId, userId); | ||
| } | ||
|
|
||
| public static void remove(String sessionId) { | ||
| SESSIONS.remove(sessionId); | ||
| } |
| String[] requestLineSplit = requestLine.split(" "); | ||
| this.requestLine = new RequestLine(requestLineSplit[0], requestLineSplit[1], requestLineSplit[2]); | ||
| this.requestLine = new RequestLine(requestLine); | ||
| this.headers = IOUtils.readRequestHeader(br); |
Comment on lines
+28
to
+30
| } catch (IOException e) { | ||
| log.error(e.getMessage()); | ||
| } |
Comment on lines
24
to
+47
| dos.writeBytes("HTTP/1.1 302 FOUND \r\n"); | ||
| dos.writeBytes("Location: http://localhost:8080/index.html\r\n"); | ||
| dos.writeBytes("Location: http://localhost:8080" + path + "\r\n"); | ||
| dos.writeBytes("Set-Cookie: sessionId=" + cookie + "; Max-Age=-1; path=/"); | ||
| dos.writeBytes("\r\n"); | ||
| } catch (IOException e) { | ||
| log.error(e.getMessage()); | ||
| } | ||
| } | ||
|
|
||
| public void response302WithCookieHeader(String path, String cookie) { | ||
| try { | ||
| dos.writeBytes("HTTP/1.1 302 FOUND \r\n"); | ||
| dos.writeBytes("Location: http://localhost:8080" + path + "\r\n"); | ||
| dos.writeBytes("Set-Cookie: sessionId=" + cookie + "; path=/"); | ||
| dos.writeBytes("\r\n"); | ||
| } catch (IOException e) { | ||
| log.error(e.getMessage()); | ||
| } | ||
| } | ||
|
|
||
| public void response302Header(String path) { | ||
| try { | ||
| dos.writeBytes("HTTP/1.1 302 FOUND \r\n"); | ||
| dos.writeBytes("Location: http://localhost:8080" + path + "\r\n"); |
| try { | ||
| dos.writeBytes("HTTP/1.1 302 FOUND \r\n"); | ||
| dos.writeBytes("Location: http://localhost:8080/index.html\r\n"); | ||
| dos.writeBytes("Location: http://localhost:8080" + path + "\r\n"); |
There was a problem hiding this comment.
이 객체가 stream 을 직접 가지고서 응답을 반환하는 것은 도메인을 담당하는 객체가 출력을 하는 것과 동일합니다. 지양해주세요.
Comment on lines
+39
to
+86
| HttpResponse httpResponse = new HttpResponse(out); | ||
|
|
||
| if (httpRequest.getPath().contains("/user/create")) { | ||
| if (httpRequest.getPath().equals("/user/create")) { | ||
| User user = new User( | ||
| httpRequest.getParameter("userId"), | ||
| httpRequest.getParameter("password"), | ||
| httpRequest.getParameter("name"), | ||
| httpRequest.getParameter("email") | ||
| ); | ||
| DataBase.addUser(user); | ||
| HttpResponse httpResponse = new HttpResponse(out); | ||
| httpResponse.response302Header(); | ||
|
|
||
| try { | ||
| DataBase.addUser(user); | ||
| httpResponse.response302Header("/index.html"); | ||
| } catch (IllegalArgumentException e) { | ||
| log.debug("exception: {}", e.getMessage()); | ||
| httpResponse.response302Header("/user/form.html"); | ||
| } | ||
| return; | ||
| } | ||
|
|
||
| if (httpRequest.getPath().equals("/user/login")) { | ||
| User user = DataBase.findUserById(httpRequest.getParameter("userId")); | ||
| if (user == null) { | ||
| httpResponse.response302Header("/user/login_failed.html"); | ||
| return; | ||
| } | ||
| if (!user.getPassword().equals(httpRequest.getParameter("password"))) { | ||
| httpResponse.response302Header("/user/login_failed.html"); | ||
| return; | ||
| } | ||
| String sessionId = UUID.randomUUID().toString(); | ||
| log.debug("return cookie: {}", sessionId); | ||
| SessionDataBase.save(sessionId, user.getUserId()); | ||
| httpResponse.response302WithCookieHeader("/index.html", sessionId); | ||
| return; | ||
| } | ||
|
|
||
| if (httpRequest.getPath().equals("/user/logout")) { | ||
| Map<String, String> cookies = HttpRequestUtils.parseCookies( | ||
| httpRequest.getHeader("Cookie")); | ||
| String sessionId = cookies.get("sessionId"); | ||
| log.debug("sessionId = {}", sessionId); | ||
| if (sessionId == null) { | ||
| httpResponse.response302Header("/index.html"); | ||
| return; | ||
| } | ||
| httpResponse.response302WithExpiredCookieHeader("/index.html", sessionId); | ||
| SessionDataBase.remove(sessionId); |
There was a problem hiding this comment.
메서드가 너무 길죠. 리팩토링 해주세요. depth 2단계는 여기서도 지양해주셔야 합니다.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
안녕하세요!! 피오 & 노리 입니다. 👬
"웹서버 4 단계 - 쿠키를 이용한 로그인 구현" 완료하여 pull request 요청드립니다.
감사합니다! 🙇
3단계 리뷰 항목
기능요구사항
프로그래밍 요구사항
추가 요구 사항