Spring Security

2022. 12. 26. 23:51스파르타 내일배움캠프/Spring 강의 정리

오늘부터 Spring security를 시작했습니다. 아직 이해가 가진 않지만 코드 deprecate된 부분을 수정해나간 부분 위주로 정리하겠습니다.

 

1. authorizeRequests() deprecated

  @Bean
    public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
        // CSRF 설정
        http.csrf().disable();
        
        http.authorizeRequests().antMatchers("/api/user/**").permitAll()
																.anyRequest().authenticated();

        // Custom 로그인 페이지 사용
        http.formLogin().loginPage("/api/user/login-page").permitAll();

				// Custom Filter 등록하기
				http.addFilterBefore(new CustomSecurityFilter(userDetailsService, passwordEncoder()), UsernamePasswordAuthenticationFilter.class);
        
        return http.build();
    }

authorizeRequests() 메소드는 더이상 사용되지 않습니다. 따라서 intelliJ가 알려주는 authorizeHttpRequests() 메소드로 변경하였습니다.

또한 authorizeHttpRequests()는 antMatchers() 메소드를 사용할 수 없기 때문에 requestMatchers() 함수로 대체해 줍니다.

 

@Bean
    public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
        // CSRF 설정
        http.csrf().disable();

        http.authorizeHttpRequests().requestMatchers("/api/user/**").permitAll()
                .anyRequest().authenticated();

        // Custom 로그인 페이지 사용
        http.formLogin().loginPage("/api/user/login-page").permitAll();

       
        return http.build();
    }

 

https://stackoverflow.com/questions/74609057/how-to-fix-spring-authorizerequests-is-deprecated

 

How to fix Spring authorizeRequests is deprecated?

Spring is updated, says authorizeRequests is deprecated, antMatchers removed. Can someone show how SpringSecurity should looks like rn? @Configuration @EnableWebSecurity @EnableMethodSecurity(

stackoverflow.com

 

 

2. @EnableGlobalMethodSecurity(securedEnabled = true) deprecated

 

@EnableGlobalMethodSecurity(securedEnabled = true) 어노테이션도 현재 deprecated 상태였습니다.

따라서 spring 공식사이트에서 알려주는 @EnableMethodSecurity 어노테이션으로 변경해줍니다.

 

기존

@Configuration
@RequiredArgsConstructor
@EnableWebSecurity // 스프링 Security 지원을 가능하게 함
@EnableGlobalMethodSecurity(securedEnabled = true) // @Secured 어노테이션 활성화 - deprecated
public class WebSecurityConfig {	
       ...
}

 

변경

@Configuration
@RequiredArgsConstructor
@EnableWebSecurity // 스프링 Security 지원을 가능하게 함
@EnableMethodSecurity(securedEnabled = true) // @Secured 어노테이션 활성화
public class WebSecurityConfig {	
       ...
}

 

https://docs.spring.io/spring-security/site/docs/current/api/org/springframework/security/config/annotation/method/configuration/EnableMethodSecurity.html

 

EnableMethodSecurity (spring-security-docs 6.0.1 API)

Indicate whether subclass-based (CGLIB) proxies are to be created as opposed to standard Java interface-based proxies. The default is false. Applicable only if mode() is set to AdviceMode.PROXY. Note that setting this attribute to true will affect all Spri

docs.spring.io

 

사실 영어에 익숙하지 않아 영문 사이트는 들어가보지 않다 한번 참고하다 해결했습니다.

앞으로도 종종 용기를 내서 활용해 보겠습니다.

 

 

'스파르타 내일배움캠프 > Spring 강의 정리' 카테고리의 다른 글

No serializer found for class 오류  (0) 2023.01.05
Spring 심화 pjt  (0) 2023.01.03
Spring 숙련 과제(최종)  (0) 2022.12.21
Spring 숙련 과제(2)  (0) 2022.12.20
Spring 숙련 과제  (0) 2022.12.19