-
Notifications
You must be signed in to change notification settings - Fork 305
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
[MVC 구현하기 - 3단계] 준팍(박준현) 미션 제출합니다. #595
Changes from 5 commits
2d05823
78d039f
062535b
45e328f
90a6330
440925c
624d9c0
2aa0595
e35c180
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package com.techcourse.controller; | ||
|
||
|
||
import context.org.springframework.stereotype.Controller; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import web.org.springframework.web.bind.annotation.RequestMapping; | ||
import web.org.springframework.web.bind.annotation.RequestMethod; | ||
import webmvc.org.springframework.web.servlet.ModelAndView; | ||
import webmvc.org.springframework.web.servlet.view.JspView; | ||
|
||
@Controller | ||
public class IndexController { | ||
|
||
@RequestMapping(value = "/", method = RequestMethod.GET) | ||
public ModelAndView index(HttpServletRequest request, HttpServletResponse response) { | ||
return new ModelAndView(new JspView("/index.jsp")); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,36 +2,69 @@ | |
|
||
import com.techcourse.domain.User; | ||
import com.techcourse.repository.InMemoryUserRepository; | ||
import context.org.springframework.stereotype.Controller; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import webmvc.org.springframework.web.servlet.mvc.asis.Controller; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import web.org.springframework.web.bind.annotation.RequestMapping; | ||
import web.org.springframework.web.bind.annotation.RequestMethod; | ||
import webmvc.org.springframework.web.servlet.ModelAndView; | ||
import webmvc.org.springframework.web.servlet.view.JspView; | ||
|
||
public class LoginController implements Controller { | ||
@Controller | ||
public class LoginController { | ||
|
||
private static final Logger log = LoggerFactory.getLogger(LoginController.class); | ||
|
||
@Override | ||
public String execute(final HttpServletRequest req, final HttpServletResponse res) throws Exception { | ||
if (UserSession.isLoggedIn(req.getSession())) { | ||
return "redirect:/index.jsp"; | ||
public static final String INDEX_JSP = "redirect:/index.jsp"; | ||
public static final String REDIRECT_401_JSP = "redirect:/401.jsp"; | ||
|
||
@RequestMapping(value = "/login", method = RequestMethod.GET) | ||
public ModelAndView showLoginPage(HttpServletRequest req, HttpServletResponse res) { | ||
|
||
String viewName = UserSession.getUserFrom(req.getSession()) | ||
.map(user -> { | ||
log.info("logged in {}", user.getAccount()); | ||
return INDEX_JSP; | ||
}) | ||
.orElse("/login.jsp"); | ||
|
||
return new ModelAndView(new JspView(viewName)); | ||
} | ||
|
||
@RequestMapping(value = "/login", method = RequestMethod.POST) | ||
public ModelAndView login(HttpServletRequest request, HttpServletResponse res) { | ||
if (UserSession.isLoggedIn(request.getSession())) { | ||
return new ModelAndView(new JspView(INDEX_JSP)); | ||
} | ||
|
||
return InMemoryUserRepository.findByAccount(req.getParameter("account")) | ||
String viewName = InMemoryUserRepository.findByAccount(request.getParameter("account")) | ||
.map(user -> { | ||
log.info("User : {}", user); | ||
return login(req, user); | ||
return validatePassword(request, user); | ||
}) | ||
.orElse("redirect:/401.jsp"); | ||
.orElse(REDIRECT_401_JSP); | ||
|
||
return new ModelAndView(new JspView(viewName)); | ||
} | ||
|
||
@RequestMapping(value = "/logout", method = RequestMethod.GET) | ||
public ModelAndView logout(HttpServletRequest req, HttpServletResponse res) { | ||
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. 제리의 의견에 공감합니다. 두 기능을 하나의 컨트롤러로 합친 이유는, 두 기능은 서로 다른 하나만 구현하였을 때 |
||
final var session = req.getSession(); | ||
session.removeAttribute(UserSession.SESSION_KEY); | ||
|
||
return new ModelAndView(new JspView("redirect:/")); | ||
} | ||
|
||
private String login(final HttpServletRequest request, final User user) { | ||
if (user.checkPassword(request.getParameter("password"))) { | ||
private String validatePassword(final HttpServletRequest request, final User user) { | ||
String password = request.getParameter("password"); | ||
if (user.checkPassword(password)) { | ||
final var session = request.getSession(); | ||
session.setAttribute(UserSession.SESSION_KEY, user); | ||
return "redirect:/index.jsp"; | ||
return INDEX_JSP; | ||
} | ||
return "redirect:/401.jsp"; | ||
return REDIRECT_401_JSP; | ||
} | ||
|
||
} |
This file was deleted.
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package com.techcourse.controller; | ||
|
||
import com.techcourse.domain.User; | ||
import com.techcourse.repository.InMemoryUserRepository; | ||
import context.org.springframework.stereotype.Controller; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import web.org.springframework.web.bind.annotation.RequestMapping; | ||
import web.org.springframework.web.bind.annotation.RequestMethod; | ||
import webmvc.org.springframework.web.servlet.ModelAndView; | ||
import webmvc.org.springframework.web.servlet.view.JsonView; | ||
|
||
@Controller | ||
public class UserController { | ||
|
||
private static final Logger log = LoggerFactory.getLogger(UserController.class); | ||
|
||
@RequestMapping(value = "/api/user", method = RequestMethod.GET) | ||
public ModelAndView show(HttpServletRequest request, HttpServletResponse response) { | ||
final String account = request.getParameter("account"); | ||
log.debug("user id : {}", account); | ||
|
||
final ModelAndView modelAndView = new ModelAndView(new JsonView()); | ||
final User user = InMemoryUserRepository.findByAccount(account) | ||
.orElseThrow(); | ||
|
||
modelAndView.addObject("user", user); | ||
return modelAndView; | ||
} | ||
|
||
} |
This file was deleted.
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.
컨트롤러를 검색할 패키지를 주입받아 완벽히 의존성을 끊어주었군요👍