LoginController_途中経過
This commit is contained in:
@@ -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";
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user