-
Notifications
You must be signed in to change notification settings - Fork 31
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[피오 & 노리] 웹서버 4단계 - 쿠키를 이용한 로그인 구현 #67
base: pio-nori
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package db; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
public class SessionDataBase { | ||
|
||
private static final Map<String, String> SESSIONS = new HashMap<>(); | ||
|
||
public static void save(String sessionId, String userId) { | ||
SESSIONS.put(sessionId, userId); | ||
} | ||
|
||
public static void remove(String sessionId) { | ||
SESSIONS.remove(sessionId); | ||
} | ||
Comment on lines
+10
to
+16
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 메서드명 👍 |
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,8 +22,7 @@ public HttpRequest(BufferedReader br) throws IOException { | |
|
||
private void init() throws IOException { | ||
String requestLine = URLDecoder.decode(br.readLine(), StandardCharsets.UTF_8); | ||
String[] requestLineSplit = requestLine.split(" "); | ||
this.requestLine = new RequestLine(requestLineSplit[0], requestLineSplit[1], requestLineSplit[2]); | ||
this.requestLine = new RequestLine(requestLine); | ||
this.headers = IOUtils.readRequestHeader(br); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 또 |
||
this.parameters = parseParameter(); | ||
} | ||
|
@@ -48,4 +47,7 @@ public String getParameter(String key) { | |
return parameters.get(key); | ||
} | ||
|
||
public String getHeader(String header) { | ||
return headers.get(header); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,10 +19,32 @@ public HttpResponse(OutputStream out) { | |
} | ||
|
||
|
||
public void response302Header() { | ||
public void response302WithExpiredCookieHeader(String path, String cookie) { | ||
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. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 이 객체가 |
||
dos.writeBytes("Set-Cookie: sessionId=" + cookie + "; Max-Age=-1; path=/"); | ||
dos.writeBytes("\r\n"); | ||
} catch (IOException e) { | ||
log.error(e.getMessage()); | ||
} | ||
Comment on lines
+28
to
+30
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 이건 올바른 예외처리가 아닙니다. 지양해주세요. |
||
} | ||
|
||
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"); | ||
Comment on lines
24
to
+47
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 중복되는 코드가 많네요. 재사용을 고려할 필요는 없을까요? |
||
dos.writeBytes("\r\n"); | ||
} catch (IOException e) { | ||
log.error(e.getMessage()); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,20 @@ | ||
package webserver; | ||
|
||
import db.DataBase; | ||
import db.SessionDataBase; | ||
import java.io.BufferedReader; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.InputStreamReader; | ||
import java.io.OutputStream; | ||
import java.net.Socket; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.Map; | ||
import java.util.UUID; | ||
import model.User; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import util.HttpRequestUtils; | ||
|
||
public class RequestHandler extends Thread { | ||
|
||
|
@@ -32,21 +36,57 @@ public void run() { | |
new InputStreamReader(in, StandardCharsets.UTF_8)); | ||
|
||
HttpRequest httpRequest = new HttpRequest(br); | ||
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); | ||
Comment on lines
+39
to
+86
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 메서드가 너무 길죠. 리팩토링 해주세요. depth 2단계는 여기서도 지양해주셔야 합니다. |
||
return; | ||
} | ||
|
||
HttpResponse httpResponse = new HttpResponse(out); | ||
httpResponse.writeBody(httpRequest.getPath()); | ||
httpResponse.response200Header(); | ||
httpResponse.responseBody(); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
HashMap
을 사용해주신 이유는 무엇인가요?