-
Notifications
You must be signed in to change notification settings - Fork 302
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
163 additions
and
4 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
20 changes: 20 additions & 0 deletions
20
app/src/main/java/com/techcourse/controller/annotation/AnnotationHomeController.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package com.techcourse.controller.annotation; | ||
|
||
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 AnnotationHomeController { | ||
|
||
private static final String INDEX_JSP = "/index.jsp"; | ||
|
||
@RequestMapping(value = "/", method = RequestMethod.GET) | ||
public ModelAndView display(HttpServletRequest req, HttpServletResponse res) { | ||
return new ModelAndView(new JspView(INDEX_JSP)); | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
app/src/main/java/com/techcourse/controller/annotation/AnnotationLoginController.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package com.techcourse.controller.annotation; | ||
|
||
import com.techcourse.controller.LoginController; | ||
import com.techcourse.controller.UserSession; | ||
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.JspView; | ||
|
||
@Controller | ||
public class AnnotationLoginController { | ||
private static final Logger log = LoggerFactory.getLogger(LoginController.class); | ||
private static final String REDIRECT_INDEX_JSP = "redirect:/index.jsp"; | ||
private static final String REDIRECT_401_JSP = "redirect:/401.jsp"; | ||
private static final String LOGIN_JSP = "/login.jsp"; | ||
|
||
@RequestMapping(value = "/login/view", method = RequestMethod.GET) | ||
public ModelAndView display(HttpServletRequest req, HttpServletResponse res) { | ||
return UserSession.getUserFrom(req.getSession()) | ||
.map(user -> { | ||
log.info("logged in {}", user.getAccount()); | ||
return new ModelAndView(new JspView(REDIRECT_INDEX_JSP)); | ||
}) | ||
.orElse(new ModelAndView(new JspView(LOGIN_JSP))); | ||
} | ||
|
||
@RequestMapping(value = "/login", method = RequestMethod.POST) | ||
public ModelAndView login(HttpServletRequest req, HttpServletResponse res) { | ||
if (UserSession.isLoggedIn(req.getSession())) { | ||
return new ModelAndView(new JspView(REDIRECT_INDEX_JSP)); | ||
} | ||
|
||
ModelAndView modelAndView = InMemoryUserRepository.findByAccount(req.getParameter("account")) | ||
.map(user -> { | ||
log.info("User : {}", user); | ||
return checkPassword(req, user); | ||
}) | ||
.orElse(new ModelAndView(new JspView(REDIRECT_401_JSP))); | ||
modelAndView.addObject("id", req.getAttribute("id")); | ||
return modelAndView; | ||
} | ||
|
||
private ModelAndView checkPassword(final HttpServletRequest request, final User user) { | ||
if (user.checkPassword(request.getParameter("password"))) { | ||
final var session = request.getSession(); | ||
session.setAttribute(UserSession.SESSION_KEY, user); | ||
return new ModelAndView(new JspView(REDIRECT_INDEX_JSP)); | ||
} | ||
return new ModelAndView(new JspView(REDIRECT_401_JSP)); | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
app/src/main/java/com/techcourse/controller/annotation/AnnotationLogoutController.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package com.techcourse.controller.annotation; | ||
|
||
import com.techcourse.controller.UserSession; | ||
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 AnnotationLogoutController { | ||
|
||
private static final String REDIRECT_INDEX_JSP = "redirect:/index.jsp"; | ||
|
||
@RequestMapping(value = "/logout", method = RequestMethod.GET) | ||
public ModelAndView logout(HttpServletRequest req, HttpServletResponse res) { | ||
final var session = req.getSession(); | ||
session.removeAttribute(UserSession.SESSION_KEY); | ||
return new ModelAndView(new JspView(REDIRECT_INDEX_JSP)); | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
app/src/main/java/com/techcourse/controller/annotation/AnnotationRegisterController.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package com.techcourse.controller.annotation; | ||
|
||
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 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 AnnotationRegisterController { | ||
|
||
private static final String REDIRECT_INDEX_JSP = "redirect:/index.jsp"; | ||
|
||
@RequestMapping(value = "/register/view", method = RequestMethod.GET) | ||
public ModelAndView display(HttpServletRequest req, HttpServletResponse res) { | ||
return new ModelAndView(new JspView("/register.jsp")); | ||
} | ||
|
||
@RequestMapping(value = "/register", method = RequestMethod.POST) | ||
public ModelAndView register(HttpServletRequest req, HttpServletResponse res) { | ||
final var user = new User( | ||
req.getParameter("account"), | ||
req.getParameter("password"), | ||
req.getParameter("email")); | ||
InMemoryUserRepository.save(user); | ||
|
||
return new ModelAndView(new JspView(REDIRECT_INDEX_JSP)); | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
...c/main/java/webmvc/org/springframework/web/servlet/mvc/tobe/AnnotationHandlerAdapter.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package webmvc.org.springframework.web.servlet.mvc.tobe; | ||
|
||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import webmvc.org.springframework.web.servlet.HandlerAdapter; | ||
import webmvc.org.springframework.web.servlet.ModelAndView; | ||
|
||
public class AnnotationHandlerAdapter implements HandlerAdapter { | ||
|
||
@Override | ||
public boolean support(final Object handler) { | ||
return handler instanceof HandlerExecution; | ||
} | ||
|
||
@Override | ||
public ModelAndView handle(final Object handler, final HttpServletRequest request, | ||
final HttpServletResponse response) | ||
throws Exception { | ||
return ((HandlerExecution) handler).handle(request, response); | ||
} | ||
} |