diff --git a/src/main/java/de/hsel/spm/baudas/analysis/ShoppingTimes.java b/src/main/java/de/hsel/spm/baudas/analysis/ShoppingTimes.java index 4c4a697..ed99ee2 100644 --- a/src/main/java/de/hsel/spm/baudas/analysis/ShoppingTimes.java +++ b/src/main/java/de/hsel/spm/baudas/analysis/ShoppingTimes.java @@ -27,7 +27,7 @@ public class ShoppingTimes implements Analysis> { /** * get customer count at specific times. * - * @return Map of Day-Hour Combinations and the corresponding customer count + * @return Map of Day-Hour Combinations and the corresponding user count */ @Override public Map getResult() { diff --git a/src/main/java/de/hsel/spm/baudas/web/AuthenticationFilter.java b/src/main/java/de/hsel/spm/baudas/web/AuthenticationFilter.java new file mode 100644 index 0000000..d262553 --- /dev/null +++ b/src/main/java/de/hsel/spm/baudas/web/AuthenticationFilter.java @@ -0,0 +1,59 @@ +package de.hsel.spm.baudas.web; + +import javax.servlet.*; +import javax.servlet.annotation.WebFilter; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import javax.servlet.http.HttpSession; +import java.io.IOException; + +/** + * Filter implementation class AuthenticationFilter. + * + * @author Edgar Schkrob + */ + +@WebFilter(urlPatterns = {"/*"}) +public class AuthenticationFilter implements Filter{ + private ServletContext context; + + /** + * Called by the web container to indicate to a filter that it is being placed into service. This filter manages the authentication. + * @param fConfig This parameter provides access to everything the code needs to work. + * @throws ServletException Defines a general exception a servlet can throw when it encounters difficulty. + */ + + public void init(FilterConfig fConfig) throws ServletException { + this.context = fConfig.getServletContext(); + this.context.log("AuthenticationFilter initialized"); + } + + /** + * The doFilter method of the Filter is called by the container each time a request/response pair is passed through the chain due to a client request for a resource at the end of the chain. + * @param request This parameter provides access to everything the code needs to work. + * @param response This parameter provides access to everything the code needs to issue a response. + * @param chain This parameter allows passing request along the chain of potential handlers until one of them handles the request. + * @throws ServletException Defines a general exception a servlet can throw when it encounters difficulty. + * @throws IOException Signals that an I/O exception of some sort has occurred. This class is the general class of exceptions produced by failed or interrupted I/O operations. + */ + + public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { + HttpServletRequest req = (HttpServletRequest) request; + HttpServletResponse res = (HttpServletResponse) response; + HttpSession session = req.getSession(false); + String url = req.getRequestURI(); + if(url.contains("login") || url.contains("logo") || url.contains("js/")){ + chain.doFilter(request, response); + } else if (session == null || !((boolean) session.getAttribute("authentication"))) { //checking whether the session exists and if authentication succeed + this.context.log("Unauthorized access request"); + res.sendRedirect(req.getContextPath() + "/login.html"); + } else { + chain.doFilter(request, response); + + } + } + + public void destroy() { + //close any resources here + } +} \ No newline at end of file diff --git a/src/main/java/de/hsel/spm/baudas/web/LoginServlet.java b/src/main/java/de/hsel/spm/baudas/web/LoginServlet.java new file mode 100644 index 0000000..ce33667 --- /dev/null +++ b/src/main/java/de/hsel/spm/baudas/web/LoginServlet.java @@ -0,0 +1,46 @@ +package de.hsel.spm.baudas.web; + +import javax.servlet.RequestDispatcher; +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 java.io.IOException; +import java.io.PrintWriter; + +/** + * Servlet implementation class LoginServlet. + * + * @author Edgar Schkrob + */ + +@WebServlet("/login") +public class LoginServlet extends HttpServlet { + + private final String password = "SPM2019SS"; + + /** + * This is a Servlet that manages the Login and creates Sessions. + * @param request This parameter provides access to everything the code needs to work. + * @param response This parameter provides access to everything the code needs to issue a response. + * @throws ServletException Defines a general exception a servlet can throw when it encounters difficulty. + * @throws IOException Signals that an I/O exception of some sort has occurred. This class is the general class of exceptions produced by failed or interrupted I/O operations. + */ + + protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + String password = request.getParameter("password"); + if (this.password.equals(password)) { + HttpSession newSession = request.getSession(true); + newSession.setAttribute("authentication", true); + newSession.setMaxInactiveInterval(5*60*60); //setting session to expiry in 5 hours + response.sendRedirect("index.jsp"); + } else { + RequestDispatcher rd = getServletContext().getRequestDispatcher("/login.html"); + PrintWriter out = response.getWriter(); + out.println("Das eingegebene Passwort ist falsch."); + rd.include(request, response); + } + } +} diff --git a/src/main/java/de/hsel/spm/baudas/web/LogoutServlet.java b/src/main/java/de/hsel/spm/baudas/web/LogoutServlet.java new file mode 100644 index 0000000..165f851 --- /dev/null +++ b/src/main/java/de/hsel/spm/baudas/web/LogoutServlet.java @@ -0,0 +1,34 @@ +package de.hsel.spm.baudas.web; + +import java.io.IOException; + +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; + +/** + * Servlet implementation class LogoutServlet. + * + * @author Edgar Schkrob + */ + +@WebServlet("/logout") +public class LogoutServlet extends HttpServlet { + + /** + * This is a Servlet that manages the Logout and deletes Sessions. + * @param request This parameter provides access to everything the code needs to work. + * @param response This parameter provides access to everything the code needs to issue a response. + * @throws IOException Signals that an I/O exception of some sort has occurred. This class is the general class of exceptions produced by failed or interrupted I/O operations. + */ + + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException { + HttpSession session = request.getSession(false); + if(session != null){ + session.invalidate(); + } + response.sendRedirect(request.getContextPath() + "/"); + } +} diff --git a/src/main/webapp/preview/login.html b/src/main/webapp/login.html similarity index 76% rename from src/main/webapp/preview/login.html rename to src/main/webapp/login.html index 8538999..cf45487 100644 --- a/src/main/webapp/preview/login.html +++ b/src/main/webapp/login.html @@ -18,20 +18,23 @@
- +
- - lock_openLogin + +
+ + +
- + diff --git a/src/main/webapp/preview/index.html b/src/main/webapp/preview/index.html index 31a72e1..790c14b 100644 --- a/src/main/webapp/preview/index.html +++ b/src/main/webapp/preview/index.html @@ -29,11 +29,11 @@ diff --git a/src/main/webapp/preview/registration.html b/src/main/webapp/preview/registration.html index a8705ba..98cbb0e 100644 --- a/src/main/webapp/preview/registration.html +++ b/src/main/webapp/preview/registration.html @@ -37,7 +37,7 @@