-
Notifications
You must be signed in to change notification settings - Fork 606
implemented authentication service using filter #662
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
Open
irynamatveieva
wants to merge
1
commit into
mate-academy:master
Choose a base branch
from
irynamatveieva:jv-web-security
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,42 @@ | ||
| package taxi.controller; | ||
|
|
||
| import java.io.IOException; | ||
| import javax.servlet.ServletException; | ||
| import javax.servlet.annotation.WebServlet; | ||
| 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; | ||
|
|
||
| @WebServlet(urlPatterns = "/login") | ||
| public class LoginController extends HttpServlet { | ||
| private static final Injector injector = Injector.getInstance("taxi"); | ||
| private final AuthenticationService authenticationService = (AuthenticationService) injector | ||
| .getInstance(AuthenticationService.class); | ||
|
|
||
| @Override | ||
| public void doGet(HttpServletRequest req, HttpServletResponse resp) | ||
| throws ServletException, IOException { | ||
| req.getRequestDispatcher("/WEB-INF/views/login.jsp").forward(req, resp); | ||
| } | ||
|
|
||
| @Override | ||
| public 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(req.getContextPath() + "/index"); | ||
| } catch (AuthenticationException e) { | ||
| req.setAttribute("errorMsg", e.getMessage()); | ||
| req.getRequestDispatcher("/WEB-INF/views/login.jsp").forward(req, resp); | ||
| } | ||
| } | ||
| } |
This file contains hidden or 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,18 @@ | ||
| package taxi.controller; | ||
|
|
||
| import java.io.IOException; | ||
| import javax.servlet.ServletException; | ||
| import javax.servlet.annotation.WebServlet; | ||
| import javax.servlet.http.HttpServlet; | ||
| import javax.servlet.http.HttpServletRequest; | ||
| import javax.servlet.http.HttpServletResponse; | ||
|
|
||
| @WebServlet(urlPatterns = "/logout") | ||
| public class LogoutController extends HttpServlet { | ||
| @Override | ||
| public void doGet(HttpServletRequest req, HttpServletResponse resp) | ||
| throws ServletException, IOException { | ||
| req.getSession().invalidate(); | ||
| resp.sendRedirect(req.getContextPath() + "/index"); | ||
| } | ||
| } |
This file contains hidden or 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
29 changes: 29 additions & 0 deletions
29
src/main/java/taxi/controller/driver/GetMyCurrentCarsController.java
This file contains hidden or 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,29 @@ | ||
| package taxi.controller.driver; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.List; | ||
| import javax.servlet.ServletException; | ||
| import javax.servlet.annotation.WebServlet; | ||
| import javax.servlet.http.HttpServlet; | ||
| import javax.servlet.http.HttpServletRequest; | ||
| import javax.servlet.http.HttpServletResponse; | ||
| import javax.servlet.http.HttpSession; | ||
| import taxi.lib.Injector; | ||
| import taxi.model.Car; | ||
| import taxi.service.CarService; | ||
|
|
||
| @WebServlet(urlPatterns = "/cars/all") | ||
| 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 { | ||
| HttpSession session = req.getSession(); | ||
| Long driverId = (Long) session.getAttribute("driver_id"); | ||
| List<Car> carsByDriver = carService.getAllByDriver(driverId); | ||
| req.setAttribute("car.drivers", carsByDriver); | ||
| req.getRequestDispatcher("/WEB-INF/views/cars/all.jsp").forward(req, resp); | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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 |
|---|---|---|
| @@ -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); | ||
| } |
This file contains hidden or 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 hidden or 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,7 @@ | ||
| package taxi.exception; | ||
|
|
||
| public class AuthenticationException extends Exception { | ||
| public AuthenticationException(String message) { | ||
| super(message); | ||
| } | ||
| } |
This file contains hidden or 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,42 @@ | ||
| package taxi.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 Set<String> allowedUrls = new HashSet<>(); | ||
|
|
||
| @Override | ||
| public void init(FilterConfig filterConfig) throws ServletException { | ||
| allowedUrls.add("/login"); | ||
| allowedUrls.add("/drivers/add"); | ||
| } | ||
|
|
||
| @Override | ||
| public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, | ||
| FilterChain filterChain) throws IOException, ServletException { | ||
| HttpServletRequest req = (HttpServletRequest) servletRequest; | ||
| HttpServletResponse resp = (HttpServletResponse) servletResponse; | ||
| HttpSession session = req.getSession(); | ||
| Long driverId = (Long) session.getAttribute("driver_id"); | ||
| if (driverId == null && allowedUrls.contains(req.getServletPath())) { | ||
| filterChain.doFilter(req, resp); | ||
| return; | ||
| } | ||
| if (driverId == null) { | ||
| resp.sendRedirect(req.getContextPath() + "/login"); | ||
| return; | ||
| } | ||
| filterChain.doFilter(req, resp); | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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,8 @@ | ||
| package taxi.service; | ||
|
|
||
| import taxi.exception.AuthenticationException; | ||
| import taxi.model.Driver; | ||
|
|
||
| public interface AuthenticationService { | ||
| Driver login(String login, String password) throws AuthenticationException; | ||
| } |
This file contains hidden or 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,25 @@ | ||
| 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> driver = driverService.findByLogin(login); | ||
| if (driver.isEmpty()) { | ||
| throw new AuthenticationException("Login or password was incorrect"); | ||
| } | ||
| if (driver.get().getPassword().equals(password)) { | ||
| return driver.get(); | ||
| } | ||
| throw new AuthenticationException("Login or password was incorrect"); | ||
| } | ||
| } |
This file contains hidden or 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 |
|---|---|---|
| @@ -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); | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
"Can't find driver" is not correct message. In case drives wasn't found you'd return empty Optional. Exception will be thrown if we couldn't obtain any results due to failure