Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[refator:] SonarLint로 정적 분석을 통해 취약점이 Major한 코드 변경 #24

Merged
merged 1 commit into from
Jan 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,43 +11,50 @@
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;

import javax.validation.ConstraintViolationException;
import java.util.NoSuchElementException;
import java.util.*;

@RestControllerAdvice //@Conroller 전역에서 발생 가능한 예외를 잡아 처리
@Slf4j
public class GlobalExceptionHandler {
private static ErrorCode errorCode;

private GlobalExceptionHandler() {
}

//ConstraintViolationException.class 는 유효성 검사 실패시 (@Validated)
@ExceptionHandler({JsonParseException.class,
MethodArgumentNotValidException.class,
ConstraintViolationException.class,
ConstraintViolationException.class,
MethodArgumentTypeMismatchException.class,
MissingServletRequestParameterException.class})
public static ResponseEntity<ErrorResponse> missMatchExceptionHandler(){
public static ResponseEntity<Object> missMatchExceptionHandler() {
errorCode = ErrorCode.INVALID_VALUE;
log.error(String.valueOf(errorCode.getHttpStatus()), errorCode.getMessage());
return ResponseEntity.status(errorCode.getHttpStatus()).body(ErrorResponse.res(errorCode.getStatus(), errorCode.getMessage(), errorCode.getReason()));
return ResponseEntity.status(errorCode.getHttpStatus()).body(
ErrorResponse.res(errorCode.getStatus(), errorCode.getMessage(), errorCode.getReason()));
}

@ExceptionHandler(CustomException.class)
public static ResponseEntity customExceptionHandler(CustomException e){
public static ResponseEntity<Object> customExceptionHandler(CustomException e) {
errorCode = e.getErrorCode();
log.error(String.valueOf(errorCode.getHttpStatus()), errorCode.getMessage());
return ResponseEntity.status(errorCode.getHttpStatus()).body(ErrorResponse.res(errorCode.getStatus(), errorCode.getMessage(), errorCode.getReason()));
return ResponseEntity.status(errorCode.getHttpStatus()).body(
ErrorResponse.res(errorCode.getStatus(), errorCode.getMessage(), errorCode.getReason()));
}

@ExceptionHandler({NoSuchElementException.class, NotFoundException.class})
public static ResponseEntity notFoundExceptionHandler(){
public static ResponseEntity<Object> notFoundExceptionHandler() {
errorCode = ErrorCode.NOT_FOUND;
log.error(String.valueOf(errorCode.getHttpStatus()), errorCode.getMessage());
return ResponseEntity.status(errorCode.getHttpStatus()).body(ErrorResponse.res(errorCode.getStatus(), errorCode.getMessage(), errorCode.getReason()));
return ResponseEntity.status(errorCode.getHttpStatus()).body(
ErrorResponse.res(errorCode.getStatus(), errorCode.getMessage(), errorCode.getReason()));
}

@ExceptionHandler(Exception.class)
public static ResponseEntity IllExceptionHandler(Exception e){
public static ResponseEntity<Object> illExceptionHandler(Exception e) {
errorCode = ErrorCode.SERVER_ERROR;
log.error(String.valueOf(errorCode.getHttpStatus()), errorCode.getMessage(), e);
return ResponseEntity.status(errorCode.getHttpStatus()).body(ErrorResponse.res(errorCode.getStatus(), errorCode.getMessage(), errorCode.getReason()));
return ResponseEntity.status(errorCode.getHttpStatus()).body(
ErrorResponse.res(errorCode.getStatus(), errorCode.getMessage(), errorCode.getReason()));
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package comento.backend.ticket.controller;

import comento.backend.ticket.config.ErrorCode;
import comento.backend.ticket.config.ErrorResponse;
import comento.backend.ticket.config.SuccessCode;
import comento.backend.ticket.config.SuccessResponse;
import comento.backend.ticket.config.customException.DuplicatedException;
Expand All @@ -13,7 +12,6 @@
import comento.backend.ticket.dto.BookingResponseCreated;
import comento.backend.ticket.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

Expand All @@ -30,7 +28,7 @@ public class BookingController {
private final SeatService seatService;

private SuccessCode successCode;
private BookingResponseCreated result;
private BookingResponseCreated bookingResponseCreated;

@Autowired
public BookingController(BookingService bookingService, BookingHistoryService bookingHistoryService, UserService userService, PerformanceService performanceService, SeatService seatService) {
Expand All @@ -42,7 +40,7 @@ public BookingController(BookingService bookingService, BookingHistoryService bo
}

@PostMapping("")
public ResponseEntity addBooking(@Valid @RequestBody BookingDto reqBooking){
public ResponseEntity<Object> addBooking(@Valid @RequestBody BookingDto reqBooking){
final User user = userService.getUser(reqBooking.getEmail());
final Performance performance = performanceService.getPerformance(reqBooking.getId(), reqBooking.getTitle());
final Seat seat = seatService.getIsBooking(performance, reqBooking.getSeatType(), reqBooking.getSeatNumber()); //false라면 예약 가능
Expand All @@ -53,18 +51,18 @@ public ResponseEntity addBooking(@Valid @RequestBody BookingDto reqBooking){
}else{
bookingService.saveBooking(user, performance, seat, reqBooking);
bookingHistoryService.saveBookingSucessLog(user, performance, seat);
result = new BookingResponseCreated(reqBooking.getSeatType(), reqBooking.getSeatNumber());
bookingResponseCreated = new BookingResponseCreated(reqBooking.getSeatType(), reqBooking.getSeatNumber());
successCode = SuccessCode.CREATED;
}
return ResponseEntity.status(successCode.getHttpStatus())
.body(SuccessResponse.res(successCode.getStatus(), successCode.getMessage(), result));
.body(SuccessResponse.res(successCode.getStatus(), successCode.getMessage(), bookingResponseCreated));
}

@GetMapping("/email/{email}")
public ResponseEntity showMyBooking(@Valid @PathVariable String email){
List<BookingResponse> result = bookingService.getMyBooking(email);
public ResponseEntity<Object> showMyBooking(@Valid @PathVariable String email){
List<BookingResponse> myBooking = bookingService.getMyBooking(email);
successCode = SuccessCode.OK;
return ResponseEntity.status(successCode.getHttpStatus())
.body(SuccessResponse.res(successCode.getStatus(), successCode.getMessage(), result));
.body(SuccessResponse.res(successCode.getStatus(), successCode.getMessage(), myBooking));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,13 @@

import comento.backend.ticket.config.SuccessCode;
import comento.backend.ticket.config.SuccessResponse;
import comento.backend.ticket.domain.Performance;
import comento.backend.ticket.dto.PerformanceDto;
import comento.backend.ticket.dto.PerformanceResponse;
import comento.backend.ticket.dto.SeatResponse;
import comento.backend.ticket.service.PerformanceService;
import comento.backend.ticket.service.SeatService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
Expand All @@ -36,7 +33,7 @@ public PerformanceController(PerformanceService performanceService, SeatService
}

@GetMapping("/info")
public ResponseEntity showPerformanceInfo(@Valid @RequestParam(value = "date", required = true)
public ResponseEntity<Object> showPerformanceInfo(@Valid @RequestParam(value = "date", required = true)
@DateTimeFormat(pattern = "yyyy-MM-dd") Date date,
@Valid @RequestParam(value = "title", required = false) String title) {
PerformanceDto performanceDto = new PerformanceDto(title, date);
Expand All @@ -45,7 +42,7 @@ public ResponseEntity showPerformanceInfo(@Valid @RequestParam(value = "date", r
}

@GetMapping("/info/seat")
public ResponseEntity showPerformanceSeatInfo(@Valid @RequestParam(value = "date", required = true)
public ResponseEntity<Object> showPerformanceSeatInfo(@Valid @RequestParam(value = "date", required = true)
@DateTimeFormat(pattern = "yyyy-MM-dd") Date date,
@Valid @RequestParam(value = "title", required = true) String title) {
PerformanceDto performanceDto = new PerformanceDto(title, date);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,27 @@

import comento.backend.ticket.config.SuccessCode;
import comento.backend.ticket.config.SuccessResponse;
import comento.backend.ticket.config.customException.NotFoundDataException;
import comento.backend.ticket.domain.User;
import comento.backend.ticket.dto.UserDto;
import comento.backend.ticket.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api")
public class UserController {
private static final String CREATED_MSG = "등록 성공";
private SuccessCode successCode = SuccessCode.CREATED;
private final UserService userService;
private static final String CREATED_MSG = "등록 성공";

@Autowired
public UserController(UserService userService) {
this.userService = userService;
}

@PostMapping("/signup")
public ResponseEntity addEmail(@Validated @RequestBody UserDto userDto){
@PostMapping("/signup")
public ResponseEntity<Object> addEmail(@Validated @RequestBody UserDto userDto){
userService.saveUser(userDto);
return ResponseEntity.status(successCode.getHttpStatus())
.body(SuccessResponse.res(successCode.getStatus(), successCode.getMessage(), CREATED_MSG));
Expand Down
3 changes: 1 addition & 2 deletions ticket/src/main/java/comento/backend/ticket/domain/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import lombok.Builder;
import lombok.Data;
import org.hibernate.annotations.CreationTimestamp;
import org.springframework.data.annotation.CreatedDate;

import javax.persistence.*;
import java.util.Date;
Expand All @@ -29,7 +28,7 @@ public class User{
public User(){}

@Builder
public User(long id, String email, Date create_at) {
public User(long id, String email, Date createAt) {
this.id = id;
this.email = email;
this.createAt = createAt;
Expand Down
10 changes: 4 additions & 6 deletions ticket/src/main/java/comento/backend/ticket/dto/SeatDto.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,15 @@

@Data
public class SeatDto {
private Long seat_id;
private Long seatId;
private Performance performance;
private SeatType seatType;
private Integer seatNumber;
private boolean isBooking;

public SeatDto(){};

@Builder
public SeatDto(Long seat_id, Performance performance, SeatType seatType, Integer seatNumber, boolean isBooking) {
this.seat_id = seat_id;
public SeatDto(Long seatId, Performance performance, SeatType seatType, Integer seatNumber, boolean isBooking) {
this.seatId = seatId;
this.performance = performance;
this.seatType = seatType;
this.seatNumber = seatNumber;
Expand All @@ -27,7 +25,7 @@ public SeatDto(Long seat_id, Performance performance, SeatType seatType, Integer

public Seat toEntity(){
return Seat.builder()
.id(seat_id)
.id(seatId)
.performance(performance)
.seatType(seatType)
.seatNumber(seatNumber)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import comento.backend.ticket.dto.BookingHistoryDto;
import comento.backend.ticket.repository.BookingHistoryRepository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class BookingHistoryService {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public Booking saveBooking(final User user, final Performance performance, final

private void updateSeat(final Seat seat, final Performance performance, final BookingDto reqBooking) {
SeatDto seatDto = SeatDto.builder()
.seat_id(seat.getId())
.seatId(seat.getId())
.performance(performance)
.seatType(reqBooking.getSeatType())
.seatNumber(reqBooking.getSeatNumber())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ public Seat updateSeat(final SeatDto seatDto) {

@Transactional(readOnly = true)
public Seat getIsBooking(final Performance performance, final SeatType seatType, final Integer seatNumber){
Seat seatIsBooking = seatRepository.findByPerformanceAndSeatTypeAndSeatNumber(performance, seatType, seatNumber)
return seatRepository.findByPerformanceAndSeatTypeAndSeatNumber(performance, seatType, seatNumber)
.orElseGet(()-> null);
return seatIsBooking;
}
}