[SpringSecurity] 필터 체인

이다혜·2023년 11월 30일
0

Spring

목록 보기
20/27
@Bean
    SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
        http
                .authorizeHttpRequests(
                        authorizeHttpRequests -> authorizeHttpRequests
                                .requestMatchers(
                                        PathRequest.toStaticResources().atCommonLocations(),
                                        new AntPathRequestMatcher("/resources/**"),
                                        new AntPathRequestMatcher("/h2-console/**")
                                )
                                .permitAll()
                                .requestMatchers(
                                        "/adm/**"
                                )
                                .hasRole("ADMIN")
                                .anyRequest()
                                .permitAll()
                )
                .headers(
                        headers -> headers
                                .addHeaderWriter(
                                        new XFrameOptionsHeaderWriter(
                                                XFrameOptionsHeaderWriter.XFrameOptionsMode.SAMEORIGIN)
                                )
                )
                .csrf(
                        csrf -> csrf
                                .ignoringRequestMatchers(
                                        "/h2-console/**"
                                )

                )
                .formLogin(
                        formLogin -> formLogin
                                .loginPage("/member/login")
                )
                .logout(
                        logout -> logout
                                .logoutRequestMatcher(
                                        new AntPathRequestMatcher("/member/logout")
                                )
                )
        ;
        return http.build();
    }
  • SecurityFilterChain : Spring Security 필터 체인을 정의한다.

  • authorizeHttpRequests : 요청에 대항 인가 규칙을 정의한다.

  • PathRequest.toStaticResources().atCommonLocations().permitAll()
    : 정적 리소스에 대한 요청은 인증 없이 허용한다.
    /css/**, /js/**, /images/**

  • new AntPathRequestMatcher("/resources/").permitAll()
    : /resouces/
    패턴에 대한 요청은 인증 없이 허용한다.

  • new AntPathRequestMatcher("/h2-console/").permitAll()
    :/h2-console/
    패턴에 대한 요청은 인증 없이 허용한다.

  • requestMatchers("/adm/").hasRole("ADMIN")
    : /adm/
    패턴에 해당하는 요청은 ADMIN 권한이 있는 사용자에게만 허용한다.
    -.anyRequest().permitAll()
    : 이외의 모든 요청에 대해 모든 사용자에게 접근을 허용한다.

0개의 댓글