[์ฝ์ง๋ก] Spring Security ์ธ์ฆ ๊ตฌํ ์ด์ ์ ํ
์คํธ ์ฝ๋ ์คํ ์ 401 ์๋ฌ ํด๊ฒฐBack-end/Error2024. 8. 17. 21:13
Table of Contents
๐ ๋ฌธ์ ์ํฉ
@DisplayName("์ปจํธ๋กค๋ฌ - Post")
@WebMvcTest(PostController.class) //MVC ๊ด๋ จ ๋น๋ค๋ง ๋ก๋ํจ - ํ
์คํธ ์คํ์ด ๋น ๋ฅด๊ณ , ๋ฉ๋ชจ๋ฆฌ ์ฌ์ฉ๋ ์ ์ | ์๋น์ค, ๋ฆฌํฌ์งํ ๋ฆฌ ๋ฑ์ ๋น์ ๋ก๋๋์ง ์์
class PostControllerTest {
private final MockMvc mvc; //HTTP ์์ฒญ์ ๋ชจ์(mock)ํ์ฌ ์ปจํธ๋กค๋ฌ์ ๋์์ ํ
์คํธ | ์ค์ ์น ์๋ฒ๋ฅผ ๋์ฐ์ง ์๊ณ ๋ ์ปจํธ๋กค๋ฌ์ ์์ฒญ-์๋ต ํ๋ฆ์ ํ
์คํธํ ์ ์์
// ํ๋์ฃผ์
@MockBean // ->MockBean์ด ์์ฑ์ ์ฃผ์
์ง์ํ์ง ์์
private PostService postService;
// ์์ฑ์์ฃผ์
PostControllerTest(
@Autowired MockMvc mvc
) {
this.mvc = mvc;
}
@DisplayName("[view] [GET] ํฌ์คํธ ๋ฆฌ์คํธ (ํผ๋) ํ์ด์ง - ์ ์ ํธ์ถ")
@Test
void givenNothing_whenRequestingPostsView_thenReturnsPostsView() throws Exception {
// Given
given(postService.getPosts(any(Pageable.class)))
.willReturn(Page.empty());
// When & Then
mvc.perform(get("/posts"))
.andExpect(status().isOk())
.andExpect(content().contentTypeCompatibleWith(MediaType.TEXT_HTML))
.andExpect(view().name("posts/index"))
.andExpect(model().attributeExists("posts"));
then(postService).should().getPosts(any(Pageable.class));
}
Status expected:<200> but was:<401>
Expected :200
Actual :401
<Click to see difference>
java.lang.AssertionError: Status expected:<200> but was:<401>
at org.springframework.test.util.AssertionErrors.fail(AssertionErrors.java:59)
at org.springframework.test.util.AssertionErrors.assertEquals(AssertionErrors.java:122)
at org.springframework.test.web.servlet.result.StatusResultMatchers.lambda$matcher$9(StatusResultMatchers.java:637)
at org.springframework.test.web.servlet.MockMvc$1.andExpect(MockMvc.java:214)
at com.project.sns.controller.PostControllerTest.givenNothing_whenRequestingPostsView_thenReturnsPostsView(PostControllerTest.java:50)
at java.base/java.lang.reflect.Method.invoke(Method.java:568)
→ ๊ฐ๋จํ ํ์ด์ง ๋ทฐ GET๋ฉ์๋์ ์คํ ๊ฒฐ๊ณผ๋ฅผ ํ ์คํธ ํ๋ ์ฝ๋์ธ๋ฐ 401 Unauthorized ์๋ฌ๊ฐ ๋ฐ์ํ๋ค.
์์ธ
@WebMvcTest๋ ๊ธฐ๋ณธ์ ์ผ๋ก ์ปจํธ๋กค๋ฌ์ ๊ด๋ จ๋ ์น ๊ณ์ธต์ ํ ์คํธํ ๋ ์ฌ์ฉ๋๋ฉฐ,
์ค์ ์ธ์ฆ ๋ฉ์ปค๋์ฆ (์: Spring Security)๋ ํจ๊ป ์ ์ฉ๋๋ค.
⇒ ์ธ์ฆ์ด ํ์ํ ์ปจํธ๋กค๋ฌ ์๋ํฌ์ธํธ๋ฅผ ํ ์คํธํ ๋ ์ธ์ฆ์ด ์ด๋ฃจ์ด์ง์ง ์์ผ๋ฉด 401 Unauthorized ์ค๋ฅ๊ฐ ๋ฐ์ํ๋ค.
โ ํด๊ฒฐ ๋ฐฉ๋ฒ
@WebMvcTest์ ํจ๊ป Spring Security๋ฅผ ์ฌ์ฉํ๋ค๋ฉด, ์ธ์ฆ์ด ํ์ํ ํ ์คํธ์์ MockMvc์ ์ธ์ฆ ์ ๋ณด๋ฅผ ์ ๊ณตํ๋ฉด ๋๋ค.
⇒ ํ ์คํธ ์์ Spring Security์ ๊ด๋ จ๋ ์ฌ์ฉ์ ์ธ์ฆ ์ ์ฐจ๋ฅผ ๋ชจํนํ์ฌ ์ค์ ์ธ์ฆ ์์ด( ์ธ์ฆ๋ ์ฌ์ฉ์๊ฐ ์๋ ๊ฒ์ฒ๋ผ ) ์ปจํธ๋กค๋ฌ ํ ์คํธ๋ฅผ ์คํํ์!
@DisplayName("์ปจํธ๋กค๋ฌ - Post")
**@Import(TestSecurityConfig.class) //โ
์ด ๋ถ๋ถ์ ํตํด Importํ์**
@WebMvcTest(PostController.class) //MVC ๊ด๋ จ ๋น๋ค๋ง ๋ก๋ํจ - ํ
์คํธ ์คํ์ด ๋น ๋ฅด๊ณ , ๋ฉ๋ชจ๋ฆฌ ์ฌ์ฉ๋ ์ ์ | ์๋น์ค, ๋ฆฌํฌ์งํ ๋ฆฌ ๋ฑ์ ๋น์ ๋ก๋๋์ง ์์
class PostControllerTest {
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.BDDMockito.given;
@Import(SecurityConfig.class)
public class TestSecurityConfig {
@MockBean private UserAccountService userAccountService;
@BeforeTestMethod //๊ฐ ํ
์คํธ ๋ฉ์๋๊ฐ ์คํ๋๊ธฐ ์ ์ ์ํ
public void securitySetUp() {
// ๊ฐ ํ
์คํธ ์คํ ์ง์ ์ ์ธ์ฆ์ ๋ณด ๋ฃ์ด์ฃผ๋ผ๊ณ ์์ฑํจ
given(userAccountService.searchUser(anyString()))
.willReturn(Optional.of(createUserAccountDto()));
given(userAccountService.saveUser(anyString(), anyString(), anyString(), anyString(), anyString()))
.willReturn(createUserAccountDto());
}
private UserAccountDto createUserAccountDto() {
return UserAccountDto.of(
"ellaTest",
"pw",
"ella-test@mail.com",
"ella-test",
"test memmo"
);
}
}
@Import (SecurityConfig.class)
Spring์ ํน์ ์ค์ ํด๋์ค๋ฅผ ํ์ฌ ํ ์คํธ ์ปจํ ์คํธ์ ์ถ๊ฐํ ๋ ์ฌ์ฉ
SecurityConfig.class๋ฅผ ํ ์คํธ ํ๊ฒฝ์ ๊ฐ์ ธ์์ Spring Security ์ค์ ์ด ํ ์คํธ์ ๋ฐ์๋๋๋ก ํจ
@MockBean
UserAccountService ๋น์ ๋ชฉ(Mock)์ผ๋ก ๋ง๋ฌ
MockBean์ผ๋ก ์์ฑ๋ ๊ฐ์ฒด๋ ํด๋น ๋น์ด ํ์ํ ๋ชจ๋ ๊ณณ์ ์ฃผ์ ๋จ
- ์ค์ ์๋น์ค ๋ ์ด์ด์ ๋ก์ง์ ํ ์คํธ ํ ํ์ ์์
- ์ํ๋๋๋ก mock๋ ์๋ต์ ์ ์ํ ์ ์์
securitySetUp ๋ฉ์๋
userAccountService์ ํน์ ๋ฉ์๋ ํธ์ถ์ ๋ํด ๋ฏธ๋ฆฌ ์ ์๋ ๊ฐ์ ๋ฐํํ๋๋ก ์ค์
Ex)
userAccountService.searchUser(anyString()) ๋ฉ์๋๊ฐ ํธ์ถ๋ ๋, ๋ฏธ๋ฆฌ ์ ์ํ UserAccountDto ๊ฐ์ฒด๋ฅผ ๋ฐํํ๋๋ก ์ค์
createUserAccountDto() ๋ฉ์๋
ํ ์คํธ ์ค์ ์ฌ์ฉ๋ ๋ชจ์(Mock) ์ฌ์ฉ์ ๋ฐ์ดํฐ๋ฅผ ๋ฐํํ์ฌ, ํ ์คํธ ํ๊ฒฝ์์ ์ฌ์ฉ์์ ์ธ์ฆ ์ ๋ณด๋ฅผ ๋ฏธ๋ฆฌ ๊ตฌ์ฑ