Skip to content
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

[토니] 3단계 - HTTP 웹서버 구현 미션 제출합니다. #186

Open
wants to merge 8 commits into
base: toneyparky
Choose a base branch
from

Conversation

toneyparky
Copy link

안녕하세요 화투 🙂
많이 늦었지만 돌아왔습니다~!

부족한 부분이 많습니다. 가감없이 리뷰 부탁드려요 😌

항상 감사합니다!

Copy link

@phs1116 phs1116 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

안녕하세요, 토니! 리뷰가 많이 늦었어요 죄송해요.
세션, 쿠키 구현 관련해서 추상화와 각 영역의 책임에 대해 구분이 잘 안된거같아
피드백 몇가지 추가했어요,
참고 부탁드릴게요!

String sessionId = UserService.login(request);

response.addHttpHeader(LOCATION, "/index.html");
response.addHttpHeader(SET_COOKIE, "sessionId=" + sessionId + "; " + PATH);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Response 내에 Session이라는 객체를 한번 추상화해보고, 요청 혹은 응답 메시지를 만들 때 쿠키에 세션아이디를 만들어 저장하도록 해보면 어떨까요?

Comment on lines +50 to +51
String sessionId = SessionStorage.create(USER_ID, user.getUserId());
SessionStorage.loginBy(sessionId);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

표현계층에 가까운 세션 스토리지를 서비스 영역에서 만드는건 역할에 맞지 않은 것 같아요.


public static String create(String userId, Object value) {
HttpSession httpSession = new HttpSession();
httpSession.setAttributes(userId, value);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

httpServletRequest, httpServletResponse를 참고해서 세션을 어떤식으로 가져오고 처리하는지 살펴보면 좋을 것 같아요!
개발자가 스토리지를 통해 직접 저장하는것보다는, 세션만 관리하고 프레임워크가 자동으로 저장하도록 추상화 방향을 잡아보면 어떨까요?

Comment on lines +23 to +26
public static void loginBy(String sessionId) {
HttpSession httpSession = sessions.get(sessionId);
httpSession.setAttributes("logined", true);
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

loginBy, create는 세션 스토리지라는 이름과 걸맞지 않게 너무 비즈니스 요소가 많이 들어가있어요.
두개를 분리해서 생각하도록 고민해보면 좋을 것 같아요.

return getSessionId(request);
}

String sessionId = SessionStorage.create(USER_ID, user.getUserId());
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

서비스 영역에서 응답으로 세션 ID를 전달받고, 이를 표현영역에서 쿠키에 직접 세션키를 세팅하고 있는데요,
사실 세션이라는게 사용자 요청에 의해 세션키가 생성이 되는거라 굳이 이럴 필요가 없습니다.
이 부분에서도 추상화를 하여 요청과 응답을 만들 때 세팅이 되도록 해보면 어떨까요?

Comment on lines +15 to +48
private static final String USER_ID = "userId";
private static final String SESSION_ID = "sessionId=";
private static final String PASSWORD = "password";
private static final int SESSION_ID_KEY_LENGTH = 10;
private static final int SESSION_ID_LENGTH = 46;

public static void create(HttpRequest request) {
User user = new User(
request.getHttpBodyValueOf(USER_ID),
request.getHttpBodyValueOf(PASSWORD),
request.getHttpBodyValueOf("name"),
request.getHttpBodyValueOf("email")
);
DataBase.addUser(user);
}

public static List<User> findAll() {
return Collections.unmodifiableList(DataBase.findAll());
}

public static String login(HttpRequest request) {
User user = DataBase.findUserById(request.getHttpBodyValueOf(USER_ID));

if (user == null) {
throw new UserLoginException("해당되는 id의 사용자가 없습니다.");
}

if (!Objects.equals(request.getHttpBodyValueOf(PASSWORD), user.getPassword())) {
throw new UserLoginException("올바르지 않은 비밀번호입니다.");
}

if (isLogin(request)) {
return getSessionId(request);
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

표현계층에 대한 정보(요청)등은 표현에서만 책임지는게 좋을 것 같네요. :)
User등의 정보를 만들어서 전달하도록 해보면 어떨까요?

Comment on lines +64 to +68
private static String getSessionId(HttpRequest request) {
String cookie = request.getHttpHeaderParameterOf("Cookie");
return cookie.substring(cookie.indexOf(SESSION_ID) + SESSION_ID_KEY_LENGTH,
cookie.indexOf(SESSION_ID) + SESSION_ID_LENGTH);
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

세션아이디를 굳이 일일이 가져와서 쳐리하지 않도록 해보아요! :)

Comment on lines +56 to +60
if (request.hasHttpHeaderParameterOf("Cookie")) {
String sessionId = getSessionId(request);
HttpSession httpSession = SessionStorage.findBy(sessionId);
return (boolean)httpSession.getAttribute("logined");
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

쿠키에 대해서도 추상화를 해보면 좋을것같아요. :)

logger.debug("error : {}", e.getMessage());

response.addHttpHeader(LOCATION, "/user/login_failed.html");
response.addHttpHeader(SET_COOKIE, "logined=false; " + PATH);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

헤더에 직접 쿠키를 세팅하지말고, 쿠키 또한 추상화를 해보면 어떨까요?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants