[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;
import jakarta.servlet.http.HttpSession;
import jakarta.validation.Valid;
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 com.example.demo.entity.AccountEntity;
import com.example.demo.form.LoginForm;
import com.example.demo.service.LoginService;
import lombok.RequiredArgsConstructor;
@Controller
@RequiredArgsConstructor
public class LoginController {
@GetMapping("/login")
public String showLogin() {
return "login";
private final LoginService loginService;
// Boot起動直後にログイン画面に移動する用のメソッド
@GetMapping("/")
public String index(HttpSession session) {
if (session.getAttribute("loginEmployee") != null) {
return "redirect:/topmenu";
}
return "redirect:/login";
}
@PostMapping("/doLogin")
public String doLogin() {
@GetMapping("/login")
public String loginForm(Model model, HttpSession session) {
if (session.getAttribute("loginEmployee") != null) {
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")
public String showTopMenu() {
return "topmenu";

View File

@@ -21,15 +21,15 @@ import lombok.Setter;
@Getter
@Setter
@NoArgsConstructor
public class Account {
public class AccountEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name="login_info_id")
private int loginInfoId;
private Integer loginInfoId;
@Column(name="employee_id")
private int employeeId;
private Integer employeeId;
@Column(name="password_hash")
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>
<html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>ログイン</title>
</head>
<body>
<h1>ログイン</h1>
<form action="doLogin"
method="post"
th:object="${loginInfoForm}">
<div class="msgBox error" th:if="${messageError} != null">
<span class="error" th:text="${messageError.message}"></span>
<p class="message message-error" th:if="${loginError}" th:text="${loginError}">
</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>
<table>
<tr>
<td class="btnArea" colspan="2">
<input type="submit" value="ログイン"/>
<button onclick="clearInput()">リセット</button>
</td>
</tr>
</table>
<div class="form-group">
<label for="passwordHash">パスワード</label>
<input id="passwordHash" type="password" th:field="*{passwordHash}">
<p class="field-error" th:errors="*{passwordHash}"></p>
</div>
<input type="submit" value="ログイン">
<input type="reset" value="リセット">
</form>
</body>
</html>

View File

@@ -1,10 +1,34 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>トップメニュー</title>
</head>
<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>
@@ -73,7 +97,7 @@
<hr>
<a class="card" href="/">
<a class="card" href="/passwordChange">
<h3>パスワード変更</h3>
<p>
@@ -82,4 +106,5 @@
</a>
</body>
</html>