[ADD]:ログイン・ログアウト機能実装
This commit is contained in:
@@ -1,24 +1,79 @@
|
||||
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;
|
||||
|
||||
@PostMapping("/doLogin")
|
||||
public String doLogin() {
|
||||
return "redirect:/topmenu";
|
||||
}
|
||||
// Boot起動直後にログイン画面に移動する用のメソッド
|
||||
@GetMapping("/")
|
||||
public String index(HttpSession session) {
|
||||
if (session.getAttribute("loginEmployee") != null) {
|
||||
return "redirect:/topmenu";
|
||||
}
|
||||
return "redirect:/login";
|
||||
}
|
||||
|
||||
@GetMapping("/topmenu")
|
||||
public String showTopMenu() {
|
||||
return "topmenu";
|
||||
}
|
||||
@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";
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
17
src/main/java/com/example/demo/form/LoginForm.java
Normal file
17
src/main/java/com/example/demo/form/LoginForm.java
Normal 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;
|
||||
|
||||
}
|
||||
@@ -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);
|
||||
|
||||
}
|
||||
31
src/main/java/com/example/demo/service/LoginService.java
Normal file
31
src/main/java/com/example/demo/service/LoginService.java
Normal 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;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,29 +1,35 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<html xmlns:th="http://www.thymeleaf.org">
|
||||
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>ログイン</title>
|
||||
<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>
|
||||
</div>
|
||||
<p class="message message-error" th:if="${loginError}" th:text="${loginError}">
|
||||
</p>
|
||||
|
||||
<table>
|
||||
<tr>
|
||||
<td class="btnArea" colspan="2">
|
||||
<input type="submit" value="ログイン"/>
|
||||
<button onclick="clearInput()">リセット</button>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
<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 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>
|
||||
@@ -1,28 +1,52 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>トップメニュー</title>
|
||||
<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>
|
||||
|
||||
|
||||
<!-- ===== 共通:社内図書一覧 ===== -->
|
||||
<form method="post" name="社内図書一覧" action="book">
|
||||
<a href="javascript:form1.submit()">リンク名</a>
|
||||
<button type="submit" class="btn-book">
|
||||
<form method="post" name="社内図書一覧" action="book">
|
||||
<a href="javascript:form1.submit()">リンク名</a>
|
||||
<button type="submit" class="btn-book">
|
||||
|
||||
<h3>社内図書一覧</h3>
|
||||
<h3>社内図書一覧</h3>
|
||||
|
||||
<p>
|
||||
登録されている図書の<br> 一覧を表示します。
|
||||
</p>
|
||||
</button>
|
||||
</form>
|
||||
<hr> <!--区切り線-->
|
||||
<p>
|
||||
登録されている図書の<br> 一覧を表示します。
|
||||
</p>
|
||||
</button>
|
||||
</form>
|
||||
<hr> <!--区切り線-->
|
||||
|
||||
<!-- ===== 共通:寄贈申請 ===== -->
|
||||
|
||||
@@ -73,7 +97,7 @@
|
||||
|
||||
<hr>
|
||||
|
||||
<a class="card" href="/">
|
||||
<a class="card" href="/passwordChange">
|
||||
<h3>パスワード変更</h3>
|
||||
|
||||
<p>
|
||||
@@ -82,4 +106,5 @@
|
||||
</a>
|
||||
|
||||
</body>
|
||||
|
||||
</html>
|
||||
Reference in New Issue
Block a user