LoginController_途中経過

This commit is contained in:
m-shimizu
2026-10-20 09:36:57 +09:00
parent f647eb6ef2
commit 013bb72439

View File

@@ -1,39 +1,87 @@
// package宣言(チームの構成に合わせる)
package com.example.demo.controller; 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.stereotype.Controller;
import org.springframework.ui.Model; import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping; 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.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 @Controller
public class LoginController { public class LoginController {
// ログイン画面表示 private final EmployeeRepository employeeRepository;
@GetMapping("/login")
public String showLogin(Model model) {
model.addAttribute("loginInfoForm", new LoginInfoForm());
return "login";
}
// ログイン処理 public LoginController(EmployeeRepository employeeRepository) {
@PostMapping("/doLogin") this.employeeRepository = employeeRepository;
public String doLogin( }
@Valid @ModelAttribute("loginInfoForm")
LoginInfoForm form,
BindingResult result) {
// 入力チェックでエラーがあればログイン画面に戻る // ログイン画面表示
if (result.hasErrors()) { @GetMapping("/login")
return "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";
}
}