• 카테고리

    질문 & 답변
  • 세부 분야

    백엔드

  • 해결 여부

    미해결

8:45 spring security 3.1.5 설정 방법 (버전 안 맞춰서 안될때)

23.11.02 12:03 작성 23.11.02 12:06 수정 조회수 1.47k

1

spring security 3.1.5 버전 방식입니다.
기존에 implement 하지 않고 클래스를 @configuration 해서
구성 파일로 인식하게 만들고 해당 메서드를 @bean 을 주입시켜 사용하는 방식입니다.
처음 참조할 부분은 여기를 참조 하시면 됩니다. 처음 설정 방법 :: 3.1.5  

 

방법 1

  1. SecurityConfig::SecurityFilterChain 메서드를 수정하기

    @Bean
    public SecurityFilterChain filterChain(HttpSecurity httpSecurity) throws Exception {
        httpSecurity
                .csrf(AbstractHttpConfigurer::disable)

                .exceptionHandling((handling) ->
                        handling.authenticationEntryPoint(jwtAuthenticationEntryPoint)
                                .accessDeniedHandler(jwtAccessDeniedHandler)
                )

                .headers((header) ->
                        header.frameOptions(
                                HeadersConfigurer.FrameOptionsConfig::sameOrigin
                        )
                )

                .authorizeHttpRequests((registry) ->
                        registry.requestMatchers("/api/hello").permitAll()
                                .requestMatchers("/api/authentication").permitAll()
                                .requestMatchers("/api/signup").permitAll()
                                .anyRequest().authenticated()
                );
        return httpSecurity.build();
    }

 

방법 2

  1. JwtSecurityConfig에 메서드를 하나 추가 한다.

    public HttpSecurity configureAndReturn(HttpSecurity httpSecurity) {
        httpSecurity.addFilterBefore(
                new JwtFilter(tokenProvider),
                UsernamePasswordAuthenticationFilter.class
        );
        return httpSecurity;
    }
  1. SecurityConfig::SecurityFilterChain 메서드를 수정하기

@Bean
public SecurityFilterChain filterChain(HttpSecurity httpSecurity) throws Exception {
    return new JwtSecurityConfig(tokenProvider).configureAndReturn(
            httpSecurity
                    .csrf(AbstractHttpConfigurer::disable)

                    .exceptionHandling((handling) ->
                            handling.authenticationEntryPoint(jwtAuthenticationEntryPoint)
                                    .accessDeniedHandler(jwtAccessDeniedHandler)
                    )
                        .headers((header) ->
                            header.frameOptions(
                                    HeadersConfigurer.FrameOptionsConfig::sameOrigin
                            )
                    )

                    .authorizeHttpRequests((registry) ->
                            registry.requestMatchers("/api/hello").permitAll()
                                    .requestMatchers("/api/authentication").permitAll()
                                    .requestMatchers("/api/signup").permitAll()
                                    .anyRequest().authenticated()
                    )
    ).build();
}

 

개인적으로는 방법 1이 깔끔하다고 느낍니다.

답변 1

답변을 작성해보세요.

0

terrinens님의 프로필

terrinens

질문자

2023.11.02

코드 설정에서 몇 가지 빠진 부분이 있어 댓글로 달아요.
게시글 수정이 안되네요...

### 방법 1, 2 둘다 넣어주세요 ###
.sessionManagement((session) -> session.sessionCreationPolicy(SessionCreationPolicy.STATELESS))

### 방법 1 에서만 넣어주세요 !체인 메서드 마지막 부분에 추가할 코드! ###
.apply(new JwtSecurityConfig(tokenProvider))
developer님의 프로필

developer

2023.12.17

안녕하세요! 기존에 depreciated 된 부분을 제외하고, 람다식으로 새롭게 올려주신 방법 잘 찹고하였습니다.

그런데 댓글로 추가 달아주신, .apply(new JwtSecurityConfig(tokenProvider)); 또한 'apply(C)' is "deprecated since version 6.2 and marked for removal" 라고 뜨는데... 혹시 어떻게 고쳐야하는지 알 수 있을까요?