From 013bb72439ed9c0f6e48c59121317796ea7b07a1 Mon Sep 17 00:00:00 2001 From: m-shimizu <26-m-e-stella-tecnica@it-college.jp> Date: Tue, 20 Oct 2026 09:36:57 +0900 Subject: [PATCH] =?UTF-8?q?LoginController=5F=E9=80=94=E4=B8=AD=E7=B5=8C?= =?UTF-8?q?=E9=81=8E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../demo/controller/LoginController.java | 96 ++++++++++++++----- 1 file changed, 72 insertions(+), 24 deletions(-) diff --git a/book-rent/src/main/java/com/example/demo/controller/LoginController.java b/book-rent/src/main/java/com/example/demo/controller/LoginController.java index 4f790ca..d67fcc2 100644 --- a/book-rent/src/main/java/com/example/demo/controller/LoginController.java +++ b/book-rent/src/main/java/com/example/demo/controller/LoginController.java @@ -1,39 +1,87 @@ -// package宣言(チームの構成に合わせる) package com.example.demo.controller; -import jakarta.validation.Valid; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpSession; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; -import org.springframework.validation.BindingResult; import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestParam; + +import com.example.demo.common.LoginUser; +import com.example.demo.entity.Employee; +import com.example.demo.repository.EmployeeRepository; @Controller public class LoginController { - // ログイン画面表示 - @GetMapping("/login") - public String showLogin(Model model) { - model.addAttribute("loginInfoForm", new LoginInfoForm()); - return "login"; - } + private final EmployeeRepository employeeRepository; - // ログイン処理 - @PostMapping("/doLogin") - public String doLogin( - @Valid @ModelAttribute("loginInfoForm") - LoginInfoForm form, - BindingResult result) { + public LoginController(EmployeeRepository employeeRepository) { + this.employeeRepository = employeeRepository; + } - // 入力チェックでエラーがあればログイン画面に戻る - if (result.hasErrors()) { - return "login"; - } + // ログイン画面表示 + @GetMapping("/login") + public String showLogin() { + return "login"; + } - // ここから社員情報の検索・認証処理を追加する + // ログイン処理 + @PostMapping("/doLogin") + public String doLogin( + @RequestParam(required = false) String mailAddress, + @RequestParam(required = false) String password, + HttpServletRequest request, + Model model) { - return "login"; - } -} + // 入力チェック + if (mailAddress == null || password == null + || mailAddress.isBlank() || password.isBlank() + || mailAddress.length() > 256 + || !mailAddress.matches( + "^[^\\s@]+@[^\\s@]+\\.[^\\s@]+$")) { + + model.addAttribute("errorMessage", + "メールアドレスとパスワードを正しく入力してください。"); + return "login"; + } + + // 在籍中の社員を検索 + Employee employee = employeeRepository + .findByMailAddressAndFlgRetirement( + mailAddress.trim(), "0") + .orElse(null); + + // 社員情報・パスワードの確認 + if (employee == null + || !password.equals(employee.getPassword())) { + + model.addAttribute("errorMessage", + "メールアドレスまたはパスワードが正しくありません。"); + return "login"; + } + + // 古いセッションがあれば破棄 + HttpSession oldSession = request.getSession(false); + if (oldSession != null) { + oldSession.invalidate(); + } + + // ログイン情報を新しいセッションに保存 + HttpSession newSession = request.getSession(true); + + LoginUser loginUser = new LoginUser( + employee.getIdEmployee(), + employee.getNmEmployee(), + employee.getKnEmployee(), + employee.getMailAddress(), + employee.getFlgAdmin()); + + newSession.setAttribute("loginUser", loginUser); + + // メニュー画面へ遷移 + return "redirect:/menu"; + } +} \ No newline at end of file