diff --git a/book-rent/pom.xml b/book-rent/pom.xml deleted file mode 100644 index 5209f82..0000000 --- a/book-rent/pom.xml +++ /dev/null @@ -1,130 +0,0 @@ - - - 4.0.0 - - org.springframework.boot - spring-boot-starter-parent - 4.1.1 - - - com.example - book-rent - 0.0.1-SNAPSHOT - war - book-rent - - - - - - - - - - - - - - - - 21 - - - -org.springframework.boot -spring-boot-starter-data-jpa - - -org.springframework.boot -spring-boot-starter-thymeleaf - - -org.springframework.boot -spring-boot-starter-validation - - - -org.springframework.boot -spring-boot-starter-web - - - -org.springframework.boot -spring-boot-devtools -runtime -true - - - -com.mysql -mysql-connector-j -runtime - - -org.projectlombok -lombok -true - - -org.springframework.boot -spring-boot-starter-tomcat -provided - - - -org.springframework.boot -spring-boot-starter-test -test - - -org.springframework.boot -spring-boot-starter-security - - - - - - org.springframework.boot - spring-boot-maven-plugin - - - org.apache.maven.plugins - maven-compiler-plugin - - - default-compile - compile - - compile - - - - - org.projectlombok - lombok - - - - - - default-testCompile - test-compile - - testCompile - - - - - org.projectlombok - lombok - - - - - - - - - - \ No newline at end of file diff --git a/book-rent/src/main/java/com/example/demo/config/CustomAuthenticationFailureHandler.java b/book-rent/src/main/java/com/example/demo/config/CustomAuthenticationFailureHandler.java index 7e64080..dc140e8 100644 --- a/book-rent/src/main/java/com/example/demo/config/CustomAuthenticationFailureHandler.java +++ b/book-rent/src/main/java/com/example/demo/config/CustomAuthenticationFailureHandler.java @@ -56,4 +56,5 @@ public class CustomAuthenticationFailureHandler extends SimpleUrlAuthenticationF } } -} \ No newline at end of file +} + \ No newline at end of file diff --git a/book-rent/src/main/java/com/example/demo/controller/LoginController.java b/book-rent/src/main/java/com/example/demo/controller/LoginController.java index 6bdc6c4..50bd5d9 100644 --- a/book-rent/src/main/java/com/example/demo/controller/LoginController.java +++ b/book-rent/src/main/java/com/example/demo/controller/LoginController.java @@ -65,4 +65,76 @@ public class LoginController { return "login"; } -} \ No newline at end of file +} +//package com.example.demo.controller; +// +// +//import java.util.Optional; +// +//import jakarta.servlet.http.HttpServletRequest; +//import jakarta.servlet.http.HttpSession; +// +//import org.springframework.stereotype.Controller; +//import org.springframework.ui.Model; +//import org.springframework.web.bind.annotation.GetMapping; +//import org.springframework.web.bind.annotation.PostMapping; +//import org.springframework.web.bind.annotation.RequestParam; +// +//import com.example.demo.common.Const; +//import com.example.demo.common.LoginUser; +//import com.example.demo.service.AuthService; +// +///** AUTH-01:ログイン/ログアウト(login.html) */ +//@Controller +//public class LoginController { +// private final AuthService authService; +// +// public LoginController(AuthService authService) { +// this.authService = authService; +// } +// +// @GetMapping("/") +// public String root() { +// return "redirect:/menu"; +// } +// +// @GetMapping("/login") +// public String loginForm(HttpSession session) { +// if (session.getAttribute(Const.SESSION_LOGIN_USER) != null) { +// return "redirect:/menu"; +// } +// return "login"; +// } +// +// @PostMapping("/login") +// public String login(@RequestParam(defaultValue = "") String mailAddress, +// @RequestParam(defaultValue = "") String password, +// HttpServletRequest request, Model model) { +// String mail = mailAddress.trim(); +// model.addAttribute("mailAddress", mail); // 入力値を保持(パスワードは保持しない) +// +// if (mail.isEmpty() || password.isEmpty()) { +// model.addAttribute("errorMessage", "メールアドレスとパスワードを入力してください。"); +// return "login"; +// } +// Optional user = authService.authenticate(mail, password); +// if (user.isEmpty()) { +// // どちらが誤りかは分からない文言にする +// model.addAttribute("errorMessage", "メールアドレスまたはパスワードが正しくありません。"); +// return "login"; +// } +// // セッション固定化攻撃対策:古いセッションを破棄して新規発行 +// HttpSession old = request.getSession(false); +// if (old != null) { +// old.invalidate(); +// } +// request.getSession(true).setAttribute(Const.SESSION_LOGIN_USER, user.get()); +// return "redirect:/menu"; +// } +// +// @PostMapping("/logout") +// public String logout(HttpSession session) { +// session.invalidate(); +// return "redirect:/login"; +// } +//} \ No newline at end of file diff --git a/book-rent/src/main/java/com/example/demo/service/CustomUserDetailsService.java b/book-rent/src/main/java/com/example/demo/service/CustomUserDetailsService.java index be96b55..57d4649 100644 --- a/book-rent/src/main/java/com/example/demo/service/CustomUserDetailsService.java +++ b/book-rent/src/main/java/com/example/demo/service/CustomUserDetailsService.java @@ -30,7 +30,7 @@ public class CustomUserDetailsService implements UserDetailsService { Employee employee = employeeRepository.findByMailAddressAndFlgRetirement(maillAddress.trim(), Const.RETIRE_NO) .orElseThrow(() -> new UsernameNotFoundException("認証に失敗しました。")); - // ↑社員情報が見つからない場合は例外処理 + // ↑社員情報が見つからない場合は例外処理 // 取得したEmployeeをLoginUserに変換する LoginUser loginUser = new LoginUser( diff --git a/book-rent/src/main/java/com/example/demo/service/EmployeeService.java b/book-rent/src/main/java/com/example/demo/service/EmployeeService.java index ebf0a89..28bdec6 100644 --- a/book-rent/src/main/java/com/example/demo/service/EmployeeService.java +++ b/book-rent/src/main/java/com/example/demo/service/EmployeeService.java @@ -18,6 +18,9 @@ import com.example.demo.entity.Employee; import com.example.demo.form.EmployeeForm; import com.example.demo.repository.EmployeeRepository; + + + /** EMP-01:社員登録、EMP-02:社員編集 */ @Service public class EmployeeService { @@ -107,8 +110,6 @@ public class EmployeeService { e.add("社員名よみを入力してください。"); else if (f.getKnEmployee().length() > 100) e.add("社員名よみは100文字以内で入力してください。"); - else if (!f.getKnEmployee().matches("^[\\u3040-\\u309F]+$")) - e.add("社員名よみはひらがなで入力してください"); } private void checkMail(EmployeeForm f, Integer selfId, List e) { @@ -192,3 +193,190 @@ public class EmployeeService { throw new BusinessException(String.join(" ", errors)); } } + +//package com.example.demo.service; +// +//import java.time.LocalDateTime; +//import java.util.ArrayList; +//import java.util.List; +//import java.util.Optional; +//import java.util.regex.Pattern; +// +//import org.springframework.dao.DataIntegrityViolationException; +//import org.springframework.stereotype.Service; +//import org.springframework.transaction.annotation.Transactional; +// +//import com.example.demo.common.BusinessException; +//import com.example.demo.common.Const; +//import com.example.demo.common.LoginUser; +//import com.example.demo.entity.Employee; +//import com.example.demo.form.EmployeeForm; +//import com.example.demo.repository.EmployeeRepository; +// +///** EMP-01:社員登録、EMP-02:社員編集 */ +//@Service +//public class EmployeeService { +// private static final Pattern MAIL = Pattern.compile("^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,}$"); +// private static final String MSG_DUP = "このメールアドレスは既に登録されています。"; +// +// private final EmployeeRepository employeeRepository; +// +// public EmployeeService(EmployeeRepository employeeRepository) { +// this.employeeRepository = employeeRepository; +// } +// +// // ---------- 検索 ---------- +// +// @Transactional(readOnly = true) +// public Optional findByMail(String mail) { +// return employeeRepository.findByMailAddress(mail == null ? "" : mail.trim()); +// } +// +// @Transactional(readOnly = true) +// public Employee find(Integer id) { +// return employeeRepository.findById(id) +// .orElseThrow(() -> new BusinessException("対象の社員が見つかりません。")); +// } +// +// /** 編集画面の初期値(パスワードは空:管理者は変更時のみ入力) */ +// public EmployeeForm toForm(Employee e) { +// EmployeeForm f = new EmployeeForm(); +// f.setIdEmployee(e.getIdEmployee()); +// f.setNmEmployee(e.getNmEmployee()); +// f.setKnEmployee(e.getKnEmployee()); +// f.setMailAddress(e.getMailAddress()); +// f.setFlgAdmin(e.getFlgAdmin()); +// f.setFlgRetirement(e.getFlgRetirement()); +// return f; +// } +// +// // ---------- 入力チェック ---------- +// +// /** 社員登録:社員名・よみ・メール・管理者フラグ。パスワードは初期値で登録する。 */ +// public List validateRegist(EmployeeForm f) { +// f.trim(); +// List e = new ArrayList<>(); +// checkBasic(f, e); +// checkMail(f, null, e); +// checkFlag(f.getFlgAdmin(), "管理者フラグ", e); +// return e; +// } +// +// /** 管理者による編集:全項目。パスワードは空なら変更しない。 */ +// public List validateUpdateByAdmin(EmployeeForm f) { +// f.trim(); +// List e = new ArrayList<>(); +// checkBasic(f, e); +// checkMail(f, f.getIdEmployee(), e); +// checkFlag(f.getFlgAdmin(), "管理者フラグ", e); +// checkFlag(f.getFlgRetirement(), "退職フラグ", e); +// if (f.getPassword().length() > 256) +// e.add("パスワードは256文字以内で入力してください。"); +// return e; +// } +// +// /** 一般ユーザーによる編集:パスワードのみ(必須)。 */ +// public List validateUpdateBySelf(EmployeeForm f) { +// f.trim(); +// List e = new ArrayList<>(); +// if (f.getPassword().isEmpty()) +// e.add("パスワードを入力してください。"); +// else if (f.getPassword().length() > 256) +// e.add("パスワードは256文字以内で入力してください。"); +// return e; +// } +// +// private void checkBasic(EmployeeForm f, List e) { +// if (f.getNmEmployee().isEmpty()) +// e.add("社員名を入力してください。"); +// else if (f.getNmEmployee().length() > 50) +// e.add("社員名は50文字以内で入力してください。"); +// // ※社員名よみの文字種(ひらがなのみ等)は設計で確定する。現状は文字数のみチェック +// if (f.getKnEmployee().isEmpty()) +// e.add("社員名よみを入力してください。"); +// else if (f.getKnEmployee().length() > 100) +// e.add("社員名よみは100文字以内で入力してください。"); +// else if (!f.getKnEmployee().matches("^[\\u3040-\\u309F]+$")) +// e.add("社員名よみはひらがなで入力してください"); +// } +// +// private void checkMail(EmployeeForm f, Integer selfId, List e) { +// String m = f.getMailAddress(); +// if (m.isEmpty()) { +// e.add("メールアドレスを入力してください。"); +// return; +// } +// if (m.length() > 256) { +// e.add("メールアドレスは256文字以内で入力してください。"); +// return; +// } +// if (!MAIL.matcher(m).matches()) { +// e.add("メールアドレスの形式が正しくありません。"); +// return; +// } +// Optional dup = employeeRepository.findByMailAddress(m); +// if (dup.isPresent() && !dup.get().getIdEmployee().equals(selfId)) +// e.add(MSG_DUP); +// } +// +// private static void checkFlag(String v, String label, List e) { +// if (!"0".equals(v) && !"1".equals(v)) +// e.add(label + "が正しくありません。"); +// } +// +// // ---------- 登録・更新 ---------- +// +// @Transactional +// public Integer register(EmployeeForm f, LoginUser admin) { +// raise(validateRegist(f)); +// Employee e = new Employee(); +// e.setNmEmployee(f.getNmEmployee()); +// e.setKnEmployee(f.getKnEmployee()); +// e.setMailAddress(f.getMailAddress()); +// e.setPassword(Const.DEFAULT_PASSWORD); +// e.setFlgAdmin(f.getFlgAdmin()); +// e.setFlgRetirement(Const.RETIRE_NO); +// e.setIdUpdate(admin.getIdEmployee()); +// e.setDateUpdate(LocalDateTime.now()); +// try { +// return employeeRepository.saveAndFlush(e).getIdEmployee(); +// } catch (DataIntegrityViolationException ex) { +// throw new BusinessException(MSG_DUP); // 同時登録によるUNIQUE制約違反 +// } +// } +// +// @Transactional +// public void updateByAdmin(EmployeeForm f, LoginUser admin) { +// raise(validateUpdateByAdmin(f)); +// Employee e = find(f.getIdEmployee()); +// e.setNmEmployee(f.getNmEmployee()); +// e.setKnEmployee(f.getKnEmployee()); +// e.setMailAddress(f.getMailAddress()); +// if (!f.getPassword().isEmpty()) +// e.setPassword(f.getPassword()); +// e.setFlgAdmin(f.getFlgAdmin()); +// e.setFlgRetirement(f.getFlgRetirement()); +// e.setIdUpdate(admin.getIdEmployee()); +// e.setDateUpdate(LocalDateTime.now()); +// try { +// employeeRepository.saveAndFlush(e); +// } catch (DataIntegrityViolationException ex) { +// throw new BusinessException(MSG_DUP); +// } +// } +// +// /** 一般ユーザー:自分自身(セッションの社員ID)のパスワードのみ更新する。 */ +// @Transactional +// public void updateBySelf(EmployeeForm f, LoginUser self) { +// raise(validateUpdateBySelf(f)); +// Employee e = find(self.getIdEmployee()); +// e.setPassword(f.getPassword()); +// e.setIdUpdate(self.getIdEmployee()); +// e.setDateUpdate(LocalDateTime.now()); +// } +// +// private static void raise(List errors) { +// if (!errors.isEmpty()) +// throw new BusinessException(String.join(" ", errors)); +// } +//}