댓글 저장과 삭제 까지능 구현 했지만, 다른 유저가 내 댓글을 삭제해서는 안된다. 따라서 인증 기능을 추가해서 해당 댓글을 작성한 user만 삭제할 수 있도록 한다.
로그인 화면을 Spring Security를 통해 구현할 때 무작정 로그인 페이지가 나타나는 것을 막기 위해 securityConfig를 통해 어떤 request 건 통과를 시켜서 로그인화면을 스킵하도록 한다.
@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
http
.authorizeHttpRequests(auth -> auth
.mvcMatchers(
HttpMethod.GET,
"/",
"articles",
"/articles/search-hashtag"
).permitAll()
.anyRequest().authenticated()
)
.formLogin();
return http.build();
}
해당 페이지들은 인증과 권한 체크를 진행하게 해준다. 인증을 진행할 필요가 없는 페이지들은 따로 설정해 줘야하는데, 이 것을 WEbSecurityCustomizer로 설정할 수 있다.
굳이 인증기능을 추가할 필요가 없는 정적 리소스들을 대상을 선택해서 설정하는 방식이다.
로그인을 시도할 때 스프링 시큐리티에서 제공하는 로그인 뷰가 잘 리턴되는지 확인한다.
@DisplayName("View 컨트롤러 - 인증")
@WebMvcTest
public class AuthControllerTest {
private final MockMvc mvc;
public AuthControllerTest(@Autowired MockMvc mvc) {
this.mvc = mvc;
}
@DisplayName("[view][GET] 로그인 페이지 - 정상 호출")
@Test
public void givenNothing_whenTryingToSlogIn_thenReturnsLogInView() throws Exception {
// given
//when & then
mvc.perform(get("/login"))
.andExpect(status().isOk())
.andExpect(content().contentTypeCompatibleWith(MediaType.TEXT_HTML)); // view -> text_html
}