Skip to content

Commit

Permalink
refactor: 로그인 검증을 SessionManager 가 담당하도록 변경
Browse files Browse the repository at this point in the history
  • Loading branch information
BGuga committed Sep 3, 2023
1 parent c3c3853 commit fa23d39
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 87 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package org.apache.coyote.http11.controller;

import jakarta.servlet.http.HttpSession;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.apache.coyote.http11.request.Cookies;
import org.apache.coyote.http11.request.Request;
import org.apache.coyote.http11.request.ResponseBody;
import org.apache.coyote.http11.response.Response;
Expand All @@ -29,7 +27,7 @@ public LoginController(LoginService loginService) {

@Override
public Response<String> handle(Request request) {
if (loggedIn(request)) {
if (SessionManager.loggedIn(request)) {
return Response.status(302)
.addHeader(LOCATION_HEADER, "/index.html")
.build();
Expand All @@ -48,24 +46,6 @@ public Response<String> handle(Request request) {
.build();
}

private boolean loggedIn(Request request) {
Optional<Cookies> cookie = request.getRequestHeaders().getCookie();
if (cookie.isPresent()) {
return checkSession(cookie.get());
}
return false;
}

private boolean checkSession(Cookies cookies) {
Optional<String> sessionCookie = cookies.getSessionCookie();
if (sessionCookie.isPresent()) {
String sessionId = sessionCookie.get();
HttpSession session = SessionManager.getInstance().findSession(sessionId);
return session != null;
}
return false;
}

private Optional<String> login(Request request) {
Map<String, String> bodyData = convertBody(request.getResponseBody());
String account = bodyData.get(ACCOUNT);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
package org.apache.coyote.http11.controller;

import jakarta.servlet.http.HttpSession;
import java.util.Optional;
import org.apache.coyote.http11.request.Cookies;
import org.apache.coyote.http11.request.Request;
import org.apache.coyote.http11.response.Response;
import org.apache.coyote.http11.session.SessionManager;
Expand All @@ -13,7 +10,7 @@ public class LoginViewController implements Controller {

@Override
public Response handle(Request request) {
if (loggedIn(request)) {
if (SessionManager.loggedIn(request)) {
return Response.status(302)
.addHeader(LOCATION_HEADER, "/index.html")
.build();
Expand All @@ -22,22 +19,4 @@ public Response handle(Request request) {
response.responseView("/login.html");
return response;
}

private boolean loggedIn(Request request) {
Optional<Cookies> cookie = request.getRequestHeaders().getCookie();
if (cookie.isPresent()) {
return checkSession(cookie.get());
}
return false;
}

private boolean checkSession(Cookies cookies) {
Optional<String> sessionCookie = cookies.getSessionCookie();
if (sessionCookie.isPresent()) {
String sessionId = sessionCookie.get();
HttpSession session = SessionManager.getInstance().findSession(sessionId);
return session != null;
}
return false;
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
package org.apache.coyote.http11.controller;

import jakarta.servlet.http.HttpSession;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.apache.coyote.http11.exception.MemberAlreadyExistsException;
import org.apache.coyote.http11.request.Cookies;
import org.apache.coyote.http11.request.Request;
import org.apache.coyote.http11.request.ResponseBody;
import org.apache.coyote.http11.response.Response;
Expand All @@ -32,7 +29,7 @@ public SignUpController(LoginService loginService) {
@Override
public Response handle(Request request) {
try {
if (loggedIn(request)) {
if (SessionManager.loggedIn(request)) {
return Response.status(302)
.addHeader(LOCATION_HEADER, "/index.html")
.build();
Expand All @@ -49,24 +46,6 @@ public Response handle(Request request) {
}
}

private boolean loggedIn(Request request) {
Optional<Cookies> cookie = request.getRequestHeaders().getCookie();
if (cookie.isPresent()) {
return checkSession(cookie.get());
}
return false;
}

private boolean checkSession(Cookies cookies) {
Optional<String> sessionCookie = cookies.getSessionCookie();
if (sessionCookie.isPresent()) {
String sessionId = sessionCookie.get();
HttpSession session = SessionManager.getInstance().findSession(sessionId);
return session != null;
}
return false;
}

private String signUp(Request request) {
Map<String, String> bodyData = convertBody(request.getResponseBody());
String account = bodyData.get(ACCOUNT);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
package org.apache.coyote.http11.controller;

import jakarta.servlet.http.HttpSession;
import java.util.Optional;
import org.apache.coyote.http11.request.Cookies;
import org.apache.coyote.http11.request.Request;
import org.apache.coyote.http11.response.Response;
import org.apache.coyote.http11.session.SessionManager;
Expand All @@ -13,7 +10,7 @@ public class SignUpViewController implements Controller {

@Override
public Response handle(Request request) {
if (loggedIn(request)) {
if (SessionManager.loggedIn(request)) {
return Response.status(302)
.addHeader(LOCATION_HEADER, "/index.html")
.build();
Expand All @@ -22,22 +19,4 @@ public Response handle(Request request) {
response.responseView("/register.html");
return response;
}

private boolean loggedIn(Request request) {
Optional<Cookies> cookie = request.getRequestHeaders().getCookie();
if (cookie.isPresent()) {
return checkSession(cookie.get());
}
return false;
}

private boolean checkSession(Cookies cookies) {
Optional<String> sessionCookie = cookies.getSessionCookie();
if (sessionCookie.isPresent()) {
String sessionId = sessionCookie.get();
HttpSession session = SessionManager.getInstance().findSession(sessionId);
return session != null;
}
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
import jakarta.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import org.apache.catalina.Manager;
import org.apache.coyote.http11.request.Cookies;
import org.apache.coyote.http11.request.Request;

public class SessionManager implements Manager {

Expand All @@ -17,6 +20,24 @@ public static SessionManager getInstance() {
return instance;
}

public static boolean loggedIn(Request request) {
Optional<Cookies> cookie = request.getRequestHeaders().getCookie();
if (cookie.isPresent()) {
return checkSession(cookie.get());
}
return false;
}

private static boolean checkSession(Cookies cookies) {
Optional<String> sessionCookie = cookies.getSessionCookie();
if (sessionCookie.isPresent()) {
String sessionId = sessionCookie.get();
HttpSession session = SessionManager.getInstance().findSession(sessionId);
return session != null;
}
return false;
}

@Override
public void add(HttpSession session) {
SESSIONS.put(session.getId(), session);
Expand Down

0 comments on commit fa23d39

Please sign in to comment.