Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
hyeona01 committed Nov 4, 2024
2 parents 5cc413e + 6274491 commit 53d65d4
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,20 @@
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.CorsConfigurationSource;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;

import java.util.List;

@Configuration
public class SecurityConfig {

@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
http
.csrf(csrf -> csrf.disable()) // CSRF 비활성화 (필요시 활성화 가능)
.cors(cors -> cors.configurationSource(corsConfigurationSource())) // CORS 설정 추가
.csrf(csrf -> csrf.disable()) // CSRF 비활성화 (필요시 활성화 가능)
.authorizeHttpRequests(auth -> auth
.requestMatchers("/api/admin/signup", "/api/admin/login").permitAll() // 회원가입, 로그인은 인증 필요 없음
.anyRequest().permitAll() // 나머지 요청도 인증 필요 없음
Expand All @@ -23,8 +29,21 @@ public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Excepti
return http.build();
}

@Bean
public CorsConfigurationSource corsConfigurationSource() {
CorsConfiguration configuration = new CorsConfiguration();
configuration.setAllowedOrigins(List.of("http://example.com", "http://localhost:3000")); // 허용할 도메인 설정
configuration.setAllowedMethods(List.of("GET", "POST", "PUT", "DELETE", "OPTIONS")); // 허용할 HTTP 메서드
configuration.setAllowedHeaders(List.of("*")); // 모든 헤더 허용
configuration.setAllowCredentials(true); // 인증 정보 포함 여부

UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration("/**", configuration);
return source;
}

@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,40 +24,72 @@ public class ExamController {
@Autowired
private ExamService examService;

// // "before" 상태의 Exam 리스트 조회
// @GetMapping("/before")
// public ResponseEntity<BaseResponse<List<ExamResponseDTO>>> getBeforeExams(HttpSession session) {
// return getExamsByStatus("BEFORE", session);
// }
//
// // "in-progress" 상태의 Exam 리스트 조회
// @GetMapping("/in-progress")
// public ResponseEntity<BaseResponse<List<ExamResponseDTO>>> getInProgressExams(HttpSession session) {
// return getExamsByStatus("IN_PROGRESS", session);
// }
//
// // "done" 상태의 Exam 리스트 조회
// @GetMapping("/done")
// public ResponseEntity<BaseResponse<List<ExamResponseDTO>>> getDoneExams(HttpSession session) {
// return getExamsByStatus("DONE", session);
// }

// 공통 메서드: 상태별 Exam 조회
// private ResponseEntity<BaseResponse<List<ExamResponseDTO>>> getExamsByStatus(String status, HttpSession session) {
// Integer adminId = (Integer) session.getAttribute("adminId");
// if (adminId == null) {
// throw new BaseException(BaseResponseCode.UNAUTHORIZED);
// }
//
// ExamStatus examStatus = ExamStatus.fromString(status);
// if (examStatus == null) {
// throw new BaseException(BaseResponseCode.INVALID_STATUS);
// }
//
// List<ExamResponseDTO> examList = examService.getExamsByStatus(adminId, examStatus);
// return ResponseEntity.ok(new BaseResponse<>(examList));
// }

// "before" 상태의 Exam 리스트 조회
@GetMapping("/before")
public ResponseEntity<BaseResponse<List<ExamResponseDTO>>> getBeforeExams(HttpSession session) {
return getExamsByStatus("BEFORE", session);
public ResponseEntity<BaseResponse<List<ExamResponseDTO>>> getBeforeExams() {
return getExamsByStatus("BEFORE");
}

// "in-progress" 상태의 Exam 리스트 조회
@GetMapping("/in-progress")
public ResponseEntity<BaseResponse<List<ExamResponseDTO>>> getInProgressExams(HttpSession session) {
return getExamsByStatus("IN_PROGRESS", session);
public ResponseEntity<BaseResponse<List<ExamResponseDTO>>> getInProgressExams() {
return getExamsByStatus("IN_PROGRESS");
}

// "done" 상태의 Exam 리스트 조회
@GetMapping("/done")
public ResponseEntity<BaseResponse<List<ExamResponseDTO>>> getDoneExams(HttpSession session) {
return getExamsByStatus("DONE", session);
public ResponseEntity<BaseResponse<List<ExamResponseDTO>>> getDoneExams() {
return getExamsByStatus("DONE");
}

// 공통 메서드: 상태별 Exam 조회
private ResponseEntity<BaseResponse<List<ExamResponseDTO>>> getExamsByStatus(String status, HttpSession session) {
Integer adminId = (Integer) session.getAttribute("adminId");
if (adminId == null) {
throw new BaseException(BaseResponseCode.UNAUTHORIZED);
}

// 공통 메서드: 상태별 Exam 조회 (세션 검증 없이)
private ResponseEntity<BaseResponse<List<ExamResponseDTO>>> getExamsByStatus(String status) {
ExamStatus examStatus = ExamStatus.fromString(status);
if (examStatus == null) {
throw new BaseException(BaseResponseCode.INVALID_STATUS);
}

List<ExamResponseDTO> examList = examService.getExamsByStatus(adminId, examStatus);
// adminId를 사용하지 않는 조회 방식으로 수정
List<ExamResponseDTO> examList = examService.getExamsByStatus(null, examStatus);
return ResponseEntity.ok(new BaseResponse<>(examList));
}



// ExamCode로 특정 Exam 조회 (POST 요청)
@PostMapping("/{examId}/code")
public ResponseEntity<BaseResponse<ExamResponseDTO>> getExamByCode(@PathVariable Integer examId, @RequestBody ExamCodeRequestDTO examCodeRequestDTO, HttpSession session) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,8 @@ public interface ExamRepository extends JpaRepository<Exam, Integer> {

// 랜덤 코드와 adminId로 Exam 조회
Exam findByExamRandomCode(String examRandomCode);


// adminId 없이 상태로만 Exam 조회
List<Exam> findByExamStatus(ExamStatus examStatus);
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,25 +50,66 @@ public boolean existsById(Integer examId) {
}

// Admin ID와 ExamStatus로 시험 목록 조회
// public List<ExamResponseDTO> getExamsByStatus(Integer adminId, ExamStatus examStatus) {
// return examRepository.findByAdmin_AdminIdAndExamStatus(adminId, examStatus).stream()
// .map(exam -> new ExamResponseDTO(
// exam.getExamId(),
// exam.getExamName(),
// exam.getExamSemester(),
// exam.getExamStudentNumber(),
// exam.getExamLocation(),
// exam.getExamDate(),
// exam.getExamStartTime(),
// exam.getExamDuration(),
// exam.getExamStatus(),
// exam.getExamNotice(),
// exam.getSession() != null ? exam.getSession().getSessionId() : null,
// exam.getExamRandomCode()
// ))
// .collect(Collectors.toList());
// }

// Admin ID 없이 ExamStatus로만 시험 목록 조회
public List<ExamResponseDTO> getExamsByStatus(Integer adminId, ExamStatus examStatus) {
return examRepository.findByAdmin_AdminIdAndExamStatus(adminId, examStatus).stream()
.map(exam -> new ExamResponseDTO(
exam.getExamId(),
exam.getExamName(),
exam.getExamSemester(),
exam.getExamStudentNumber(),
exam.getExamLocation(),
exam.getExamDate(),
exam.getExamStartTime(),
exam.getExamDuration(),
exam.getExamStatus(),
exam.getExamNotice(),
exam.getSession() != null ? exam.getSession().getSessionId() : null,
exam.getExamRandomCode()
))
.collect(Collectors.toList());
if (adminId == null) {
return examRepository.findByExamStatus(examStatus).stream()
.map(exam -> new ExamResponseDTO(
exam.getExamId(),
exam.getExamName(),
exam.getExamSemester(),
exam.getExamStudentNumber(),
exam.getExamLocation(),
exam.getExamDate(),
exam.getExamStartTime(),
exam.getExamDuration(),
exam.getExamStatus(),
exam.getExamNotice(),
exam.getSession() != null ? exam.getSession().getSessionId() : null,
exam.getExamRandomCode()
))
.collect(Collectors.toList());
} else {
return examRepository.findByAdmin_AdminIdAndExamStatus(adminId, examStatus).stream()
.map(exam -> new ExamResponseDTO(
exam.getExamId(),
exam.getExamName(),
exam.getExamSemester(),
exam.getExamStudentNumber(),
exam.getExamLocation(),
exam.getExamDate(),
exam.getExamStartTime(),
exam.getExamDuration(),
exam.getExamStatus(),
exam.getExamNotice(),
exam.getSession() != null ? exam.getSession().getSessionId() : null,
exam.getExamRandomCode()
))
.collect(Collectors.toList());
}
}



// ExamCode로 시험 조회
public ExamResponseDTO getExamByCode(String examCode) {
Exam exam = examRepository.findByExamRandomCode(examCode);
Expand Down

0 comments on commit 53d65d4

Please sign in to comment.