책을 보면서 공부하다가 똑같은 코드를 작성했는데 작동이 안 하는 부분이 있었다. 그래서 왜 그런가 살펴보니

나중에 없어지니까 사용하지 말라고 한다... 그럼 어떻게 하지;;하고 인터넷을 찾아 봤다.
https://spring.io/blog/2019/11/21/spring-security-lambda-dsl
Spring Security - Lambda DSL
Overview of Lambda DSL The release of Spring Security 5.2 includes enhancements to the DSL, which allow HTTP security to be configured using lambdas. It is important to note that the prior configuration style is still valid and supported. The addition of l
spring.io
예를 들어서 이렇게 람다식으로 사용하면 된다는 듯하다.
아래가 문제가 발생하는 코드이다.
@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
return http.authorizeRequests()
.requestMatchers("/login", "/signup", "/user")
.permitAll()
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage("/login")
.defaultSuccessUrl("/articles")
.and()
.logout()
.logoutSuccessUrl("/logout")
.invalidateHttpSession(true)
.and()
.csrf(AbstractHttpConfigurer::disable)
.build();
}
위 코드를 아래와 같이 바꿔주면 해결할 수 있다.
람다식을 이용하는 것 외에도 authorizeRequests() 메서드를 authorizeHttpRequests() 메서드로 바꿔주어야 한다.
@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
return http.authorizeHttpRequests(auth -> auth
.requestMatchers("/login", "/signup", "/user")
.permitAll()
.anyRequest().authenticated()
)
.formLogin(login -> login
.loginPage("/login")
.defaultSuccessUrl("/articles")
)
.logout(logout -> logout
.logoutSuccessUrl("/logout")
.invalidateHttpSession(true)
)
.csrf(AbstractHttpConfigurer::disable)
.build();
}
그런데 이렇게 바꾸고 나서 실행해보면 안 된다...
This method cannot decide whether these patterns are Spring MVC patterns or not.
If this endpoint is a Spring MVC endpoint, please use requestMatchers(MvcRequestMatcher);
otherwise, please use requestMatchers(AntPathRequestMatcher)
이런 컴파일 에러가 뜨는데 해석해보면 스프링 MVC 패턴인지 인식을 잘 못하는 것 같다. 만약 스프링 MVC endpoint면 저 두 메서드 중 하나를 사용하라고 한다.
import static org.springframework.security.web.util.matcher.AntPathRequestMatcher.*;
// import 해야함
@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
return http.authorizeHttpRequests(auth -> auth
.requestMatchers(antMatcher("/login"), antMatcher("/signup"), antMatcher("/user"))
.permitAll()
.anyRequest().authenticated()
)
.formLogin(login -> login
.loginPage("/login")
.defaultSuccessUrl("/articles")
)
.logout(logout -> logout
.logoutSuccessUrl("/logout")
.invalidateHttpSession(true)
)
.csrf(AbstractHttpConfigurer::disable)
.build();
}
AntPathRequestMatcher.antMatcher()를 이용하여 코드를 바꿔주자.
'스터디 모임' 카테고리의 다른 글
| [스터디 모임] CSRF (0) | 2024.03.04 |
|---|---|
| [스터디 모임] 스프링 핵심 원리 - 도입편 (0) | 2024.02.18 |
| 오늘 겪었던 문제 Error: 4091-42S02: Unknown SEQUENCE: (0) | 2024.02.13 |
| 오늘 구현한 것들 - 회원 가입 / 검증 / @Valid 작동 안 하던 문제 해결 (0) | 2023.11.02 |
| [스터디] 데이터베이스 설계하기 (0) | 2023.10.09 |