Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
M
misuperproyecto
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Nahuel Mereles Rodriguez
misuperproyecto
Commits
ab9d7927
Commit
ab9d7927
authored
May 11, 2022
by
Emanuel Lugo
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
LogginHandler y RecursoProtegido - Estructura anhadida
parent
0dfb87a5
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
79 additions
and
0 deletions
+79
-0
src/main/java/com/roshka/proyectofinal/LoginHandler.java
+48
-0
src/main/java/com/roshka/proyectofinal/ProtectedResource.java
+31
-0
No files found.
src/main/java/com/roshka/proyectofinal/LoginHandler.java
0 → 100644
View file @
ab9d7927
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
src/main/java/com/roshka/proyectofinal/ProtectedResource.java
0 → 100644
View file @
ab9d7927
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
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment