사붐이개발일기
[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:23WebConfig
@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()));
}
}
Spring Security 6 hasIpAddress 사용하는법 (feat.Spring boot 3)
들어가며 이번 프로젝트에서 WhiteIpList 관리 기능 부분을 맡아 만들게 되어 해당 기능을 Security 를 이용해서 붙여보려 했었다. 결국은 다르게 구현했지만 Security 로는 어떻게 할 수 있을까 공부 해
jongmin4943.tistory.com
'인프런 > [Spring Cloud] MSA-이도원' 카테고리의 다른 글
[JWT] Security Authentication in user-service (0) | 2023.12.05 |
---|---|
[Eureka] ApiGateway Routers 재정의 (/user-service/ 제거) (0) | 2023.12.01 |
[Spring MSA]섹션4. Users Microservice 에러사항 (0) | 2023.11.27 |
[Spring MSA]섹션3. E-commerce 애플리케이션 구조 (0) | 2023.11.23 |
[Spring MSA]섹션2. API 게이트웨이 서비스 (0) | 2023.11.21 |