본문 바로가기
Programming/JAVA

[JAVA] Spring Boot(스프링 부트) - Security(시큐리티) 설정

by JeeU147 2022. 12. 24.
반응형

안녕하세요.

 

오늘은 Spring Boot(스프링 부트) - Security(시큐리티) 설정에 대해 알아보려고 합니다.

 

스프링 부트 시큐리티 5.6.x 무료 업데이트 기간이 별로 남지 않은 시점에서 5.7.x로 넘어가보려고 합니다.

 

근데 버전을 변경하고 실행하게 된다면 에러가 떨어지게 됩니다.

 

Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'configure' defined in class path resource [파일 위치]: Invalid factory method 'configure' on class [파일 위치]: needs to have a non-void return type!

 

이번 5.7.x로 올라오게 되면서 추상화 객체를 안쓰고 @Bean으로 대체되고 void를 사용하여 리턴값을 주지 않았지만 해당 버전부터는 return 값이 필요하게 변경이 되었습니다.

 

스프링 부트 공식 블로그

https://spring.io/blog/2022/02/21/spring-security-without-the-websecurityconfigureradapter

 

Spring Security without the WebSecurityConfigurerAdapter

<p>In Spring Security 5.7.0-M2 we <a href="https://github.com/spring-projects/spring-security/issues/10822">deprecated</a> the <code>WebSecurityConfigurerAdapter</code>, as we encourage users to move towards a component-based security configuration.</p> <p

spring.io

 

해당 스프링 부트 공식 블로그에서 5.7.x 버전부터는 이런 식으로 변경을 해야한다고 나와있습니다.

 

변경 전

@Configuration
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            .authorizeHttpRequests((authz) -> authz
                .anyRequest().authenticated()
            )
            .httpBasic(withDefaults());
    }

}

변경 후 (추상화 객체가 빠지고 @Bean을 추가하고 함수 명이 바뀌고 리턴값이 생긴걸 볼 수 있습니다.)

@Configuration
public class SecurityConfiguration {

    @Bean
    public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
        http
            .authorizeHttpRequests((authz) -> authz
                .anyRequest().authenticated()
            )
            .httpBasic(withDefaults());
        return http.build();
    }

}

 

변경 전

@Configuration
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {

    @Override
    public void configure(WebSecurity web) {
        web.ignoring().antMatchers("/ignore1", "/ignore2");
    }

}

변경 후 (추상화 객체가 빠지고 @Bean을 추가하고 함수 명이 바뀌고 리턴값이 생긴걸 볼 수 있습니다.)

@Configuration
public class SecurityConfiguration {

    @Bean
    public WebSecurityCustomizer webSecurityCustomizer() {
        return (web) -> web.ignoring().antMatchers("/ignore1", "/ignore2");
    }

}

 

버전업을 하셨다면 공식 사이트를 꼭 확인을 해보셔야될 것 같습니다.

 

감사합니다.

반응형

댓글