Skip to content

Commit

Permalink
Feat(*): 개발용 로그인 구현 (#31)
Browse files Browse the repository at this point in the history
* Feat(*): 개발용 로그인 구현

* Fix(*): exception 오타 수정
  • Loading branch information
InHyeok-J authored and morenow98 committed Jul 26, 2024
1 parent 1ee8e78 commit a6ecd4e
Show file tree
Hide file tree
Showing 7 changed files with 160 additions and 8 deletions.
2 changes: 2 additions & 0 deletions jabiseo-api/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,6 @@ dependencies {
implementation 'io.jsonwebtoken:jjwt-api:0.11.2'
runtimeOnly 'io.jsonwebtoken:jjwt-impl:0.11.2'
runtimeOnly 'io.jsonwebtoken:jjwt-jackson:0.11.2'

testImplementation 'org.springframework.security:spring-security-test'
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.jabiseo.auth.application;


import com.jabiseo.auth.dto.LoginResponse;
import com.jabiseo.cache.RedisCacheRepository;
import com.jabiseo.member.domain.Member;
import com.jabiseo.member.domain.MemberRepository;
import com.jabiseo.member.exception.MemberBusinessException;
import com.jabiseo.member.exception.MemberErrorCode;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@RequiredArgsConstructor
@Transactional(readOnly = true)
public class DevLoginHelper {

private final JwtHandler jwtHandler;
private final MemberRepository memberRepository;
private final RedisCacheRepository redisCacheRepository;

public LoginResponse login(String memberId) {
Member member = memberRepository.findById(memberId)
.orElseThrow(() -> new MemberBusinessException(MemberErrorCode.MEMBER_NOT_FOUND));

String accessToken = jwtHandler.createAccessToken(member);
String refreshToken = jwtHandler.createRefreshToken();
redisCacheRepository.saveToken(member.getId(), refreshToken);
return new LoginResponse(accessToken, refreshToken);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.jabiseo.auth.controller;

import com.jabiseo.auth.application.DevLoginHelper;
import com.jabiseo.auth.dto.LoginResponse;
import com.jabiseo.exception.CommonErrorCode;
import com.jabiseo.exception.ErrorResponse;
import jakarta.validation.constraints.NotBlank;
import lombok.RequiredArgsConstructor;
import org.springframework.core.env.Environment;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;

@RestController
@RequiredArgsConstructor
@RequestMapping("/api")
public class DevAuthController {

private final Environment environment;
private final DevLoginHelper loginHelper;
private static final String LIMIT_PROFILE = "local";

@GetMapping("/dev/auth")
public ResponseEntity<?> devAuth(@RequestParam(value = "member-id") @NotBlank String memberId) {
if (!isLocalProfiles(environment.getActiveProfiles())) {
return ResponseEntity.status(CommonErrorCode.FORBIDDEN.getStatusCode()).body(ErrorResponse.of(CommonErrorCode.FORBIDDEN));
}

LoginResponse result = loginHelper.login(memberId);
return ResponseEntity.ok(result);
}


private boolean isLocalProfiles(String[] profiles) {
return Arrays.asList(profiles).contains(LIMIT_PROFILE);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

import com.jabiseo.database.exception.PersistenceException;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.method.annotation.HandlerMethodValidationException;

@Slf4j
@RestControllerAdvice
@RequiredArgsConstructor
public class GlobalExceptionHandler {
Expand All @@ -28,10 +30,22 @@ public ResponseEntity<?> handlePersistenceException(PersistenceException e) {
}

@ExceptionHandler(HandlerMethodValidationException.class)
public ResponseEntity<?> handleMethodArgumentNotValidException(HandlerMethodValidationException e) {
ErrorCode code = CommonErrorCode.INVALID_REQUEST_PARAMETER;
public ResponseEntity<?> handleMethodValidationException(HandlerMethodValidationException e) {
ErrorCode errorCode = CommonErrorCode.INVALID_REQUEST_PARAMETER;
log.error(e.getMessage());
return ResponseEntity
.status(code.getStatusCode())
.body(new ErrorResponse(code.getMessage(), code.getErrorCode()));
.status(errorCode.getStatusCode())
.body(new ErrorResponse(e.getMessage(), errorCode.getErrorCode()));
}

@ExceptionHandler(Exception.class)
public ResponseEntity<?> handleException(Exception e) {
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append(e.getMessage());
stringBuilder.append(CommonErrorCode.INTERNAL_SERVER_ERROR.getMessage());
return ResponseEntity
.status(ErrorCode.INTERNAL_SERVER_ERROR)
.body(new ErrorResponse(stringBuilder.toString(), CommonErrorCode.INTERNAL_SERVER_ERROR.getErrorCode()));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package com.jabiseo.auth.controller;

import com.jabiseo.auth.application.DevLoginHelper;
import com.jabiseo.auth.dto.LoginResponse;
import com.jabiseo.common.security.JwtAuthenticationFilter;
import com.jabiseo.common.security.JwtExceptionFilter;
import com.jabiseo.common.security.SecurityConfig;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.FilterType;
import org.springframework.core.env.Environment;
import org.springframework.mock.env.MockEnvironment;
import org.springframework.security.test.context.support.WithMockUser;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.ResultActions;

import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.*;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;

@ExtendWith(SpringExtension.class)
@WebMvcTest(controllers = DevAuthController.class, excludeFilters = {
@ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, classes = SecurityConfig.class),
@ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, classes = JwtAuthenticationFilter.class),
@ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, classes = JwtExceptionFilter.class)
})
@WithMockUser
class DevAuthControllerTest {

@Autowired
private MockMvc mockMvc;


@MockBean
DevLoginHelper devLoginHelper;

@Test
@DisplayName("개발용 로그인 요청")
void devLoginSuccess() throws Exception {
//given
String memberId = "1234";
given(devLoginHelper.login(memberId)).willReturn(new LoginResponse("accc", "refresh"));

//when
ResultActions perform = mockMvc.perform(get("/api/dev/auth?member-id=" + memberId));

//then
perform.andExpect(status().isOk())
.andExpect(jsonPath("$.accessToken").value("accc"))
.andExpect(jsonPath("$.refreshToken").value("refresh"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
@Getter
public enum CommonErrorCode implements ErrorCode {

INVALID_REQUEST_BODY("invalid request body", "COM_001", ErrorCode.BAD_REQUEST),
INTERNAL_SERVER_ERROR("server error", "COM_002", ErrorCode.INTERNAL_SERVER_ERROR),
INVALID_REQUEST_PARAMETER("invalid request parameter", "COM_003", ErrorCode.BAD_REQUEST),
;
INVALID_REQUEST_BODY("요청 바디가 잘못됨", "COM_001", ErrorCode.BAD_REQUEST),
INTERNAL_SERVER_ERROR("서버 에러", "COM_002", ErrorCode.INTERNAL_SERVER_ERROR),
INVALID_REQUEST_PARAMETER("요청 파라미터가 잘못됨", "COM_003", ErrorCode.BAD_REQUEST),
FORBIDDEN("권한이 없거나 금지된 요청임", "COM_004", ErrorCode.FORBIDDEN);

private final String message;
private final String errorCode;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ public interface ErrorCode {

int INTERNAL_SERVER_ERROR = 500;

int FORBIDDEN = 403;

String getMessage();

String getErrorCode();
Expand Down

0 comments on commit a6ecd4e

Please sign in to comment.