Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Authentication Example #73

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 42 additions & 5 deletions CL-BSCSD-22-03/my-web-app1/src/main/java/oop/Utill.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,59 @@
*/
package oop;

import java.net.HttpCookie;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
*
* @author Chanaka
*/
public class Utill {

public List<Person> getPersons() {
public static List<Person> getPersons() {
List<Person> persons = new ArrayList<>();
persons.add(new Person("Roshan","54657","0715236589","1990-05-26","Male"));
persons.add(new Person("Amali","98414","0774582658","1986-08-15","Female"));
persons.add(new Person("Sudath","63874","0782463598","1970-04-02","Male"));
persons.add(new Person("Hasini","25896","0725896354","1991-10-20","Female"));
persons.add(new Person("Roshan", "54657", "0715236589", "1990-05-26", "Male"));
persons.add(new Person("Amali", "98414", "0774582658", "1986-08-15", "Female"));
persons.add(new Person("Sudath", "63874", "0782463598", "1970-04-02", "Male"));
persons.add(new Person("Hasini", "25896", "0725896354", "1991-10-20", "Female"));
return persons;
}

public static boolean authenticate(HttpServletRequest request, HttpServletResponse response, HttpSession session) {
String username = request.getParameter("username");
String password = request.getParameter("password");
if ((username != null) && (password != null) && (username.equals("admin")) && (password.equals("123"))) {
String newSessionId = UUID.randomUUID().toString();
session.setAttribute("session_id", newSessionId);
Cookie newCookie = new Cookie("session_id", newSessionId);
response.addCookie(newCookie);
return true;
} else {
try {
Cookie sessionCookie = null;
Cookie[] cookies = request.getCookies();
for (Cookie cookie : cookies) {
if (cookie.getName().equals("session_id")) {
sessionCookie = cookie;
}
}
if (sessionCookie != null) {
String sessionId = session.getAttribute("session_id").toString();
if (sessionId.equals(sessionCookie.getValue())) {
return true;
}
}
return false;
} catch (NullPointerException e) {
return false;
}
}
}

}
24 changes: 20 additions & 4 deletions CL-BSCSD-22-03/my-web-app1/src/main/webapp/index.jsp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
<title>JSP Page</title>
</head>
<body>
<%

if (Utill.authenticate(request, response, session)) {
%>
<table border="1">
<thead>
<tr>
Expand All @@ -26,18 +30,30 @@
</thead>
<tbody>
<%
Utill utill = new Utill();
for (Person p : utill.getPersons()) {
for (Person p : Utill.getPersons()) {
%>
<tr>
<td><%= p.getNic()%></td>
<td><%= p.getName() %></td>
<td><%= p.getName()%></td>
<td><%= p.getDateOfBirth()%></td>
<td><%= p.getGender()%></td>
<td><%= p.getMobile()%></td>
</tr>
<% } %>
<%
}
%>
</tbody>
</table>
<br>
<br>
<br>
<form action="logout.jsp" method="POST">
<input type="submit" value="Logout" />
</form>
<%
} else {
response.sendRedirect("login.jsp");
}
%>
</body>
</html>
32 changes: 32 additions & 0 deletions CL-BSCSD-22-03/my-web-app1/src/main/webapp/login.jsp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<%--
Document : login
Created on : Aug 15, 2021, 4:59:23 PM
Author : Chanaka
--%>

<%@page import="oop.Utill"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<%
if (Utill.authenticate(request, response, session)) {
response.sendRedirect("./");
} else {
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<form action="index.jsp" method="POST">
<label>Username :</label>
<input type="text" name="username"><br/>
<label>Password :</label>
<input type="password" name="password"><br/>
<input type="submit" value="Login">
</form>
</body>
</html>
<%
}
%>
10 changes: 10 additions & 0 deletions CL-BSCSD-22-03/my-web-app1/src/main/webapp/logout.jsp
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<%--
Document : logout.jsp
Created on : Aug 29, 2021, 12:17:48 PM
Author : Chanaka
--%>

<%
session.invalidate();
response.sendRedirect("./login.jsp");
%>