Commit a87e6390 by Emanuel Lugo

protected resource updated

parent 55f46bc4
......@@ -15,16 +15,22 @@ public class ProtectedResource extends HttpServlet {
HttpSession session = req.getSession(true);
// Does the session indicate this user already logged in?
Object done = session.getValue("logon.isDone");
Object done = session.getAttribute("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");
// 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;
}
// If we get here, the user has logged in and can see the goods
out.println("Unpublished O'Reilly book manuscripts await you!");
// El usuario se loggeo y puede ver el recurso
out.println("PUEDES ACCEDER AL RECURSO - ESTAS LOGGEADO");
}
}
\ No newline at end of file
......@@ -2,6 +2,7 @@ package com.roshka.proyectofinal.login;
import java.io.IOException;
import java.io.PrintWriter;
import java.security.NoSuchAlgorithmException;
import jakarta.servlet.ServletException;
......@@ -12,6 +13,9 @@ import jakarta.servlet.http.HttpServletResponse;
import com.roshka.proyectofinal.entity.LoginBean;
import com.roshka.proyectofinal.login.md5JavaHash;
import jakarta.servlet.http.HttpSession;
import static java.lang.System.out;
/**
......@@ -44,6 +48,7 @@ public class LoginServlet extends HttpServlet {
LoginDao loginDao = new LoginDao();
md5JavaHash passEncrip = new md5JavaHash();
String passwordMD5 = "";
PrintWriter out = response.getWriter();
String username = request.getParameter("username");
String correo = request.getParameter("correo");
......@@ -55,22 +60,44 @@ public class LoginServlet extends HttpServlet {
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
System.out.println(passwordMD5);
out.println(passwordMD5);
loginBean.setPassword(passwordMD5);
loginBean.setCorreo(correo);
System.out.println("EL pass encriptado es: " +passwordMD5);
out.println("EL pass encriptado es: " +passwordMD5);
if (loginDao.validate(loginBean))
{
response.sendRedirect("loginSuccess.jsp");
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");
response.sendRedirect("loginSuccess.jsp");
if (target != null)
response.sendRedirect(target);
return;
}
else {
//HttpSession session = request.getSession();
response.sendRedirect("login.jsp");
catch (Exception ignored) { }
// Si no es posible redireccionar a la pagina solicitada, llevar a la main page
//response.sendRedirect(request.getScheme() + "://" +
// request.getServerName() + ":" + request.getServerPort());
System.out.println("redirigir al index.html");
} else {
//si no es un user valido - mandar error y redireccionar al inicio de sesion
out.println("<p> You may want to <a href='/login.jsp'> try again </a> </p>");
// request.getRequestDispatcher("login.jsp").include(request, response);
// response.sendRedirect("login.jsp");
}
}
}
......@@ -13,7 +13,6 @@
<tr><td>Password:</td><td><input type="password" name="password"></td></tr>
<tr><td><input type="submit" value="Login"/></td></tr>
</table>
</form>
</div>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment