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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.javatutsworld.springboot</groupId>
    <artifactId>student-services-security</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>student-services</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.4.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!-- <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> 
            </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-devtools</artifactId>
            <scope>runtime</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


</project>

SpringBootFirstWebApplication.java

package com.javatutsworld.springboot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;

@SpringBootApplication
@ComponentScan("com.javatutsworld.springboot")
public class SpringBootFirstWebApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringBootFirstWebApplication.class, args);
    }
}

EmployeeController.java

package com.javatutsworld.springboot.controller;

import java.net.URI;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;

import com.javatutsworld.springboot.model.Employee;
import com.javatutsworld.springboot.model.Employees;
import com.javatutsworld.springboot.service.EmployeeDAO;


@RestController
@RequestMapping(path = "/employees")
public class EmployeeController 
{
    @Autowired
    private EmployeeDAO employeeDao;

    @GetMapping(path="/", produces = "application/json")
    public Employees getEmployees() 
    {
        return employeeDao.getAllEmployees();
    }

    @PostMapping(path= "/", consumes = "application/json", produces = "application/json")
    public ResponseEntity<Object> addEmployee(@RequestBody Employee employee) 
    {
        Integer id = employeeDao.getAllEmployees().getEmployeeList().size() + 1;
        employee.setId(id);

        employeeDao.addEmployee(employee);

        URI location = ServletUriComponentsBuilder.fromCurrentRequest()
                                    .path("/{id}")
                                    .buildAndExpand(employee.getId())
                                    .toUri();

        return ResponseEntity.created(location).build();
    }
}

LoginController.java

package com.javatutsworld.springboot.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.javatutsworld.springboot.service.LoginService;


@Controller
@SessionAttributes("name")
public class LoginController {

    @Autowired
    LoginService service;

    @RequestMapping(value="/login", method = RequestMethod.GET)
    public String showLoginPage(ModelMap model){
        return "login";
    }

    @RequestMapping(value="/login", method = RequestMethod.POST)
    public String showWelcomePage(ModelMap model, @RequestParam String name, @RequestParam String password){

        boolean isValidUser = service.validateUser(name, password);

        if (!isValidUser) {
            model.put("errorMessage", "Invalid Credentials");
            return "login";
        }

        model.put("name", name);
        model.put("password", password);

        return "welcome";
    }

}

UserController.java

package com.javatutsworld.springboot.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.javatutsworld.springboot.model.Employee;
import com.javatutsworld.springboot.model.Employees;
import com.javatutsworld.springboot.service.EmployeeDAO;
import com.javatutsworld.springboot.service.UserService;

@Controller
@SessionAttributes("name")
public class UserController {

    @Autowired
    UserService service;
    @Autowired EmployeeDAO employeeDao;

    @RequestMapping(value="/listuserinfo", method = RequestMethod.GET)
    public String showUserInfo(ModelMap model){
        String name = (String) model.get("name");
        System.out.println("name>>"+name);
        model.put("userinfo", service.retrieveUserInfo(name));
        return "userinfo";
    }
    @RequestMapping(value="/employee", method = RequestMethod.GET)
    public String showEmployee(ModelMap model){

        Employees e=employeeDao.getAllEmployees();
        List<Employee> list= new ArrayList<Employee>();

        for(Employee emp: e.getEmployeeList()){
            list.add(emp);
        }
        model.put("employeeList", list);
        System.out.println(e.getEmployeeList());

        return "employee";
    }

}

Employee.java

package com.javatutsworld.springboot.model;

public class Employee {

    public Employee() {

    }

    public Employee(Integer id, String firstName, String lastName, String email) {
        super();
        this.id = id;
        this.firstName = firstName;
        this.lastName = lastName;
        this.email = email;
    }

    private Integer id;
    private String firstName;
    private String lastName;
    private String email;

    //Getters and setters

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    @Override
    public String toString() {
        return "Employee [id=" + id + ", firstName=" + firstName + ", lastName=" + lastName + ", email=" + email + "]";
    }
}

Employees.java

package com.javatutsworld.springboot.model;

import java.util.ArrayList;
import java.util.List;

public class Employees 
{
    private List<Employee> employeeList;

    public List<Employee> getEmployeeList() {
        if(employeeList == null) {
            employeeList = new ArrayList<>();
        }
        return employeeList;
    }

    public void setEmployeeList(List<Employee> employeeList) {
        this.employeeList = employeeList;
    }
}

UserInfo.java

package com.javatutsworld.springboot.model;

import java.util.Date;

public class UserInfo {
    private int id;
    private String user;
    private String desc;
    private Date targetDate;
    private boolean isDone;
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getUser() {
        return user;
    }
    public void setUser(String user) {
        this.user = user;
    }
    public String getDesc() {
        return desc;
    }
    public void setDesc(String desc) {
        this.desc = desc;
    }
    public Date getTargetDate() {
        return targetDate;
    }
    public void setTargetDate(Date targetDate) {
        this.targetDate = targetDate;
    }
    public boolean isDone() {
        return isDone;
    }
    public void setDone(boolean isDone) {
        this.isDone = isDone;
    }
    public UserInfo(int id, String user, String desc, Date targetDate,
            boolean isDone) {
        super();
        this.id = id;
        this.user = user;
        this.desc = desc;
        this.targetDate = targetDate;
        this.isDone = isDone;
    }


} 

EmployeeDAO.java

package com.javatutsworld.springboot.service;

import org.springframework.stereotype.Repository;

import com.javatutsworld.springboot.model.Employee;
import com.javatutsworld.springboot.model.Employees;



@Repository
public class EmployeeDAO 
{
    public static Employees list = new Employees();

    static
    {
        list.getEmployeeList().add(new Employee(1, "Rahul", "Gupta", "rahul123456@gmail.com"));
        list.getEmployeeList().add(new Employee(2, "ali", "Saaim", "alisaaim@gmail.com"));
        list.getEmployeeList().add(new Employee(3, "Ankur", "Sharma", "xyz@gmail.com"));
    }

    public Employees getAllEmployees() 
    {
        return list;
    }

    public void addEmployee(Employee employee) {
        list.getEmployeeList().add(employee);
    }
}

LoginService.java

package com.javatutsworld.springboot.service;

import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

@Service
public class LoginService {

    public boolean validateUser(String userid, String password) {
        return userid.equalsIgnoreCase("ali")
                && password.equalsIgnoreCase("ali");
    }

}

UserService.java

package com.javatutsworld.springboot.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.stereotype.Service;

import com.javatutsworld.springboot.model.UserInfo;

@Service
public class UserService {
    private static List<UserInfo> userInfo = new ArrayList<UserInfo>();

    static {
        userInfo.add(new UserInfo(1, "ali", "He is web developer", new Date(),
                false));
        userInfo.add(new UserInfo(2, "Gaurav", "He is scala developer", new Date(), false));
        userInfo.add(new UserInfo(3, "Naman", "He is java developer", new Date(),
                false));
        userInfo.add(new UserInfo(4, "Sanjay", "He is hadoop developer", new Date(),
                false));
    }

    public List<UserInfo> retrieveUserInfo(String user) {
        List<UserInfo> filteredUserInfo = new ArrayList<UserInfo>();
        for (UserInfo uinfo : userInfo) {
            if (uinfo.getUser().equals(user)) {
                filteredUserInfo.add(uinfo);
            }
        }
        return filteredUserInfo;
    }
}

employee.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>

<head>
<title>First Web Application</title>
</head>

<body>
    Here are the list of employee:<br><br>
    <c:if test="${not empty employeeList}">
    <c:forEach items="${employeeList}" var ="emp">
    <font color="green" size="8">${emp}</font>
</c:forEach>
</c:if>

</body>

</html>

login.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>

<head>
<title>First Web Application</title>
</head>

<body>
    <font color="red">${errorMessage}</font>
    <font color="green" size="8">
    <form method="post">
        Name : <input type="text" name="name" />
        Password : <input type="password" name="password" /> 
        <input type="submit" />
    </form>
    </font>
</body>

</html>

userinfo.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>

<head>
<title>First Web Application</title>
</head>

<body>
    Here are the list of your user:<br><br>
    <c:if test="${not empty userinfo}">
    <c:forEach items="${userinfo}" var="uinfo">
    <font color="green" size="8">${uinfo.user}</font>
</c:forEach>

</c:if> 
    <br>
    Your Name is : ${name}

    Welcome Employees!! <a href="/employee">Click here</a> to show employee list.
</body>

</html>

welcome.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>

<head>
<title>First Web Application</title>
</head>

<body>
    <font color="green" size="8">Welcome ${name}!! <a href="/listuserinfo">Click here</a> to show the user information.</font>
</body>

</html>

application.properties

spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp

Output

run the using the http://localhost:8080/login and enter user/password below

username = ali password = ali