From ab9d7927e97a7119a0c964cbc4fd9e7f5d7624bd Mon Sep 17 00:00:00 2001 From: Emanuel Date: Wed, 11 May 2022 13:48:32 -0400 Subject: [PATCH] LogginHandler y RecursoProtegido - Estructura anhadida --- src/main/java/com/roshka/proyectofinal/LoginHandler.java | 47 +++++++++++++++++++++++++++++++++++++++++++++++ src/main/java/com/roshka/proyectofinal/ProtectedResource.java | 30 ++++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+) create mode 100644 src/main/java/com/roshka/proyectofinal/LoginHandler.java create mode 100644 src/main/java/com/roshka/proyectofinal/ProtectedResource.java diff --git a/src/main/java/com/roshka/proyectofinal/LoginHandler.java b/src/main/java/com/roshka/proyectofinal/LoginHandler.java new file mode 100644 index 0000000..8595e08 --- /dev/null +++ b/src/main/java/com/roshka/proyectofinal/LoginHandler.java @@ -0,0 +1,47 @@ +import java.io.*; +import java.util.*; +import javax.servlet.*; +import javax.servlet.http.*; + +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 new file mode 100644 index 0000000..caf282b --- /dev/null +++ b/src/main/java/com/roshka/proyectofinal/ProtectedResource.java @@ -0,0 +1,30 @@ +package com.roshka.proyectofinal; + +import java.io.*; +import java.util.*; +import javax.servlet.*; +import javax.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.getValue("logon.isDone"); + // marker object + if (done == null) { + // No logon.isDone means he hasn't logged in. // Save the request URL as the true target and redirect to the login page + session.putValue("login.target", + HttpUtils.getRequestURL(req).toString()); res.sendRedirect(req.getScheme() + "://" + req.getServerName() + ":" + + req.getServerPort() + "/login.html"); + return; + } + // If we get here, the user has logged in and can see the goods + out.println("Unpublished O'Reilly book manuscripts await you!"); + } + } \ No newline at end of file -- libgit2 0.26.0