From c3ad0ce599738eca33cba3d7a1eb37e432c0c9f0 Mon Sep 17 00:00:00 2001 From: JongbeomLee623 Date: Fri, 1 Nov 2024 16:51:44 +0900 Subject: [PATCH] =?UTF-8?q?BE:=20[feature]=20=EA=B4=80=EB=A6=AC=EC=9E=90?= =?UTF-8?q?=20=ED=9A=8C=EC=9B=90=EA=B0=80=EC=9E=85/=EB=A1=9C=EA=B7=B8?= =?UTF-8?q?=EC=9D=B8=20=EA=B5=AC=ED=98=84=20#1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/backend/Eyesee/build.gradle | 1 + .../fortune/eyesee/config/SecurityConfig.java | 30 +++++++++++++ .../eyesee/controller/AdminController.java | 40 +++++++++++++++++ .../com/fortune/eyesee/dto/AdminLoginDTO.java | 9 ++++ .../fortune/eyesee/dto/AdminSignupDTO.java | 11 +++++ .../eyesee/repository/AdminRepository.java | 10 +++++ .../fortune/eyesee/service/AdminService.java | 45 +++++++++++++++++++ 7 files changed, 146 insertions(+) create mode 100644 src/backend/Eyesee/src/main/java/com/fortune/eyesee/config/SecurityConfig.java create mode 100644 src/backend/Eyesee/src/main/java/com/fortune/eyesee/controller/AdminController.java create mode 100644 src/backend/Eyesee/src/main/java/com/fortune/eyesee/dto/AdminLoginDTO.java create mode 100644 src/backend/Eyesee/src/main/java/com/fortune/eyesee/dto/AdminSignupDTO.java create mode 100644 src/backend/Eyesee/src/main/java/com/fortune/eyesee/repository/AdminRepository.java create mode 100644 src/backend/Eyesee/src/main/java/com/fortune/eyesee/service/AdminService.java diff --git a/src/backend/Eyesee/build.gradle b/src/backend/Eyesee/build.gradle index 27f006e..0ce4d86 100644 --- a/src/backend/Eyesee/build.gradle +++ b/src/backend/Eyesee/build.gradle @@ -28,6 +28,7 @@ dependencies { implementation 'org.springframework.boot:spring-boot-starter-web' implementation 'org.springframework.boot:spring-boot-starter-webflux' implementation 'org.springframework.boot:spring-boot-starter-websocket' + implementation 'org.springframework.boot:spring-boot-starter-security' compileOnly 'org.projectlombok:lombok' runtimeOnly 'com.mysql:mysql-connector-j' annotationProcessor 'org.projectlombok:lombok' diff --git a/src/backend/Eyesee/src/main/java/com/fortune/eyesee/config/SecurityConfig.java b/src/backend/Eyesee/src/main/java/com/fortune/eyesee/config/SecurityConfig.java new file mode 100644 index 0000000..517f3cb --- /dev/null +++ b/src/backend/Eyesee/src/main/java/com/fortune/eyesee/config/SecurityConfig.java @@ -0,0 +1,30 @@ +package com.fortune.eyesee.config; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.security.config.annotation.web.builders.HttpSecurity; +import org.springframework.security.web.SecurityFilterChain; +import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; +import org.springframework.security.crypto.password.PasswordEncoder; + +@Configuration +public class SecurityConfig { + + @Bean + public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception { + http + .csrf(csrf -> csrf.disable()) // CSRF 비활성화 (필요시 활성화 가능) + .authorizeHttpRequests(auth -> auth + .requestMatchers("/api/admin/signup", "/api/admin/login").permitAll() // 회원가입, 로그인은 인증 필요 없음 + .anyRequest().authenticated() // 나머지 요청은 인증 필요 + ) + .formLogin(form -> form.disable()); // 기본 로그인 폼 비활성화 + + return http.build(); + } + + @Bean + public PasswordEncoder passwordEncoder() { + return new BCryptPasswordEncoder(); + } +} \ No newline at end of file diff --git a/src/backend/Eyesee/src/main/java/com/fortune/eyesee/controller/AdminController.java b/src/backend/Eyesee/src/main/java/com/fortune/eyesee/controller/AdminController.java new file mode 100644 index 0000000..9dff7e1 --- /dev/null +++ b/src/backend/Eyesee/src/main/java/com/fortune/eyesee/controller/AdminController.java @@ -0,0 +1,40 @@ +package com.fortune.eyesee.controller; + +import com.fortune.eyesee.dto.AdminLoginDTO; +import com.fortune.eyesee.dto.AdminSignupDTO; +import com.fortune.eyesee.entity.Admin; +import com.fortune.eyesee.service.AdminService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.*; + +import jakarta.servlet.http.HttpSession; + +@RestController +@RequestMapping("/api/admin") +public class AdminController { + @Autowired + private AdminService adminService; + + // 회원가입 API + @PostMapping("/signup") + public ResponseEntity registerAdmin(@RequestBody AdminSignupDTO adminSignupDTO) { + adminService.registerAdmin(adminSignupDTO); + return ResponseEntity.ok("회원가입 성공"); + } + + // 로그인 API + @PostMapping("/login") + public ResponseEntity loginAdmin(@RequestBody AdminLoginDTO adminLoginDTO, HttpSession session) { + Admin admin = adminService.loginAdmin(adminLoginDTO); + session.setAttribute("admin", admin); // 세션에 로그인 정보 저장 + return ResponseEntity.ok("로그인 성공"); + } + + // 로그아웃 API + @PostMapping("/logout") + public ResponseEntity logoutAdmin(HttpSession session) { + session.invalidate(); // 세션 무효화 + return ResponseEntity.ok("로그아웃 성공"); + } +} \ No newline at end of file diff --git a/src/backend/Eyesee/src/main/java/com/fortune/eyesee/dto/AdminLoginDTO.java b/src/backend/Eyesee/src/main/java/com/fortune/eyesee/dto/AdminLoginDTO.java new file mode 100644 index 0000000..d6b5983 --- /dev/null +++ b/src/backend/Eyesee/src/main/java/com/fortune/eyesee/dto/AdminLoginDTO.java @@ -0,0 +1,9 @@ +package com.fortune.eyesee.dto; + +import lombok.Data; + +@Data +public class AdminLoginDTO { + private String adminEmail; + private String password; +} \ No newline at end of file diff --git a/src/backend/Eyesee/src/main/java/com/fortune/eyesee/dto/AdminSignupDTO.java b/src/backend/Eyesee/src/main/java/com/fortune/eyesee/dto/AdminSignupDTO.java new file mode 100644 index 0000000..3f471ee --- /dev/null +++ b/src/backend/Eyesee/src/main/java/com/fortune/eyesee/dto/AdminSignupDTO.java @@ -0,0 +1,11 @@ +package com.fortune.eyesee.dto; + +import lombok.Data; + +@Data +public class AdminSignupDTO { + private String adminEmail; + private String password; + private String passwordConfirm; + private String adminName; // 회원가입 시 필요한 이름 필드 +} \ No newline at end of file diff --git a/src/backend/Eyesee/src/main/java/com/fortune/eyesee/repository/AdminRepository.java b/src/backend/Eyesee/src/main/java/com/fortune/eyesee/repository/AdminRepository.java new file mode 100644 index 0000000..b520a7e --- /dev/null +++ b/src/backend/Eyesee/src/main/java/com/fortune/eyesee/repository/AdminRepository.java @@ -0,0 +1,10 @@ +package com.fortune.eyesee.repository; + +import com.fortune.eyesee.entity.Admin; +import org.springframework.data.jpa.repository.JpaRepository; + +import java.util.Optional; + +public interface AdminRepository extends JpaRepository { + Optional findByAdminEmail(String adminEmail); +} \ No newline at end of file diff --git a/src/backend/Eyesee/src/main/java/com/fortune/eyesee/service/AdminService.java b/src/backend/Eyesee/src/main/java/com/fortune/eyesee/service/AdminService.java new file mode 100644 index 0000000..d32247b --- /dev/null +++ b/src/backend/Eyesee/src/main/java/com/fortune/eyesee/service/AdminService.java @@ -0,0 +1,45 @@ +package com.fortune.eyesee.service; + +import com.fortune.eyesee.dto.AdminLoginDTO; +import com.fortune.eyesee.dto.AdminSignupDTO; +import com.fortune.eyesee.entity.Admin; +import com.fortune.eyesee.repository.AdminRepository; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.security.crypto.password.PasswordEncoder; +import org.springframework.stereotype.Service; + +@Service +public class AdminService { + @Autowired + private AdminRepository adminRepository; + + @Autowired + private PasswordEncoder passwordEncoder; + + // 회원가입 메서드 + public Admin registerAdmin(AdminSignupDTO adminSignupDTO) { + if (adminRepository.findByAdminEmail(adminSignupDTO.getAdminEmail()).isPresent()) { + throw new IllegalArgumentException("이미 사용 중인 이메일입니다."); + } + if (!adminSignupDTO.getPassword().equals(adminSignupDTO.getPasswordConfirm())) { + throw new IllegalArgumentException("비밀번호가 일치하지 않습니다."); + } + + Admin admin = new Admin(); + admin.setAdminEmail(adminSignupDTO.getAdminEmail()); + admin.setPassword(passwordEncoder.encode(adminSignupDTO.getPassword())); + admin.setAdminName(adminSignupDTO.getAdminName()); + return adminRepository.save(admin); + } + + // 로그인 메서드 + public Admin loginAdmin(AdminLoginDTO adminLoginDTO) { + Admin admin = adminRepository.findByAdminEmail(adminLoginDTO.getAdminEmail()) + .orElseThrow(() -> new IllegalArgumentException("이메일 또는 비밀번호가 잘못되었습니다.")); + + if (!passwordEncoder.matches(adminLoginDTO.getPassword(), admin.getPassword())) { + throw new IllegalArgumentException("이메일 또는 비밀번호가 잘못되었습니다."); + } + return admin; + } +} \ No newline at end of file