[Spring Security]Security Config 인가작업

원모어깨찰빵·2024년 3월 10일
0

스프링

목록 보기
7/17

인가작업의 목표

필터를 설정하여 특정한 경로에 대하여 모두에게 오픈하여 놓거나, 특정 권한을 가져야만 접근할 수 있도록 한다.
기본적으로 모든 경로에 대하여 로그인을 요구하지만, Config 클래스를 등록하여 수정할 수 있다.
등록한 Config 클래스는 상단에서부터 순서대로 반영된다.

예시 SpringSecurity 코드

@Configuration //해당 클래스를 config등록
@EnableWebSecurity //security 설정
public class SecurityConfig {
    @Bean
    public SecurityFilterChain filterChain(HttpSecurity http) throws Exception{ //메소드명 자유
        http
                .authorizeHttpRequests((auth) -> auth
                        .requestMatchers("/", "/login").permitAll()  //모두 접근 가능
                        .requestMatchers("/admin").hasRole("ADMIN")  //ADMIN role 가져야 접근가능
                        .requestMatchers("/my/**").hasAnyRole("ADMIN", "USER") //여러가지 role 지정
                        .anyRequest().authenticated()  //로그인만 진행하면 접근가능(나머지 경로들)
                );
        return http.build();
    }
}

이 때, SpringBoot의 버전에 따라 자동으로 결정되는 SpringSecurity의 버전이 바뀔 때마다 시큐리티 구현 방법이 달라지는데, Spring 깃허브에서 확인할 수 있다.

Reference

개발자 유미 유튜브
개발자 유미 노션

profile
https://fuzzy-hose-356.notion.site/1ee34212ee2d42bdbb3c4a258a672612

0개의 댓글