Spring Security 6.1.0에서 is deprecated and marked for removal 오류

wooSim·2023년 6월 18일
6

Spring Security 6.1.0에서 SecurityConfig 작성시 오류


□ ...deprecated and marked for removal 오류

Spring Security 6에서 websecurityconfigureradapter가 deprecated가 되고 SecurityFilterChain bean을 생성하도록 변경되어 Spring Security 6.0.2에서 새로 작성해보며 공부하였었습니다.


6.0.2 버전에서 작성한 코드는 아래와 같습니다.

@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
	http
			.csrf().disable().headers().frameOptions().disable().and()
			.authorizeHttpRequests()
			.requestMatchers(PathRequest.toH2Console()).permitAll()
			.requestMatchers("/", "/login/**").permitAll()
			.requestMatchers("/posts/**", "/api/v1/posts/**").hasRole(Role.USER.name())
			.requestMatchers("/admins/**", "/api/v1/admins/**").hasRole(Role.ADMIN.name())
			.anyRequest().authenticated()
			.and()
			.formLogin()
			.loginPage("/login/login")
			.usernameParameter("username")
			.passwordParameter("password")
			.loginProcessingUrl("/login/login-proc")
			.defaultSuccessUrl("/", true)
			.and()
			.logout()
			.logoutSuccessUrl("/")
			.and()
			.userDetailsService(myUserDetailsService);


	return http.build();
}

하지만 다시 공부한 것을 정리하기 위해 새로 프로젝트를 생성하니 ...is deprecated and marked for removal 라는 오류는 만나게 되었습니다.

□ 코드 변경

찾아보니 제가 만든 새롭게 만든 프로젝트는 Spring boot 3.1.0, Spring Security 6.1.0이었습니다. Spring Security 6.1.0부터는 메서드 체이닝의 사용을 지양하고 람다식을 통해 함수형으로 설정하게 지향하고 있습니다.

@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {

	http
			.csrf((csrfConfig) ->
					csrfConfig.disable()
			) 
			.headers((headerConfig) ->
					headerConfig.frameOptions(frameOptionsConfig ->
							frameOptionsConfig.disable()
					)
			)
			.authorizeHttpRequests((authorizeRequests) ->
					authorizeRequests
							.requestMatchers(PathRequest.toH2Console()).permitAll()
							.requestMatchers("/", "/login/**").permitAll()
							.requestMatchers("/posts/**", "/api/v1/posts/**").hasRole(Role.USER.name())
							.requestMatchers("/admins/**", "/api/v1/admins/**").hasRole(Role.ADMIN.name())
							.anyRequest().authenticated()
			)
			.exceptionHandling((exceptionConfig) ->
					exceptionConfig.authenticationEntryPoint(unauthorizedEntryPoint).accessDeniedHandler(accessDeniedHandler)
			) 
			.formLogin((formLogin) ->
					formLogin
							.loginPage("/login/login") 
							.usernameParameter("username") 
							.passwordParameter("password") 
							.loginProcessingUrl("/login/login-proc") 
							.defaultSuccessUrl("/", true)
			)
			.logout((logoutConfig) ->
					logoutConfig.logoutSuccessUrl("/") 
			)
			.userDetailsService(myUserDetailsService); 

	return http.build();
}




profile
daily study

2개의 댓글

comment-user-thumbnail
2023년 8월 1일

덕분에 해결했습니다! 고맙습니다

답글 달기
comment-user-thumbnail
2023년 9월 6일

spring security 설정 때문에 구글링 엄청했는데 여러 글 다 봐도 잘 몰랐었는데 이 글 읽고 파악하고 해결했어요. 감사합니다. :)

답글 달기