-
Notifications
You must be signed in to change notification settings - Fork 606
hw-jv-web-security-solution-andrey-kiyas #667
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
base: master
Are you sure you want to change the base?
Changes from 1 commit
b6cd4e7
71c1722
a1d57d1
43e20b0
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,41 @@ | ||
| package taxi.controller; | ||
|
|
||
| import java.io.IOException; | ||
| import javax.servlet.ServletException; | ||
| import javax.servlet.http.HttpServlet; | ||
| import javax.servlet.http.HttpServletRequest; | ||
| import javax.servlet.http.HttpServletResponse; | ||
| import javax.servlet.http.HttpSession; | ||
| import taxi.exception.AuthenticationException; | ||
| import taxi.lib.Injector; | ||
| import taxi.model.Driver; | ||
| import taxi.service.AuthenticationService; | ||
|
|
||
| public class LoginController extends HttpServlet { | ||
| private static final Injector injector = Injector.getInstance("taxi"); | ||
| private final AuthenticationService authenticationService = | ||
| (AuthenticationService) injector.getInstance(AuthenticationService.class); | ||
|
|
||
| @Override | ||
| protected void doGet(HttpServletRequest req, HttpServletResponse resp) | ||
| throws ServletException, IOException { | ||
| req.getRequestDispatcher("/WEB-INF/views/login.jsp").forward(req, resp); | ||
| } | ||
|
|
||
| @Override | ||
| protected void doPost(HttpServletRequest req, HttpServletResponse resp) | ||
| throws ServletException, IOException { | ||
| String login = req.getParameter("login"); | ||
| String password = req.getParameter("password"); | ||
|
|
||
| try { | ||
| Driver driver = authenticationService.login(login, password); | ||
| HttpSession session = req.getSession(); | ||
| session.setAttribute("driver_id", driver.getId()); | ||
| resp.sendRedirect("/index"); | ||
| } catch (AuthenticationException e) { | ||
| req.setAttribute("errorMsg", e.getMessage()); | ||
| req.getRequestDispatcher("/WEB-INF/views/login.jsp").forward(req, resp); | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| package taxi.controller; | ||
|
|
||
| import java.io.IOException; | ||
| import javax.servlet.ServletException; | ||
| import javax.servlet.http.HttpServlet; | ||
| import javax.servlet.http.HttpServletRequest; | ||
| import javax.servlet.http.HttpServletResponse; | ||
|
|
||
| public class LogoutController extends HttpServlet { | ||
| @Override | ||
| protected void doGet(HttpServletRequest req, HttpServletResponse resp) | ||
| throws ServletException, IOException { | ||
| req.getSession().invalidate(); | ||
| resp.sendRedirect("/login"); | ||
|
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. lets add contextPath to all sendRedirect
Author
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. Done, thanks! 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 |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| package taxi.controller.car; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.List; | ||
| import javax.servlet.ServletException; | ||
| import javax.servlet.http.HttpServlet; | ||
| import javax.servlet.http.HttpServletRequest; | ||
| import javax.servlet.http.HttpServletResponse; | ||
| import taxi.lib.Injector; | ||
| import taxi.model.Car; | ||
| import taxi.service.CarService; | ||
| import taxi.service.DriverService; | ||
|
|
||
| public class GetMyCurrentCarsController extends HttpServlet { | ||
| private static final Injector injector = Injector.getInstance("taxi"); | ||
| private final DriverService driverService = | ||
| (DriverService) injector.getInstance(DriverService.class); | ||
| private final CarService carService = (CarService) injector.getInstance(CarService.class); | ||
|
|
||
| @Override | ||
| protected void doGet(HttpServletRequest req, HttpServletResponse resp) | ||
| throws ServletException, IOException { | ||
| Long driverId = (Long) req.getSession().getAttribute("driver_id"); | ||
| List<Car> allCarsByDriver = carService.getAllByDriver(driverId); | ||
| req.setAttribute("cars", allCarsByDriver); | ||
| req.getRequestDispatcher("/WEB-INF/views/cars/all.jsp").forward(req, resp); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,8 @@ | ||
| package taxi.dao; | ||
|
|
||
| import java.util.Optional; | ||
| import taxi.model.Driver; | ||
|
|
||
| public interface DriverDao extends GenericDao<Driver> { | ||
| Optional<Driver> findByLogin(String login); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| package taxi.exception; | ||
|
|
||
| public class AuthenticationException extends Exception { | ||
| public AuthenticationException(String message) { | ||
| super(message); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| package taxi.service; | ||
|
|
||
| import taxi.exception.AuthenticationException; | ||
| import taxi.model.Driver; | ||
|
|
||
| public interface AuthenticationService { | ||
| Driver login(String login, String password) throws AuthenticationException; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| package taxi.service; | ||
|
|
||
| import java.util.Optional; | ||
| import taxi.exception.AuthenticationException; | ||
| import taxi.lib.Inject; | ||
| import taxi.lib.Service; | ||
| import taxi.model.Driver; | ||
|
|
||
| @Service | ||
| public class AuthenticationServiceImpl implements AuthenticationService { | ||
| @Inject | ||
| private DriverService driverService; | ||
|
|
||
| @Override | ||
| public Driver login(String login, String password) throws AuthenticationException { | ||
| Optional<Driver> driverDaoByLogin = driverService.findByLogin(login); | ||
| if (password.equals(driverDaoByLogin.get().getPassword())) { | ||
| return driverDaoByLogin.get(); | ||
| } | ||
| throw new AuthenticationException("Login or Password is incorrect."); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,8 @@ | ||
| package taxi.service; | ||
|
|
||
| import java.util.Optional; | ||
| import taxi.model.Driver; | ||
|
|
||
| public interface DriverService extends GenericService<Driver> { | ||
| Optional<Driver> findByLogin(String login); | ||
| } |

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.
lets add contextPath
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.
Yes? Done.