User Auth Source Code
User Auth Source Code
User Auth Source Code
Source Code:
Jsp:
Index.jsp:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Home Page</title>
</head>
<body>
<center>
<h2 style="text-align: center">Welcome</h2>
<br> <br>
<form method="get" action="/loginform">
<button type="submit">Login</button>
</form>
<br> <br>
</center>
</body>
</html>
deniedAccess.jsp:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Login Denied</title>
</head>
<body>
import javax.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.example.Authentication.entities.User;
import com.example.Authentication.exceptions.UserNotFoundException;
import com.example.Authentication.services.UserService;
@Controller
public class LoginController {
@Autowired
UserService userService;
Logger log = LoggerFactory.getLogger(LoginController.class);
@GetMapping("/index")
public String showGreeting(ModelMap map) {
return "index";
}
@GetMapping("/loginform")
public String showLogin(ModelMap map) {
return "loginform";
}
@PostMapping("/loginform")
public String submitLogin(ModelMap map, @RequestParam String username,
@RequestParam String password){
User checkUser;
try {
checkUser= userService.GetUserByName(username);
if (userService.verifyPassword(username, password) == false) {
log.info(username + " - " + password);
map.addAttribute("error", "Error Wrong password");
return "deniedAccess";
}
else
return "success";
}
}
userDNEController.java:
package com.example.Authentication.controllers;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import com.example.Authentication.entities.User;
import com.example.Authentication.exceptions.UserNotFoundException;
import com.example.Authentication.services.UserService;
@ControllerAdvice
public class UserDNEController {
}
}
Entities:
User.java:
package com.example.Authentication.entities;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
public User() {
@Override
public String toString() {
return (id.toString() + " " + name + " " + email + " " + password);
}
}
Exceptions:
userNotFoundException.java:
package com.example.Authentication.exceptions;
Repositories:
userRepository.java:
package com.example.Authentication.repositories;
import java.util.Optional;
import org.springframework.data.repository.CrudRepository;
import com.example.Authentication.entities.User;
import java.util.List;
import java.util.Optional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.example.Authentication.entities.User;
import com.example.Authentication.exceptions.UserNotFoundException;
import com.example.Authentication.repositories.UserRepository;
@Service
public class UserService {
@Autowired
private UserRepository userRepository;
return(foundUser.get());
}
/*
public User GetUserById(int id) {
Optional<User> foundUser = userRepository.findById(id);
if (!foundUser.isPresent()) {
throw new UserNotFoundException();
}
return(foundUser.get());
}*/
if (user.getPassword().equals(password)) {
verified = true;
}
return verified;
}
authenticationApplication.java:
package com.example.Authentication;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Import;
import com.example.Authentication.controllers.LoginController;
import com.example.Authentication.controllers.UserDNEController;
import com.example.Authentication.entities.User;
import com.example.Authentication.exceptions.UserNotFoundException;
import com.example.Authentication.services.UserService;
@SpringBootApplication
@Import({
UserNotFoundException.class,
UserService.class,
LoginController.class,
User.class,
UserDNEController.class
})
public class AuthenticationApplication {
Resources:
Application.properties:
spring.jpa.hibernate.ddl-auto=update
spring.datasource.url=jdbc:mysql://localhost:3306/Auther
spring.datasource.username=root
spring.datasource.password=Sahi@1234#
logging.level.org.springframework.web: DEBUG
spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp
server.port=8090
server.error.whitelabel.enabled=false
test:
authenticationApplicationTests.java
package com.example.Authentication;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class AuthenticationApplicationTests {
@Test
void contextLoads() {
}
authenticationWebTests.java:
package com.example.Authentication;
import com.example.Authentication.controllers.LoginController;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.web.server.LocalServerPort;
import org.springframework.test.web.servlet.MockMvc;
import
org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.hamcrest.Matchers.containsString;
import static
org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static
org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static
org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static
org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@AutoConfigureMockMvc
public class AuthenticationWebTests {
@LocalServerPort
private int port;
@Autowired
private LoginController controller;
@Autowired
private MockMvc mockMvc;
@Test
public void shouldReturnDefaultMessage() throws Exception {
this.mockMvc.perform(get("/")).andDo(print()).andExpect(status().isOk(
));
}
}
userEntityTests.java:
package com.example.Authentication;
import com.example.Authentication.entities.User;
import com.example.Authentication.repositories.UserRepository;
import com.example.Authentication.services.UserService;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Assertions.*;
import static org.junit.jupiter.api.Assertions.assertEquals;
@Test
public void WhenSetPassword_CheckGetPassword() {
User testUser = new User();
testUser.setPassword("Samplepassword");
assertEquals(testUser.getPassword(),"Samplepassword");
}
@Test
public void WhenSetPassword_CheckPassword() {
User testUser = new User();
testUser.setPassword("password");
assertEquals(test, "password");
}
@Test
public void WhenSetName_CheckGetName() {
User testUser = new User();
testUser.setName("Samplename");
assertEquals(testUser.getName(),"Samplename");
}
@Test
public void WhenSetName_CheckName() {
User testUser = new User();
testUser.setName("name");
assertEquals(testUser.getName(),"name");
}
@Test
public void WhenSetEmail_CheckEmail() {
User testUser = new User();
testUser.setEmail("[email protected]");
assertEquals(test, "[email protected]");
}
@Test
public void WhenSetEmail_CheckGetEmail() {
User testUser = new User();
testUser.setEmail("[email protected]");
assertEquals(test, "[email protected]");
}
userRepoTests.java:
package com.example.Authentication;
import com.example.Authentication.entities.User;
import com.example.Authentication.repositories.UserRepository;
import com.example.Authentication.services.UserService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.boot.test.autoconfigure.orm.jpa.TestEntityManager;
import org.springframework.boot.test.context.SpringBootTest;
import org.junit.jupiter.api.Assertions.*;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.util.Optional;
@DataJpaTest
@Autowired
private TestEntityManager entityManager;
@Autowired
private UserRepository userRepository;
@Test
public void whenFindByName_thenReturnUser() {
// given
// when
Optional<User> found = userRepository.findByName(dummyUser.getName());
// then
assertEquals(founded.getName(), dummyUser.getName());
}
//
// @Test
// public void whenGetPassword_thenReturnBoolean() {
//
// User dummyUser = new User();
// dummyUser.setName("Dummy");
// dummyUser.setEmail("[email protected]");
// dummyUser.setPassword("password");
// entityManager.persist(dummyUser);
// entityManager.flush();
//
// String test = dummyUser.getPassword();
//
// assertEquals(test, "password");
// }
userServiceTest.java:
package com.example.Authentication;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.util.List;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.boot.test.autoconfigure.orm.jpa.TestEntityManager;
import com.example.Authentication.entities.User;
import com.example.Authentication.services.UserService;
@DataJpaTest
public class UserServiceTest {
@Autowired
private TestEntityManager eM;
@Autowired
private UserService us;
@BeforeEach
public void bulid() {
eM.flush();
}
@Test
public void testGetAllUsers() {
assertEquals(count, 2);
}
@Test
public void testVerifyPassword() {
String username = "Dummy";
String password = "password";
boolean b = us.verifyPassword(username, password);
assertEquals(b, true);
}
Pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.3</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>Authentication</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>Authentication</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>