From c8da564c8f9adb656d6974c82786e3d77d93b08d Mon Sep 17 00:00:00 2001 From: Emanuel Date: Fri, 13 May 2022 12:53:51 -0400 Subject: [PATCH] Login updated --- src/main/java/com/roshka/proyectofinal/LoginHandler.java | 52 ---------------------------------------------------- src/main/java/com/roshka/proyectofinal/ProtectedResource.java | 36 ------------------------------------ src/main/java/com/roshka/proyectofinal/login/LoginServlet.java | 31 ++++++++++++++++++------------- src/main/java/com/roshka/proyectofinal/login/ProtectedResource.java | 39 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 57 insertions(+), 101 deletions(-) delete mode 100644 src/main/java/com/roshka/proyectofinal/LoginHandler.java delete mode 100644 src/main/java/com/roshka/proyectofinal/ProtectedResource.java create mode 100644 src/main/java/com/roshka/proyectofinal/login/ProtectedResource.java diff --git a/src/main/java/com/roshka/proyectofinal/LoginHandler.java b/src/main/java/com/roshka/proyectofinal/LoginHandler.java deleted file mode 100644 index 4d7999f..0000000 --- a/src/main/java/com/roshka/proyectofinal/LoginHandler.java +++ /dev/null @@ -1,52 +0,0 @@ -package com.roshka.proyectofinal; - -import jakarta.servlet.ServletException; -import jakarta.servlet.http.HttpServlet; -import jakarta.servlet.http.HttpServletRequest; -import jakarta.servlet.http.HttpServletResponse; -import jakarta.servlet.http.HttpSession; - -import java.io.*; - -public class LoginHandler extends HttpServlet { - - public void doPost(HttpServletRequest req, HttpServletResponse res) - throws ServletException, IOException { - res.setContentType("text/html"); - PrintWriter out = res.getWriter(); - - // Get the user's name and password - String name = req.getParameter("name"); - String passwd = req.getParameter("passwd"); - - // Check the name and password for validity - if (!allowUser(name, passwd)) { - out.println("<HTML><HEAD><TITLE>Access Denied</TITLE></HEAD>"); - out.println("<BODY>Your login and password are invalid.<BR>"); - out.println("You may want to <A HREF=\"/login.html\">try again</A>"); - out.println("</BODY></HTML>"); - } - else { - // Valid login. Make a note in the session object. - HttpSession session = req.getSession(true); - session.putValue("logon.isDone", name); // just a marker object - - // Try redirecting the client to the page he first tried to access - try { - String target = (String) session.getValue("login.target"); - if (target != null) - res.sendRedirect(target); - return; - } - catch (Exception ignored) { } - - // Couldn't redirect to the target. Redirect to the site's home page. - res.sendRedirect(req.getScheme() + "://" + - req.getServerName() + ":" + req.getServerPort()); - } - } - - protected boolean allowUser(String user, String passwd) { - return true; // trust everyone - } -} \ No newline at end of file diff --git a/src/main/java/com/roshka/proyectofinal/ProtectedResource.java b/src/main/java/com/roshka/proyectofinal/ProtectedResource.java deleted file mode 100644 index eab9085..0000000 --- a/src/main/java/com/roshka/proyectofinal/ProtectedResource.java +++ /dev/null @@ -1,36 +0,0 @@ -package com.roshka.proyectofinal; - -import java.io.*; -import java.util.*; -import jakarta.servlet.*; -import jakarta.servlet.http.*; - -public class ProtectedResource extends HttpServlet { - - public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { - res.setContentType("text/plain"); - PrintWriter out = res.getWriter(); - - // Get the session - HttpSession session = req.getSession(true); - - // Does the session indicate this user already logged in? - Object done = session.getAttribute("logon.isDone"); - // marker object - if (done == null) { - // No se encuentra loggeado // Guardamos donde trato de dirigirse y lo REDIRIGIMOS AL LOGGIN - session.setAttribute("login.target", - HttpUtils.getRequestURL(req).toString()); - res.sendRedirect(req.getScheme() + "://" + req.getServerName() + ":" - + req.getServerPort() + "/login.jsp"); - return; - } - // El usuario se loggeo y puede ver el recurso - out.println("PUEDES ACCEDER AL RECURSO - ESTAS LOGGEADO"); - - - - - - } - } \ No newline at end of file diff --git a/src/main/java/com/roshka/proyectofinal/login/LoginServlet.java b/src/main/java/com/roshka/proyectofinal/login/LoginServlet.java index 538eb2b..e7b379b 100644 --- a/src/main/java/com/roshka/proyectofinal/login/LoginServlet.java +++ b/src/main/java/com/roshka/proyectofinal/login/LoginServlet.java @@ -1,6 +1,4 @@ package com.roshka.proyectofinal.login; - - import java.io.IOException; import java.io.PrintWriter; import java.security.NoSuchAlgorithmException; @@ -17,7 +15,6 @@ import jakarta.servlet.http.HttpSession; import static java.lang.System.out; - /** * Servlet implementation class LoginServlet */ @@ -60,19 +57,16 @@ public class LoginServlet extends HttpServlet { } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } - out.println(passwordMD5); loginBean.setPassword(passwordMD5); loginBean.setCorreo(correo); - out.println("EL pass encriptado es: " +passwordMD5); + //out.println("EL pass encriptado es: " +passwordMD5); if (loginDao.validate(loginBean)) { HttpSession session = request.getSession(true); //incluir nota de sesion valida session.setAttribute("logon.isDone", username); - - // Tratar de re-dirigir a la pagina que el usuario quiso acceder try { String target = (String) session.getAttribute("login.target"); @@ -91,13 +85,24 @@ public class LoginServlet extends HttpServlet { } else { //si no es un user valido - mandar error y redireccionar al inicio de sesion - + /* out.println(""); out.println("

You may want to try again

"); -// request.getRequestDispatcher("login.jsp").include(request, response); -// response.sendRedirect("login.jsp"); - - - //FOR COMMIT LOGIN 2 + out.println("Access Denied<title><head>");*/ + //request.getRequestDispatcher("login.jsp").include(request, response); + out.println("<!DOCTYPE html>"); + out.println("<html> <head> <title>BootcampsLogin "); + out.println("
"); + out.println("

User Login Form

"); + out.println("
"); + out.println(""); + out.println(""); + out.println(""); + out.println("
"); + out.println("
Correo:
Password:
"); + out.println("
Credenciales incorrectas! Reintente ...
"); + out.println(""); + + //response.sendRedirect("login.jsp"); } } diff --git a/src/main/java/com/roshka/proyectofinal/login/ProtectedResource.java b/src/main/java/com/roshka/proyectofinal/login/ProtectedResource.java new file mode 100644 index 0000000..980e356 --- /dev/null +++ b/src/main/java/com/roshka/proyectofinal/login/ProtectedResource.java @@ -0,0 +1,39 @@ +package com.roshka.proyectofinal.login; + +import java.io.*; +import java.util.*; +import jakarta.servlet.*; +import jakarta.servlet.annotation.WebServlet; +import jakarta.servlet.http.*; + +@WebServlet("/protected") + +public class ProtectedResource extends HttpServlet { + + public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { + res.setContentType("text/plain"); + PrintWriter out = res.getWriter(); + + // Get the session + HttpSession session = req.getSession(true); + + // Does the session indicate this user already logged in? + Object done = session.getAttribute("logon.isDone"); + // marker object + if (done == null) { + // No se encuentra loggeado // Guardamos donde trato de dirigirse y lo REDIRIGIMOS AL LOGGIN + session.setAttribute("login.target", + HttpUtils.getRequestURL(req).toString()); + res.sendRedirect(req.getScheme() + "://" + req.getServerName() + ":" + + req.getServerPort() + "/finalProyect2/login.jsp"); + return; + } + // El usuario se loggeo y puede ver el recurso + out.println("PUEDES ACCEDER AL RECURSO - ESTAS LOGGEADO"); + + + + + + } + } \ No newline at end of file -- libgit2 0.26.0