[ADD]:ログイン・ログアウト機能実装

This commit is contained in:
h-seya
2026-09-24 16:04:12 +09:00
parent 21dd389deb
commit 004ea0d5b9
7 changed files with 206 additions and 61 deletions

View File

@@ -1,22 +1,77 @@
package com.example.demo.controller; package com.example.demo.controller;
import jakarta.servlet.http.HttpSession;
import jakarta.validation.Valid;
import org.springframework.stereotype.Controller; 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.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PostMapping;
import com.example.demo.entity.AccountEntity;
import com.example.demo.form.LoginForm;
import com.example.demo.service.LoginService;
import lombok.RequiredArgsConstructor;
@Controller @Controller
@RequiredArgsConstructor
public class LoginController { public class LoginController {
@GetMapping("/login") private final LoginService loginService;
public String showLogin() {
return "login"; // Boot起動直後にログイン画面に移動する用のメソッド
@GetMapping("/")
public String index(HttpSession session) {
if (session.getAttribute("loginEmployee") != null) {
return "redirect:/topmenu";
}
return "redirect:/login";
} }
@PostMapping("/doLogin") @GetMapping("/login")
public String doLogin() { public String loginForm(Model model, HttpSession session) {
if (session.getAttribute("loginEmployee") != null) {
return "redirect:/topmenu"; return "redirect:/topmenu";
} }
model.addAttribute("loginForm", new LoginForm());
return "login";
}
// 実際のログイン処理
@PostMapping("/login")
public String doLogin(
@Valid @ModelAttribute LoginForm loginForm,
BindingResult bindingResult,
Model model,
HttpSession session) {
if (bindingResult.hasErrors()) {
return "login";
}
AccountEntity accountEntity = loginService.login(loginForm);
if (accountEntity == null) {
model.addAttribute("loginError",
"ログインIDまたはパスワードが正しくありません");
return "login";
}
session.setAttribute("loginEmployee", accountEntity);
return "redirect:/topmenu";
}
// ログアウト処理
@GetMapping("/logout")
public String logout(HttpSession session) {
session.invalidate();
return "redirect:/login";
}
// 各画面からトップメニューへの遷移用
@GetMapping("/topmenu") @GetMapping("/topmenu")
public String showTopMenu() { public String showTopMenu() {
return "topmenu"; return "topmenu";

View File

@@ -21,15 +21,15 @@ import lombok.Setter;
@Getter @Getter
@Setter @Setter
@NoArgsConstructor @NoArgsConstructor
public class Account { public class AccountEntity {
@Id @Id
@GeneratedValue(strategy = GenerationType.IDENTITY) @GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name="login_info_id") @Column(name="login_info_id")
private int loginInfoId; private Integer loginInfoId;
@Column(name="employee_id") @Column(name="employee_id")
private int employeeId; private Integer employeeId;
@Column(name="password_hash") @Column(name="password_hash")
private String passwordHash; private String passwordHash;

View File

@@ -0,0 +1,17 @@
package com.example.demo.form;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import lombok.Data;
@Data
public class LoginForm {
@NotNull(message = "社員IDを入力してください")
private Integer employeeId;
@NotBlank(message = "パスワードを入力してください")
private String passwordHash;
}

View File

@@ -0,0 +1,11 @@
package com.example.demo.repository;
import org.springframework.data.jpa.repository.JpaRepository;
import com.example.demo.entity.AccountEntity;
public interface AccountRepository extends JpaRepository<AccountEntity,Integer>{
AccountEntity findByEmployeeId (Integer employeeId);
}

View File

@@ -0,0 +1,31 @@
package com.example.demo.service;
import org.springframework.stereotype.Service;
import com.example.demo.entity.AccountEntity;
import com.example.demo.form.LoginForm;
import com.example.demo.repository.AccountRepository;
import lombok.RequiredArgsConstructor;
@Service
@RequiredArgsConstructor
public class LoginService {
private final AccountRepository accountRepository;
public AccountEntity login(LoginForm form) {
AccountEntity accountEntity =
accountRepository.findByEmployeeId(form.getEmployeeId());
if(accountEntity == null) {
return null;
}
if(!accountEntity.getPasswordHash().equals(form.getPasswordHash()))
return null;
return accountEntity;
}
}

View File

@@ -1,29 +1,35 @@
<!DOCTYPE html> <!DOCTYPE html>
<html> <html xmlns:th="http://www.thymeleaf.org">
<head> <head>
<meta charset="UTF-8"> <meta charset="UTF-8">
<title>ログイン</title> <title>ログイン</title>
</head> </head>
<body> <body>
<h1>ログイン</h1> <h1>ログイン</h1>
<form action="doLogin"
method="post"
th:object="${loginInfoForm}">
<div class="msgBox error" th:if="${messageError} != null"> <p class="message message-error" th:if="${loginError}" th:text="${loginError}">
<span class="error" th:text="${messageError.message}"></span> </p>
<form th:action="@{/login}" th:object="${loginForm}" method="post">
<div class="form-group">
<label for="employeeId">社員ID</label>
<input id="employeeId" type="text" th:field="*{employeeId}">
<p class="field-error" th:errors="*{employeeId}"></p>
</div> </div>
<table> <div class="form-group">
<tr> <label for="passwordHash">パスワード</label>
<td class="btnArea" colspan="2"> <input id="passwordHash" type="password" th:field="*{passwordHash}">
<input type="submit" value="ログイン"/> <p class="field-error" th:errors="*{passwordHash}"></p>
<button onclick="clearInput()">リセット</button> </div>
</td>
</tr> <input type="submit" value="ログイン">
</table> <input type="reset" value="リセット">
</form> </form>
</body> </body>
</html> </html>

View File

@@ -1,10 +1,34 @@
<!DOCTYPE html> <!DOCTYPE html>
<html> <html>
<head> <head>
<meta charset="UTF-8"> <meta charset="UTF-8">
<title>トップメニュー</title> <title>トップメニュー</title>
</head> </head>
<body> <body>
<div class="header-bar">
<div class="header-left">
ShareBooks
</div>
<div class="header-right">
<span class="user">
テストユーザー(一般)
</span>
<a class="button-link" th:href="@{/logout}">
ログアウト
</a>
</div>
</div>
<h1> <h1>
メインメニュー メインメニュー
</h1> </h1>
@@ -73,7 +97,7 @@
<hr> <hr>
<a class="card" href="/"> <a class="card" href="/passwordChange">
<h3>パスワード変更</h3> <h3>パスワード変更</h3>
<p> <p>
@@ -82,4 +106,5 @@
</a> </a>
</body> </body>
</html> </html>