-
Notifications
You must be signed in to change notification settings - Fork 606
add my solution #672
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?
add my solution #672
Changes from 1 commit
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 username = req.getParameter("login"); | ||||||
| String password = req.getParameter("password"); | ||||||
|
|
||||||
| try { | ||||||
| Driver driver = authenticationService.login(username, password); | ||||||
| HttpSession session = req.getSession(); | ||||||
| session.setAttribute("driver_id",driver.getId()); | ||||||
| resp.sendRedirect("/index"); | ||||||
| } catch (AuthenticationException e) { | ||||||
| req.setAttribute("errorMsg",e.getMessage()); | ||||||
|
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.
Suggested change
|
||||||
| 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("/index"); | ||
|
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. same |
||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| 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; | ||
|
|
||
| public class GetMyCurrentCarsController extends HttpServlet { | ||
| private static final Injector injector = Injector.getInstance("taxi"); | ||
| 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> carList = carService.getAllByDriver(driverId); | ||
| req.setAttribute("cars", carList); | ||
| req.getRequestDispatcher("/WEB-INF/views/cars/all.jsp").forward(req, resp); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -211,10 +211,14 @@ private Driver parseDriverFromResultSet(ResultSet resultSet) throws SQLException | |
| Long driverId = resultSet.getObject("id", Long.class); | ||
| String name = resultSet.getString("name"); | ||
| String licenseNumber = resultSet.getString("license_number"); | ||
| String login = resultSet.getString("login"); | ||
| String password = resultSet.getString("password"); | ||
|
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. Does it work? You from request do not pull these fields. |
||
| Driver driver = new Driver(); | ||
| driver.setId(driverId); | ||
| driver.setName(name); | ||
| driver.setLicenseNumber(licenseNumber); | ||
| driver.setLogin(login); | ||
| driver.setPassword(password); | ||
| return driver; | ||
| } | ||
|
|
||
|
|
||
| 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,27 @@ | ||
| package taxi.service; | ||
|
|
||
| import java.util.Optional; | ||
| import taxi.dao.DriverDao; | ||
| import taxi.exception.AuthenticationException; | ||
| import taxi.lib.Inject; | ||
| import taxi.lib.Service; | ||
| import taxi.model.Driver; | ||
|
|
||
| @Service | ||
| public class AuthenticationServiceImpl implements AuthenticationService { | ||
| @Inject | ||
| private DriverDao driverDao; | ||
|
|
||
| @Override | ||
| public Driver login(String login, String password) throws AuthenticationException { | ||
|
|
||
|
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. |
||
| Optional<Driver> driver = driverDao.findByLogin(login); | ||
| if (driver.isEmpty()) { | ||
| throw new AuthenticationException("username or password was incorrect"); | ||
| } | ||
| if (driver.get().getPassword().equals(password)) { | ||
| return driver.get(); | ||
| } | ||
| throw new AuthenticationException("username or password was incorrect"); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| package taxi.web.filter; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.HashSet; | ||
| import java.util.Set; | ||
| import javax.servlet.Filter; | ||
| import javax.servlet.FilterChain; | ||
| import javax.servlet.FilterConfig; | ||
| import javax.servlet.ServletException; | ||
| import javax.servlet.ServletRequest; | ||
| import javax.servlet.ServletResponse; | ||
| import javax.servlet.http.HttpServletRequest; | ||
| import javax.servlet.http.HttpServletResponse; | ||
| import javax.servlet.http.HttpSession; | ||
|
|
||
| public class AuthenticationFilter implements Filter { | ||
| private final Set<String> allowedUrls = new HashSet<>(); | ||
|
|
||
| @Override | ||
| public void init(FilterConfig filterConfig) { | ||
| allowedUrls.add("/login"); | ||
| allowedUrls.add("/drivers/add"); | ||
| } | ||
|
|
||
| @Override | ||
| public void doFilter(ServletRequest servletRequest, | ||
| ServletResponse servletResponse, FilterChain filterChain) | ||
| throws IOException, ServletException { | ||
| HttpServletRequest request = (HttpServletRequest) servletRequest; | ||
| HttpServletResponse response = (HttpServletResponse) servletResponse; | ||
| HttpSession session = request.getSession(); | ||
|
|
||
| Long driverId = (Long) session.getAttribute("driver_id"); | ||
| if (driverId == null && allowedUrls.contains(request.getServletPath())) { | ||
|
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. |
||
| filterChain.doFilter(request, response); | ||
| return; | ||
| } | ||
|
|
||
| if (driverId == null) { | ||
| response.sendRedirect(request.getContextPath() + "/login"); | ||
| return; | ||
| } | ||
| filterChain.doFilter(request, response); | ||
| } | ||
| } | ||


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.
dont forget about contextPath