Skip to content

Commit

Permalink
chore: 에러 및 정규 표현식 상수화 (#71)
Browse files Browse the repository at this point in the history
* chore: cors origin 세팅

* chore: 불필요 설정 파일 삭제

* chore: 백엔드 개발서버 url 추가

* chore: 에러메세지 상수화

* chore: 정규 표현식 공통 상수로 묶기

* chore: 유효성 검사 에러메세지에서 필드명 제거
  • Loading branch information
kckc0608 authored Feb 17, 2025
1 parent 83b2e87 commit ff4c4fa
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.keunsori.keunsoriserver.domain.auth.controller;

import static com.keunsori.keunsoriserver.global.exception.ErrorMessage.INVALID_REFRESH_TOKEN;
import static com.keunsori.keunsoriserver.global.exception.ErrorMessage.MEMBER_NOT_EXISTS_WITH_STUDENT_ID;

import com.keunsori.keunsoriserver.domain.auth.login.LoginService;
Expand All @@ -8,7 +9,6 @@
import com.keunsori.keunsoriserver.domain.auth.login.JwtTokenManager;
import com.keunsori.keunsoriserver.domain.auth.login.dto.response.LoginResponse;
import com.keunsori.keunsoriserver.domain.member.domain.Member;
import com.keunsori.keunsoriserver.domain.member.domain.vo.MemberStatus;
import com.keunsori.keunsoriserver.domain.member.repository.MemberRepository;
import com.keunsori.keunsoriserver.global.exception.AuthException;
import com.keunsori.keunsoriserver.global.exception.MemberException;
Expand Down Expand Up @@ -45,7 +45,7 @@ public ResponseEntity<LoginResponse> reissue(@RequestHeader("Refresh-Token") Str
//Redis에 저장된 Refresh Token과 일치하는지 확인
String storedRefreshToken = authService.getRefreshToken(studentId);
if(storedRefreshToken == null || !storedRefreshToken.equals(refreshToken)){
throw new AuthException("유효하지 않은 Refresh Token");
throw new AuthException(INVALID_REFRESH_TOKEN);
}

//새로운 AccessToken 생성
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.keunsori.keunsoriserver.domain.auth.login;

import static com.keunsori.keunsoriserver.global.exception.ErrorMessage.PASSWORD_NOT_CORRECT;
import static com.keunsori.keunsoriserver.global.exception.ErrorMessage.STUDENT_ID_NOT_EXISTS;

import com.keunsori.keunsoriserver.domain.auth.login.dto.request.LoginRequest;
import com.keunsori.keunsoriserver.domain.auth.login.dto.response.LoginResponse;
import com.keunsori.keunsoriserver.domain.auth.redis.RefreshTokenService;
Expand Down Expand Up @@ -27,11 +30,11 @@ public LoginResponse login(LoginRequest loginRequest) {

//학번으로 사용자 조회
Member member= memberRepository.findByStudentIdIgnoreCase(loginRequest.studentId())
.orElseThrow(() -> new MemberException("존재하지 않는 학번입니다."));
.orElseThrow(() -> new MemberException(STUDENT_ID_NOT_EXISTS));

//비밀번호 일치하는지 검증
if(!passwordEncoder.matches(loginRequest.password(), member.getPassword())){
throw new AuthException("비밀번호가 일치하지 않습니다.");
throw new AuthException(PASSWORD_NOT_CORRECT);
}

//Access Token, Refresh Token 생성
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
package com.keunsori.keunsoriserver.domain.auth.login.dto.request;

import static com.keunsori.keunsoriserver.global.constant.RequestFormatConstant.PASSWORD_REGEX;
import static com.keunsori.keunsoriserver.global.constant.RequestFormatConstant.STUDENT_ID_REGEX;

import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.Pattern;

public record LoginRequest(

@NotBlank(message = "아이디를 입력해주세요.")
@Pattern(
regexp = STUDENT_ID_REGEX,
message = "학번 형식이 올바르지 않습니다."
)
String studentId,

@NotBlank(message = "비밀번호를 입력해주세요.")
@Pattern(
regexp = "^(?=.*[!@#$%^&*(),.?\":{}|<>])[a-zA-Z0-9!@#$%^&*(),.?\":{}|<>]{8,25}$",
regexp = PASSWORD_REGEX,
message = "비밀번호는 특수문자, 영문자, 숫자를 포함한 8자리 이상 문자열입니다."
)
String password
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public void saveRefreshToken(String studentId, String refreshToken, long expirat
stringRedisTemplate.opsForValue().set(studentId,refreshToken,expirationTime, TimeUnit.MILLISECONDS);
}

//Refersh Token 조회하기
//Refresh Token 조회하기
public String getRefreshToken(String studentId) {
return stringRedisTemplate.opsForValue().get(studentId);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.keunsori.keunsoriserver.domain.member.sign_up.dto.request;

import static com.keunsori.keunsoriserver.global.constant.RequestFormatConstant.PASSWORD_REGEX;
import static com.keunsori.keunsoriserver.global.constant.RequestFormatConstant.STUDENT_ID_REGEX;

import jakarta.validation.constraints.Email;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.Pattern;
Expand All @@ -10,20 +13,20 @@

public record SignUpRequest(
@NotBlank(message = "이름은 필수 입력값입니다.")
@Pattern(regexp="[가-힣]{1,6}$", message = "이름은 한글 6자 이하로 입력해주세요.")
@Pattern(regexp = "[가-힣]{1,6}$", message = "이름은 한글 6자 이하로 입력해주세요.")
String name,

@NotBlank(message = "학번은 필수 입력값입니다.")
@Pattern(regexp="^[a-zA-Z][0-9]{6}", message="학번을 제대로 입력해주세요.")
@Pattern(regexp = STUDENT_ID_REGEX, message = "학번을 제대로 입력해주세요.")
String studentId,

@NotBlank(message="이메일은 필수 입력값입니다.")
@NotBlank(message = "이메일은 필수 입력값입니다.")
@Email(message = "이메일 형식이 올바르지 않습니다.")
String email,

@NotBlank(message = "비밀번호는 필수 입력값입니다.")
@Pattern(regexp="^(?=.*[!@#$%^&*(),.?\":{}|<>])[a-zA-Z0-9!@#$%^&*(),.?\":{}|<>]{8,25}$",message="비밀번호는 특수문자, 영문자, 숫자를 포함한 8자리 이상 문자열입니다.")
@Pattern(regexp = PASSWORD_REGEX, message = "비밀번호는 특수문자, 영문자, 숫자를 포함한 8자리 이상 문자열입니다.")
String password,

@NotBlank(message="비밀번호를 한 번 더 입력해주세요.")
@NotBlank(message = "비밀번호를 한 번 더 입력해주세요.")
String passwordConfirm) {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.keunsori.keunsoriserver.global.constant;

public class RequestFormatConstant {

public static final String PASSWORD_REGEX = "^(?=.*[!@#$%^&*(),.?\":{}|<>])[a-zA-Z0-9!@#$%^&*(),.?\":{}|<>]{8,25}$";
public static final String STUDENT_ID_REGEX = "^[a-zA-Z][0-9]{6}";
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,9 @@ public class ErrorMessage {
// Admin Reservation
public static final String INVALID_DATE_SCHEDULE = "설정하는 날짜가 이미 지난 날짜입니다.";
public static final String INVALID_SCHEDULE_TIME = "시작 시간과 끝 시간의 순서가 올바르지 않습니다.";

// Auth
public static final String STUDENT_ID_NOT_EXISTS = "존재하지 않는 학번입니다.";
public static final String PASSWORD_NOT_CORRECT = "비밀번호가 일치하지 않습니다.";
public static final String INVALID_REFRESH_TOKEN = "유효하지 않은 리프레시 토큰입니다.";
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.keunsori.keunsoriserver.global.exception;

import org.springframework.context.support.DefaultMessageSourceResolvable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.MethodArgumentNotValidException;
Expand All @@ -19,7 +20,7 @@ public ResponseEntity<ErrorResponse> handleMethodArgumentNotValidException(Metho
.getFieldErrors()
.stream()
.findFirst()
.map(error-> error.getField() + ":" + error.getDefaultMessage())
.map(DefaultMessageSourceResolvable::getDefaultMessage)
.orElse("유효성 검사 실패");

ErrorResponse response = new ErrorResponse(HttpStatus.BAD_REQUEST.value(),errorMessage);
Expand Down

0 comments on commit ff4c4fa

Please sign in to comment.