사붐이개발일기

[Spring Security] Spring Security6 이상에서 hasIpAddress 사용하는법 (feat. springboot 3.0 본문

인프런/[Spring Cloud] MSA-이도원

[Spring Security] Spring Security6 이상에서 hasIpAddress 사용하는법 (feat. springboot 3.0

sabeom 2023. 11. 29. 17:23

WebConfig

@Configuration
@EnableWebSecurity
public class WebSecurity {
    private static final String IP_ADDRESS = "172.0.0.1";
    private static final String SUBNET = "/32";
    private static final IpAddressMatcher IP_ADDRESS_MATCHER = new IpAddressMatcher(IP_ADDRESS + SUBNET);
    private static final String[] WHITE_LIST = {
            "/**",
            "/users/**"
    };

    @Bean
    protected SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
        http.csrf(csrf -> csrf.disable())
                .headers(headers -> headers
                        .frameOptions(frameOptions -> frameOptions.disable())
                )
                .authorizeHttpRequests(authorize -> authorize
//                        .requestMatchers(AntPathRequestMatcher.antMatcher("/users/**")).permitAll()
                        .requestMatchers(AntPathRequestMatcher.antMatcher("/**")).access(this::hasIpAddress)
                        .requestMatchers(PathRequest.toH2Console()).permitAll()
                );
        return http.build();
    }

    private AuthorizationDecision hasIpAddress(Supplier<Authentication> authentication, RequestAuthorizationContext object) {
        return new AuthorizationDecision(IP_ADDRESS_MATCHER.matches(object.getRequest()));
    }

}

 
 
참고
https://jongmin4943.tistory.com/entry/Spring-Security-6-hasIpAddress-%EC%82%AC%EC%9A%A9%ED%95%98%EB%8A%94%EB%B2%95

Spring Security 6 hasIpAddress 사용하는법 (feat.Spring boot 3)

들어가며 이번 프로젝트에서 WhiteIpList 관리 기능 부분을 맡아 만들게 되어 해당 기능을 Security 를 이용해서 붙여보려 했었다. 결국은 다르게 구현했지만 Security 로는 어떻게 할 수 있을까 공부 해

jongmin4943.tistory.com