Skip to content

Commit

Permalink
BE: [feature] 관리자 회원가입/로그인 구현 #1
Browse files Browse the repository at this point in the history
  • Loading branch information
JongbeomLee623 committed Nov 1, 2024
1 parent 33417ec commit c3ad0ce
Show file tree
Hide file tree
Showing 7 changed files with 146 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/backend/Eyesee/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
@@ -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<String> registerAdmin(@RequestBody AdminSignupDTO adminSignupDTO) {
adminService.registerAdmin(adminSignupDTO);
return ResponseEntity.ok("회원가입 성공");
}

// 로그인 API
@PostMapping("/login")
public ResponseEntity<String> loginAdmin(@RequestBody AdminLoginDTO adminLoginDTO, HttpSession session) {
Admin admin = adminService.loginAdmin(adminLoginDTO);
session.setAttribute("admin", admin); // 세션에 로그인 정보 저장
return ResponseEntity.ok("로그인 성공");
}

// 로그아웃 API
@PostMapping("/logout")
public ResponseEntity<String> logoutAdmin(HttpSession session) {
session.invalidate(); // 세션 무효화
return ResponseEntity.ok("로그아웃 성공");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.fortune.eyesee.dto;

import lombok.Data;

@Data
public class AdminLoginDTO {
private String adminEmail;
private String password;
}
Original file line number Diff line number Diff line change
@@ -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; // 회원가입 시 필요한 이름 필드
}
Original file line number Diff line number Diff line change
@@ -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<Admin, Integer> {
Optional<Admin> findByAdminEmail(String adminEmail);
}
Original file line number Diff line number Diff line change
@@ -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;
}
}

0 comments on commit c3ad0ce

Please sign in to comment.