Compare commits
1 Commits
feature/re
...
feature/pr
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
a2c2b2eaf5 |
1
server/.gitignore
vendored
1
server/.gitignore
vendored
@@ -6,7 +6,6 @@
|
||||
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
|
||||
|
||||
HELP.md
|
||||
bin/**
|
||||
|
||||
# User-specific stuff
|
||||
.idea/**/workspace.xml
|
||||
|
||||
@@ -43,6 +43,8 @@ dependencies {
|
||||
implementation("com.google.code.findbugs:jsr305:3.0.2")
|
||||
implementation ("org.springframework.cloud:spring-cloud-starter-config")
|
||||
implementation("org.springframework.cloud:spring-cloud-starter-openfeign")
|
||||
implementation("io.micrometer:micrometer-core")
|
||||
implementation("io.micrometer:micrometer-registry-prometheus")
|
||||
|
||||
modules {
|
||||
module("org.springframework.boot:spring-boot-starter-logging") {
|
||||
|
||||
@@ -49,7 +49,7 @@ class TicketLockAspectTest {
|
||||
|
||||
assertAll(
|
||||
() -> assertThat(result1).isNotEqualTo(result2),
|
||||
() -> assertThat(unlockCount > 0).isTrue()
|
||||
() -> assertThat(unlockCount > 1).isTrue()
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
package com.ticketing.server.user.application;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.jupiter.api.Assertions.assertAll;
|
||||
import static org.springframework.security.test.web.servlet.setup.SecurityMockMvcConfigurers.springSecurity;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
|
||||
@@ -11,11 +9,8 @@ import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.ticketing.server.global.redis.RefreshRedisRepository;
|
||||
import com.ticketing.server.user.application.request.LoginRequest;
|
||||
import com.ticketing.server.user.application.request.RefreshRequest;
|
||||
import com.ticketing.server.user.application.request.SignUpRequest;
|
||||
import com.ticketing.server.user.application.response.TokenResponse;
|
||||
import com.ticketing.server.user.service.interfaces.UserService;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
@@ -35,12 +30,8 @@ import org.springframework.web.context.WebApplicationContext;
|
||||
class AuthControllerTest {
|
||||
|
||||
private static final String LOGIN_URL = "/api/auth/token";
|
||||
private static final String REFRESH_URL = "/api/auth/refresh";
|
||||
private static final String LOGOUT_URL = "/api/auth/logout";
|
||||
private static final String REGISTER_URL = "/api/users";
|
||||
|
||||
private static final String USER_EMAIL = "ticketing@gmail.com";
|
||||
private static final String USER_PW = "qwe123";
|
||||
|
||||
@Autowired
|
||||
WebApplicationContext context;
|
||||
@@ -63,7 +54,7 @@ class AuthControllerTest {
|
||||
@DisplayName("로그인 인증 성공")
|
||||
void loginSuccess() throws Exception {
|
||||
// given
|
||||
LoginRequest request = new LoginRequest(USER_EMAIL, USER_PW);
|
||||
LoginRequest request = new LoginRequest(USER_EMAIL, "qwe123");
|
||||
|
||||
// when
|
||||
ResultActions actions = mvc.perform(post(LOGIN_URL)
|
||||
@@ -91,69 +82,6 @@ class AuthControllerTest {
|
||||
.andExpect(status().isUnauthorized());
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("리프레쉬 토큰 발급 성공")
|
||||
void refreshTokenSuccess() throws Exception {
|
||||
// given
|
||||
LoginRequest loginRequest = new LoginRequest(USER_EMAIL, USER_PW);
|
||||
|
||||
// when
|
||||
// 로그인
|
||||
String loginResponseBody = mvc.perform(post(LOGIN_URL)
|
||||
.content(asJsonString(loginRequest))
|
||||
.contentType(MediaType.APPLICATION_JSON))
|
||||
.andReturn()
|
||||
.getResponse()
|
||||
.getContentAsString();
|
||||
|
||||
TokenResponse loginResponse = objectMapper.readValue(loginResponseBody, TokenResponse.class);
|
||||
RefreshRequest refreshRequest = new RefreshRequest(loginResponse.getRefreshToken());
|
||||
|
||||
// 토큰재발급
|
||||
String refreshResponseBody = mvc.perform(post(REFRESH_URL)
|
||||
.content(asJsonString(refreshRequest))
|
||||
.contentType(MediaType.APPLICATION_JSON))
|
||||
.andReturn()
|
||||
.getResponse()
|
||||
.getContentAsString();
|
||||
|
||||
TokenResponse refreshBody = objectMapper.readValue(refreshResponseBody, TokenResponse.class);
|
||||
|
||||
// then
|
||||
assertAll(
|
||||
() -> assertThat(refreshBody.getAccessToken()).isNotEmpty(),
|
||||
() -> assertThat(refreshBody.getRefreshToken()).isNotEmpty(),
|
||||
() -> assertThat(loginResponse.getTokenType()).isEqualTo(refreshBody.getTokenType()),
|
||||
() -> assertThat(loginResponse.getExpiresIn()).isEqualTo(refreshBody.getExpiresIn())
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("로그아웃 성공")
|
||||
void logoutSuccess() throws Exception {
|
||||
// given
|
||||
LoginRequest loginRequest = new LoginRequest(USER_EMAIL, USER_PW);
|
||||
|
||||
// 로그인
|
||||
String loginResponseBody = mvc.perform(post(LOGIN_URL)
|
||||
.content(asJsonString(loginRequest))
|
||||
.contentType(MediaType.APPLICATION_JSON))
|
||||
.andReturn()
|
||||
.getResponse()
|
||||
.getContentAsString();
|
||||
|
||||
TokenResponse loginResponse = objectMapper.readValue(loginResponseBody, TokenResponse.class);
|
||||
String authorization = loginResponse.getTokenType() + " " + loginResponse.getAccessToken();
|
||||
|
||||
// 로그아웃
|
||||
ResultActions actions = mvc.perform(post(LOGOUT_URL)
|
||||
.header("Authorization", authorization));
|
||||
|
||||
// then
|
||||
actions.andDo(print())
|
||||
.andExpect(status().isOk());
|
||||
}
|
||||
|
||||
@BeforeEach
|
||||
void init() throws Exception {
|
||||
mvc = MockMvcBuilders
|
||||
@@ -161,7 +89,7 @@ class AuthControllerTest {
|
||||
.apply(springSecurity())
|
||||
.build();
|
||||
|
||||
SignUpRequest signUpRequest = new SignUpRequest("ticketing", USER_EMAIL, USER_PW, "010-1234-5678");
|
||||
SignUpRequest signUpRequest = new SignUpRequest("ticketing", USER_EMAIL, "qwe123", "010-1234-5678");
|
||||
|
||||
mvc.perform(post(REGISTER_URL)
|
||||
.content(asJsonString(signUpRequest))
|
||||
@@ -174,7 +102,6 @@ class AuthControllerTest {
|
||||
}
|
||||
|
||||
private String asJsonString(Object object) throws JsonProcessingException {
|
||||
|
||||
return objectMapper.writeValueAsString(object);
|
||||
}
|
||||
|
||||
|
||||
@@ -69,6 +69,7 @@ class UserControllerTest {
|
||||
|
||||
SignUpRequest signUpRequest;
|
||||
|
||||
|
||||
@Test
|
||||
@DisplayName("회원가입 성공")
|
||||
void registerSuccess() throws Exception {
|
||||
|
||||
@@ -1,11 +1,10 @@
|
||||
package com.ticketing.server.user.application;
|
||||
|
||||
import com.ticketing.server.user.application.request.LoginRequest;
|
||||
import com.ticketing.server.user.application.request.RefreshRequest;
|
||||
import com.ticketing.server.user.application.response.LogoutResponse;
|
||||
import com.ticketing.server.user.service.dto.TokenDTO;
|
||||
import com.ticketing.server.user.application.response.TokenResponse;
|
||||
import com.ticketing.server.user.service.dto.DeleteRefreshTokenDTO;
|
||||
import com.ticketing.server.user.service.dto.TokenDTO;
|
||||
import com.ticketing.server.user.service.interfaces.AuthenticationService;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
@@ -17,6 +16,7 @@ import org.springframework.security.core.userdetails.UserDetails;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController
|
||||
@@ -37,8 +37,8 @@ public class AuthController {
|
||||
}
|
||||
|
||||
@PostMapping("/refresh")
|
||||
public ResponseEntity<TokenResponse> refreshToken(@RequestBody RefreshRequest request) {
|
||||
TokenDTO tokenDto = authenticationService.reissueTokenDto(request.getRefreshToken());
|
||||
public ResponseEntity<TokenResponse> refreshToken(@RequestParam("refreshToken") String refreshToken) {
|
||||
TokenDTO tokenDto = authenticationService.reissueTokenDto(refreshToken);
|
||||
|
||||
return ResponseEntity.status(HttpStatus.OK)
|
||||
.headers(getHttpHeaders())
|
||||
|
||||
@@ -1,14 +0,0 @@
|
||||
package com.ticketing.server.user.application.request;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
@Getter
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class RefreshRequest {
|
||||
|
||||
private String refreshToken;
|
||||
|
||||
}
|
||||
@@ -2,16 +2,14 @@ package com.ticketing.server.user.application.response;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
@Getter
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class TokenResponse {
|
||||
|
||||
private String accessToken;
|
||||
private String refreshToken;
|
||||
private String tokenType;
|
||||
private long expiresIn;
|
||||
private final String accessToken;
|
||||
private final String refreshToken;
|
||||
private final String tokenType;
|
||||
private final long expiresIn;
|
||||
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ package com.ticketing.server.user.service;
|
||||
import com.ticketing.server.global.exception.ErrorCode;
|
||||
import com.ticketing.server.global.redis.RefreshRedisRepository;
|
||||
import com.ticketing.server.global.redis.RefreshToken;
|
||||
import com.ticketing.server.global.security.jwt.JwtProperties;
|
||||
import com.ticketing.server.global.security.jwt.JwtProvider;
|
||||
import com.ticketing.server.user.service.dto.DeleteRefreshTokenDTO;
|
||||
import com.ticketing.server.user.service.dto.TokenDTO;
|
||||
@@ -13,6 +14,7 @@ import org.springframework.security.config.annotation.authentication.builders.Au
|
||||
import org.springframework.security.core.Authentication;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
@@ -21,6 +23,7 @@ public class AuthenticationServiceImpl implements AuthenticationService {
|
||||
private final RefreshRedisRepository refreshRedisRepository;
|
||||
|
||||
private final JwtProvider jwtProvider;
|
||||
private final JwtProperties jwtProperties;
|
||||
private final AuthenticationManagerBuilder authenticationManagerBuilder;
|
||||
|
||||
@Override
|
||||
@@ -52,7 +55,9 @@ public class AuthenticationServiceImpl implements AuthenticationService {
|
||||
|
||||
@Override
|
||||
@Transactional
|
||||
public TokenDTO reissueTokenDto(String refreshToken) {
|
||||
public TokenDTO reissueTokenDto(String bearerRefreshToken) {
|
||||
String refreshToken = resolveToken(bearerRefreshToken);
|
||||
|
||||
// 토큰 검증
|
||||
jwtProvider.validateToken(refreshToken);
|
||||
|
||||
@@ -62,7 +67,7 @@ public class AuthenticationServiceImpl implements AuthenticationService {
|
||||
RefreshToken findTokenEntity = refreshRedisRepository.findByEmail(authentication.getName())
|
||||
.orElseThrow(ErrorCode::throwRefreshTokenNotFound);
|
||||
|
||||
// input 토큰이 최신 토큰이 아닐 경우 예외 처리
|
||||
// redis 토큰과 input 토큰이 일치한지 확인
|
||||
if (!refreshToken.equals(findTokenEntity.getToken())) {
|
||||
throw ErrorCode.throwUnavailableRefreshToken();
|
||||
}
|
||||
@@ -89,4 +94,11 @@ public class AuthenticationServiceImpl implements AuthenticationService {
|
||||
);
|
||||
}
|
||||
|
||||
private String resolveToken(String bearerToken) {
|
||||
if (StringUtils.hasText(bearerToken) && jwtProperties.hasTokenStartsWith(bearerToken)) {
|
||||
return bearerToken.substring(7);
|
||||
}
|
||||
throw ErrorCode.throwTokenType();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -28,3 +28,7 @@ spring:
|
||||
redis:
|
||||
host: 172.18.0.3
|
||||
port: 6379
|
||||
|
||||
application:
|
||||
name: monitoring
|
||||
|
||||
|
||||
@@ -26,6 +26,24 @@ spring:
|
||||
config:
|
||||
import: "optional:configserver:"
|
||||
|
||||
application:
|
||||
name: monitoring
|
||||
|
||||
management:
|
||||
endpoint:
|
||||
metrics:
|
||||
enabled: true
|
||||
prometheus:
|
||||
enabled: true
|
||||
|
||||
endpoints:
|
||||
web:
|
||||
exposure:
|
||||
include: health, info, metrics, prometheus
|
||||
|
||||
metrics:
|
||||
tags:
|
||||
application: ${spring.application.name}
|
||||
|
||||
jasypt:
|
||||
encryptor:
|
||||
|
||||
@@ -10,8 +10,8 @@ import com.ticketing.server.global.redis.RefreshRedisRepository;
|
||||
import com.ticketing.server.global.redis.RefreshToken;
|
||||
import com.ticketing.server.global.security.jwt.JwtProperties;
|
||||
import com.ticketing.server.global.security.jwt.JwtProvider;
|
||||
import com.ticketing.server.user.domain.UserGrade;
|
||||
import com.ticketing.server.user.service.dto.TokenDTO;
|
||||
import com.ticketing.server.user.domain.UserGrade;
|
||||
import java.util.Collections;
|
||||
import java.util.Optional;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
@@ -62,7 +62,7 @@ class AuthenticationServiceImplTest {
|
||||
@DisplayName("토큰 재발급 성공")
|
||||
void reissueAccessToken() {
|
||||
// given
|
||||
String refreshToken = "eyJhbGciOiJIUzUxMiJ9";
|
||||
String refreshToken = "Bearer eyJhbGciOiJIUzUxMiJ9";
|
||||
when(jwtProvider.validateToken(any())).thenReturn(true);
|
||||
when(jwtProvider.getAuthentication(any())).thenReturn(authenticationToken);
|
||||
when(jwtProvider.generateTokenDto(any())).thenReturn(useJwtProvider.generateTokenDto(authenticationToken));
|
||||
|
||||
Reference in New Issue
Block a user