-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
33417ec
commit c3ad0ce
Showing
7 changed files
with
146 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
src/backend/Eyesee/src/main/java/com/fortune/eyesee/config/SecurityConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
src/backend/Eyesee/src/main/java/com/fortune/eyesee/controller/AdminController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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("로그아웃 성공"); | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
src/backend/Eyesee/src/main/java/com/fortune/eyesee/dto/AdminLoginDTO.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
11 changes: 11 additions & 0 deletions
11
src/backend/Eyesee/src/main/java/com/fortune/eyesee/dto/AdminSignupDTO.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; // 회원가입 시 필요한 이름 필드 | ||
} |
10 changes: 10 additions & 0 deletions
10
src/backend/Eyesee/src/main/java/com/fortune/eyesee/repository/AdminRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
45 changes: 45 additions & 0 deletions
45
src/backend/Eyesee/src/main/java/com/fortune/eyesee/service/AdminService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |