728x90
새로운 프로젝트를 시작하면서 Spring Boot 3.0.1, Java 17을 사용하게 되었다.
기존 Spring Boot. 2.7.x 버전에서 사용하던 Spring Security 설정을 그대로 복사 붙여 넣기를 했더니 @deprecated된 명령어도 있고, 아예 사라진(변경된) 명령어가 존재하여 새로운 버전에 맞춰 코드를 수정하였다.
기존 Security 설정
@Configuration
@EnableWebSecurity
@RequiredArgsConstructor
public class SecurityConfig {
private final UserService userService;
@Value("${jwt.secret}")
private String secretKey;
@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity httpSecurity) throws Exception {
return httpSecurity.
httpBasic().disable()
.csrf().disable()
.cors().disable()
.authorizeRequests()
.antMatchers("...").permitAll()
.antMatchers(HttpMethod.POST, "/api/v1/**","/").authenticated()
.and()
.exceptionHandling()
.authenticationEntryPoint(new CustomAuthenticationEntryPoint())
.and()
.sessionManagement()
.sessionCreationPolicy(SessionCreationPolicy.STATELESS)
.and()
.addFilterBefore(new JwtFilter(userService, secretKey), UsernamePasswordAuthenticationFilter.class)
.build();
}
}
Spring Boot 3.0.1 기준 Spring Security 설정
@Configuration
@EnableWebSecurity
@RequiredArgsConstructor
public class SecurityConfig {
private final JwtAuthenticationFilter jwtFilter;
private final AuthenticationProvider authenticationProvider;
private final String[] AUTHORIZATION = {"", ""};
private final String[] TEST_URL = {"/api/v1/hello/**"};
@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
http
.csrf()
.disable()
.authorizeHttpRequests()
.requestMatchers(TEST_URL)
.permitAll()
.requestMatchers(AUTHORIZATION)
.permitAll()
.anyRequest()
.authenticated()
.and()
.sessionManagement()
.sessionCreationPolicy(SessionCreationPolicy.STATELESS)
.and()
.authenticationProvider(authenticationProvider)
.addFilterBefore(jwtFilter, UsernamePasswordAuthenticationFilter.class);
return http.build();
}
}
antMatchers -> requestMatchers로 변경되었다.
번외) requestMatchers에 들어가는 url의 경우 final String 배열을 미리 선언한 후 대입하는 방식으로 깔끔하게 정리했다.
반응형
'Server > Spring&Spring Boot' 카테고리의 다른 글
[Spring] test code에 대한 회고 (0) | 2023.01.24 |
---|---|
[Spring Boot] SpringBoot 3.0.x 이상에서 Swagger 사용 (0) | 2023.01.19 |
[JPA] Update 후 해당 객체를 Return했을 때 @CreatedDate가 null 이 반환되는 상태에 대한 회고 (0) | 2023.01.06 |
[Spring] 전역 예외, Global Exception 생성 (0) | 2022.12.21 |
[Spring] Custom Response 생성 (0) | 2022.12.21 |