-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Feat(*): 개발용 로그인 구현 * Fix(*): exception 오타 수정
- Loading branch information
Showing
7 changed files
with
160 additions
and
8 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
33 changes: 33 additions & 0 deletions
33
jabiseo-api/src/main/java/com/jabiseo/auth/application/DevLoginHelper.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,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); | ||
} | ||
|
||
} |
39 changes: 39 additions & 0 deletions
39
jabiseo-api/src/main/java/com/jabiseo/auth/controller/DevAuthController.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,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); | ||
} | ||
|
||
} |
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
62 changes: 62 additions & 0 deletions
62
jabiseo-api/src/test/java/com/jabiseo/auth/controller/DevAuthControllerTest.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,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")); | ||
} | ||
} |
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
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