Spring Security 기본인증 설정 변경

Soo·2024년 4월 4일

Spring Security 기본인증에서는 스프링 시큐리티가 기본으로 제공해주는 보안 필터를 사용했습니다. 리소스에 접근을 하기 위해서는 무조건 로그인을 했어야 했습니다.

이번에는 Spring Security FilterChain을 커스텀 해서 사용해 보겠습니다.

study.rest.webservices.restfulwebservices 아래에 security폴더를 만들고 SpringSecurityConfiguration을 생성합니다.

SpringSecurityConfiguration

  • 모든 요청이 인증되도록 합니다.
package study.rest.webservices.restfulwebservices.security;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.web.SecurityFilterChain;

@Configuration
public class SpringSecurityConfiguration {

    @Bean
    public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
        http.authorizeHttpRequests(
                auth -> auth.anyRequest().authenticated()
        );

        return http.build();
    }
}

설정을 하자마자 바로 접근이 거부당했습니다.

이번에는 기본인증을 사용해보겠습니다.

SpringSecurityConfiguration

  • 기본인증 사용
  • Customizer.withDefaults() → static import를 사용해서 Customizer 제거
package study.rest.webservices.restfulwebservices.security;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.web.SecurityFilterChain;

import static org.springframework.security.config.Customizer.withDefaults;

@Configuration
public class SpringSecurityConfiguration {

    @Bean
    public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
        http.authorizeHttpRequests(
                auth -> auth.anyRequest().authenticated()
        );

        http.httpBasic(withDefaults());
        return http.build();
    }
}

스프링 기본보안을 사용하니 로그인 창이 나옵니다.

application.properties에 설정해 둔 아이디와 비밀번호로 로그인이 가능합니다.

마지막으로, CSRF를 제거합니다.

CSRF(Cross-Site Request Forgery)

사용자가 의도하지 않은 요청을 웹 응용 프로그램에 보내는 공격 기법입니다.

공격자는 희생자의 브라우저를 이용하여 인증된 세션을 이용하여 의도하지 않은 요청을 보내고, 이를 통해 피해를 입히려고 시도합니다.

예를 들어, 사용자가 인터넷 뱅킹에 로그인되어 있는 상태에서 공격자가 만든 피싱 사이트를 통해 의도하지 않은 이체 요청이 이루어지는 것과 같은 상황이 될 수 있습니다.

SpringSecurityConfiguration

  • CSRF 제거 (POST, PUT 요청을 Postman에서 보내기 위해)
package study.rest.webservices.restfulwebservices.security;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.web.SecurityFilterChain;

import static org.springframework.security.config.Customizer.withDefaults;

@Configuration
public class SpringSecurityConfiguration {

    @Bean
    public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
        http.authorizeHttpRequests(
                auth -> auth.anyRequest().authenticated()
        );

        http.httpBasic(withDefaults());

        http.csrf(
                csrf -> csrf.disable()
        );
        return http.build();
    }
}

POST 요청이 정상적으로 실행되었습니다.

0개의 댓글