kievreclama.task.controllers.RootController.java Source code

Java tutorial

Introduction

Here is the source code for kievreclama.task.controllers.RootController.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package kievreclama.task.controllers;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.web.authentication.logout.SecurityContextLogoutHandler;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import kievreclama.task.web.models.UserModel;

/**
 *
 * @author firsov
 */
@Controller
public class RootController {

    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String homePage() {
        return "redirect:/employee/";
    }

    @RequestMapping(value = "/login", method = RequestMethod.GET)
    public String getLoginForm(@RequestParam(value = "error", required = false) String error, Model model) {
        model.addAttribute("user", new UserModel());
        if (error != null) {
            model.addAttribute("error", "");
        }
        return "login";
    }

    @RequestMapping(value = "/logout", method = RequestMethod.GET)
    public String logoutPage(HttpServletRequest request, HttpServletResponse response) {
        Authentication auth = SecurityContextHolder.getContext().getAuthentication();
        if (auth != null) {
            new SecurityContextLogoutHandler().logout(request, response, auth);
        }
        return "redirect:/task";
    }
}