Dev_Henry

[Spring] 자바17+스프링3.x로 업그레이드하며 생긴 이슈. (Spring Security, Swagger) 본문

Web/Spring

[Spring] 자바17+스프링3.x로 업그레이드하며 생긴 이슈. (Spring Security, Swagger)

데브헨리 2024. 2. 18. 19:11
728x90

1. 스웨거 springfox 사용 불가 -> springdoc 사용

문법도 조금 바뀜.

 

 

2. spring security 6.2이상에서 설정 방법 변경

public final class HttpSecurity extends AbstractConfiguredSecurityBuilder<DefaultSecurityFilterChain, HttpSecurity>
	implements SecurityBuilder<DefaultSecurityFilterChain>, HttpSecurityBuilder<HttpSecurity> {
    .
    .
        
	public HttpSecurity cors(Customizer<CorsConfigurer<HttpSecurity>> corsCustomizer) throws Exception {
		corsCustomizer.customize(getOrApply(new CorsConfigurer<>()));
		return HttpSecurity.this;
	}
    .
    .
}

 

HttpSecurity를 구성할때, 기존 dot(.)과 .and메서드로 나열하며 설정하던 방식에서  'Customizer'라는 함수형 인터페이스를 파라미터로 받아 설정을 처리하도록 바뀜.

 

@FunctionalInterface
public interface Customizer<T> {

    /**
     * Performs the customizations on the input argument.
     * @param t the input argument
     */
    void customize(T t);

    /**
     * Returns a {@link Customizer} that does not alter the input argument.
     * @return a {@link Customizer} that does not alter the input argument.
     */
    static <T> Customizer<T> withDefaults() {
       return (t) -> {
       };
    }

}

Customizer를 사용할때는 withDefaults()를 넘겨 특별한 동작없이 기본값을 넘길수있다.

또는 추상클래스 AbstractHttpConfigurer를 상속받는 각종 Configurer들을 T받아 customize 함수를 수행시키도록 사용할 수도있다.

(위의 예시코드 참고)

http.cors()는 CorsConfigurer를 받아서 처리하도록 구성되있는데, 개발자가 직접 람다식 (또는 익명클래스)를 적어주어 customize할 동작을 정의할수있다.

 

 

스프링 시큐리티는 원래도 내용이 너무 깊고 복잡해서 어려워서 디테일한 공부는 후순위로 미뤄두고 그냥 사용하고 있었는데.. 자세히 공부를 안해서 그럴까 ㅜ 이번 버전이 올라가며 코드 형태가 많이 바뀌어 아주 당황스러웠다. 구글링에 아직 관련 정보도 적어서 대응하기 다소 힘들었다.

728x90
반응형