-
Notifications
You must be signed in to change notification settings - Fork 606
web security hw #663
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
RostyslavOnysh
wants to merge
13
commits into
mate-academy:master
Choose a base branch
from
RostyslavOnysh:secutiryWebBranch
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
web security hw #663
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
3fad3f9
In the drivers table, I added login and password. There was an error …
RostyslavOnysh b4eaf2b
added a filter here. Everything else is marked with an annotation
RostyslavOnysh b2074e7
added login and password here
RostyslavOnysh bc0172c
Without any significant changes
RostyslavOnysh 16bdcd2
added login and password here
RostyslavOnysh 9f36df9
custom exception
RostyslavOnysh 10d7ed9
controller classes
RostyslavOnysh 9ab82b2
interfaces and their implementations
RostyslavOnysh e00cd4f
filter class and database connection
RostyslavOnysh 27bbd19
Update src/main/java/taxi/controller/LoginController.java
RostyslavOnysh 74af436
The changes have been made according to the comments
RostyslavOnysh 88e3b1a
Merge branch 'secutiryWebBranch' of github.com:RostyslavOnysh/jv-web-…
RostyslavOnysh 1d65812
fix checkstyle
RostyslavOnysh 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,43 @@ | ||
| 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 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(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 | ||
| protected void doGet(HttpServletRequest req, HttpServletResponse resp) | ||
| throws ServletException, IOException { | ||
| req.getSession().invalidate(); | ||
| resp.sendRedirect(req.getContextPath() + "/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
27 changes: 27 additions & 0 deletions
27
src/main/java/taxi/controller/car/GetMyCurrentCarController.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,27 @@ | ||
| 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 javax.servlet.http.HttpSession; | ||
| import taxi.lib.Injector; | ||
| import taxi.model.Car; | ||
| import taxi.service.CarService; | ||
|
|
||
| public class GetMyCurrentCarController extends HttpServlet { | ||
| private static 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 id = (Long) session.getAttribute("driver_id"); | ||
| List<Car> cars = carService.getAllByDriver(id); | ||
| req.setAttribute("cars",cars); | ||
| 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
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,47 @@ | ||
| package taxi.filters; | ||
|
|
||
| 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("/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,22 @@ | ||
| package taxi.service; | ||
|
|
||
| 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 { | ||
| Driver driver = driverService.findByLogin(login); | ||
| if (driver != null && driver.getPassword().equals(password)) { | ||
| return driver; | ||
| } | ||
| 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
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.
You are trying to get the 'password' field from the result set, but 'password' field is not included in SQL query. You need to add 'password' in your SQL query.