From ce1b010e9f1e821d23cb8ea69057326a3bca3965 Mon Sep 17 00:00:00 2001 From: hseong3243 Date: Tue, 23 Jan 2024 16:45:40 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20=EC=9D=B8=EC=A6=9D=20=ED=94=84=EB=A1=9C?= =?UTF-8?q?=EC=84=B8=EC=8A=A4=20=EC=9D=B8=ED=84=B0=EC=85=89=ED=84=B0?= =?UTF-8?q?=EB=A5=BC=20WebMVC=20=EA=B5=AC=EC=84=B1=EC=97=90=20=EB=93=B1?= =?UTF-8?q?=EB=A1=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../shoutlink/global/config/WebConfig.java | 25 +++++++++++++++++ .../shoutlink/base/BaseControllerTest.java | 28 ++++++++++++++++++- 2 files changed, 52 insertions(+), 1 deletion(-) create mode 100644 src/main/java/com/seong/shoutlink/global/config/WebConfig.java diff --git a/src/main/java/com/seong/shoutlink/global/config/WebConfig.java b/src/main/java/com/seong/shoutlink/global/config/WebConfig.java new file mode 100644 index 0000000..ef0d5de --- /dev/null +++ b/src/main/java/com/seong/shoutlink/global/config/WebConfig.java @@ -0,0 +1,25 @@ +package com.seong.shoutlink.global.config; + +import com.seong.shoutlink.global.auth.authentication.AuthenticationContext; +import com.seong.shoutlink.global.auth.authentication.JwtAuthenticationInterceptor; +import com.seong.shoutlink.global.auth.authentication.JwtAuthenticationProvider; +import lombok.RequiredArgsConstructor; +import org.springframework.context.annotation.Configuration; +import org.springframework.web.servlet.config.annotation.InterceptorRegistry; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; + +@Configuration +@RequiredArgsConstructor +public class WebConfig implements WebMvcConfigurer { + + private final JwtAuthenticationProvider jwtAuthenticationProvider; + private final AuthenticationContext authenticationContext; + + @Override + public void addInterceptors(InterceptorRegistry registry) { + registry.addInterceptor( + new JwtAuthenticationInterceptor(jwtAuthenticationProvider, authenticationContext)) + .order(1) + .addPathPatterns("/api/**"); + } +} diff --git a/src/test/java/com/seong/shoutlink/base/BaseControllerTest.java b/src/test/java/com/seong/shoutlink/base/BaseControllerTest.java index 23a9409..85724b0 100644 --- a/src/test/java/com/seong/shoutlink/base/BaseControllerTest.java +++ b/src/test/java/com/seong/shoutlink/base/BaseControllerTest.java @@ -3,12 +3,19 @@ import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; import com.fasterxml.jackson.databind.ObjectMapper; +import com.seong.shoutlink.base.BaseControllerTest.BaseControllerConfig; +import com.seong.shoutlink.domain.auth.JwtProvider; import com.seong.shoutlink.domain.auth.service.AuthService; +import com.seong.shoutlink.fixture.AuthFixture; +import com.seong.shoutlink.global.auth.authentication.AuthenticationContext; +import com.seong.shoutlink.global.auth.authentication.JwtAuthenticationProvider; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.extension.ExtendWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; +import org.springframework.boot.test.context.TestConfiguration; import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Import; import org.springframework.restdocs.RestDocumentationContextProvider; import org.springframework.restdocs.RestDocumentationExtension; @@ -20,10 +27,29 @@ import org.springframework.web.filter.CharacterEncodingFilter; @WebMvcTest -@Import({RestDocsConfig.class}) +@Import({RestDocsConfig.class, BaseControllerConfig.class}) @ExtendWith(RestDocumentationExtension.class) public class BaseControllerTest { + @TestConfiguration + static class BaseControllerConfig { + + @Bean + public JwtProvider jwtProvider() { + return AuthFixture.jwtProvider(); + } + + @Bean + public JwtAuthenticationProvider jwtAuthenticationProvider(JwtProvider jwtProvider) { + return new JwtAuthenticationProvider(jwtProvider); + } + + @Bean + public AuthenticationContext authenticationContext() { + return new AuthenticationContext(); + } + } + protected MockMvc mockMvc; @Autowired