사이트 개발/트러블슈팅

[Spring-Security] http.with 가 없는 메소드로 뜰때, 빨간줄 에러

OneCrazyman 2024. 1. 18. 18:26

문제

@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception{
    http
            // token을 사용하는 방식이기 때문에 csrf를 disable합니다.
            //corsfilter
            .csrf(AbstractHttpConfigurer::disable)
            .exceptionHandling(exceptionHandling->exceptionHandling
                    .accessDeniedHandler(jwtAccessDeniedHandler)
                    .authenticationEntryPoint(jwtAuthenticationEntryPoint)
            )
            //api 허가
            .authorizeHttpRequests(authz -> authz
                    .requestMatchers("/api/hello","/api/authenticate","/api/signup").permitAll()
                    .requestMatchers(PathRequest.toH2Console()).permitAll() //h2 콘솔 허가
                    .anyRequest().authenticated()
            )
            // 세션을 사용하지 않기 때문에 STATELESS로 설정
            .sessionManagement(sessionManagement ->
                    sessionManagement.sessionCreationPolicy(SessionCreationPolicy.STATELESS)
            )

            // enable h2-console
            .headers(headers ->
                    headers.frameOptions(HeadersConfigurer.FrameOptionsConfig::sameOrigin)
            )
            .with(new JwtSecurityConfig(tokenProvider),customizer -> {});

    return http.build();
}

 

에러

 

JWT를 적용하기 위해 SecurityConfig를 작성 중 http.with가 동작하지 않았다.

.with 메소드

해결

메소드는 3.2.x 이상부터 사용할 수 있어서 기존의 프로젝트 3.1.7버전에서 3.2.1로 버전업해주어 해결하였다.

 

이하 버전은 http.apply() 메소드를 사용하여 진행할수 있겠다.

참고

https://stackoverflow.com/questions/77837619/its-the-same-code-as-github-but-i-cant-get-the-http-with-method-in-spring-boo?noredirect=1#comment137222844_77837619

 

It's the same code as GitHub, but I can't get the http.with method In spring boot for jwt tutorial

I'm currently developing a website and watching JWT tutorials to apply the code. Here code is a function of the SecurityConfig class in Spring boot. @Bean public SecurityFilterChain filterChain(

stackoverflow.com