본문 바로가기
Framekwork/SPRING

[스프링부트] 시큐리티 설정 ('authorizeHttpRequests()' is deprecated and makred for removal)

by 아이엠제니 2024. 3. 1.

 

<스프링 부트 3 백엔드 개발자 되기(자바 편)> 실습 中

 

 

 

실제 인증 처리를 하는 시큐리티 설정 파일을 책을 보고 따라 작성했다.

 

 

1. 시큐리티 설정 수정 전

WebSecurityConfig.java
package com.example.springbootBoard.config;

import com.example.springbootBoard.service.UserDetailService;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityCustomizer;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.web.SecurityFilterChain;

@RequiredArgsConstructor
@Configuration
public class WebSecurityConfigExample {

    private final UserDetailService userService;

    // 1. 스프링 시큐리티 기능 비활성화
    @Bean
    public WebSecurityCustomizer configure() {
        return (web) -> web.ignoring()
//                .requestMatchers(toH2Console())
                .requestMatchers("/static/**");
    }

    // 2. 특정 HTTP 요청에 대한 웹 기반 보안 구성
    @Bean
    public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
        return http
                .authorizeHttpRequests() // 3. 인증, 인가 설정
                .requestMatchers("/login", "/signup", "/user").permitAll()
                .and()
                .formLogin() // 4. 폼 기반 로그인 설정
                .loginPage("/login")
                .defaultSuccessUrl("/articles")
                .and()
                .logout() // 5. 로그아웃 설정
                .logoutSuccessUrl("/login")
                .invalidateHttpSession(true)
                .and()
                .csrf().disable() // 6. csrf 비활성화
                .build();
    }

    // 7. 인증 관리자 관련 설정
    @Bean
    public AuthenticationManager authenticationManager(HttpSecurity http, BCryptPasswordEncoder bCryptPasswordEncoder, UserDetailService userDetailService) throws Exception {
        return http
                .getSharedObject(AuthenticationManagerBuilder.class)
                .userDetailsService(userService) // 8. 사용자 정보 서비스 설정
                .passwordEncoder(bCryptPasswordEncoder)
                .and()
                .build();
    }

    // 9. 패스워드 인코더로 사용할 빈 등록
    @Bean
    public BCryptPasswordEncoder bCryptPasswordEncoder() {
        return new BCryptPasswordEncoder();
    }
}

기존에 책에 있는 코드대로 작성을 했더니?

 

 

이렇게 빨간 취소선을 볼 수 있었다.

 

 

인텔리제이에서 취소선이 있는 곳에 마우스선을 올려보면, 대략 이유를 볼 수 있다.

'authorizeHttpRequests()' is deprecated and makred for removal

 

사용 중지가 되었으며, 중지 예정이라고 한다.

더이상 권장되지 않기 때문에, 새로운 버전을 찾아 교체하는 게 좋아 보인다.

 

 

 

2. 시큐리티 설정 수정 후

WebSecurityConfig.java
package com.example.springbootBoard.config;

import com.example.springbootBoard.service.UserDetailService;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityCustomizer;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.web.SecurityFilterChain;

@RequiredArgsConstructor
@Configuration
public class WebSecurityConfig {

    private final UserDetailService userService;

    // 1. 스프링 시큐리티 기능 비활성화
    @Bean
    public WebSecurityCustomizer configure() {
        return (web) -> web.ignoring()
//                .requestMatchers(toH2Console())
                .requestMatchers("/static/**");
    }

    // 2. 특정 HTTP 요청에 대한 웹 기반 보안 구성
    // 책에서 나왔던 거랑 다르게 작성함
    // security 버전이 달라지면서, 람다식으로 적는 것을 지향
    @Bean
    public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
        return http
                .authorizeHttpRequests((authorizeRequests) -> // 3. 인증, 인가 설정
                        authorizeRequests
                                .requestMatchers("/login", "/signup", "/user").permitAll()
                                .anyRequest().authenticated()
                )
                .formLogin((formLogin) -> // 4. 폼 기반 로그인 설정
                        formLogin
                                .loginPage("/login")
                                .defaultSuccessUrl("/articles")
                )
                .logout((logoutConfig) -> // 5. 로그아웃 설정
                        logoutConfig
                                .logoutSuccessUrl("/login")
                                .invalidateHttpSession(true) // 세션 무효화
                )
                .csrf((csrfConfig) -> // 6.csrf 비활성화
                        csrfConfig.disable()
                )
                .build();
    }

    // 7. 인증 관리자 관련 설정
    @Bean
    public AuthenticationManager authenticationManager(HttpSecurity http, BCryptPasswordEncoder bCryptPasswordEncoder, UserDetailService userDetailService) throws Exception {
        return http
                .getSharedObject(AuthenticationManagerBuilder.class)
                .userDetailsService(userService) // 8. 사용자 정보 서비스 설정
                .passwordEncoder(bCryptPasswordEncoder)
                .and()
                .build();
    }

    // 9. 패스워드 인코더로 사용할 빈 등록
    @Bean
    public BCryptPasswordEncoder bCryptPasswordEncoder() {
        return new BCryptPasswordEncoder();
    }
}

그래서 위와 같이 수정을 했다.

버전이 달라지면서, 현재는 람다식으로 적는 것을 지향한다고 한다.

위처럼 작성하면, 더이상의 취소선은 안 보인다.

300x250