programing

스프링 보안에 대한 요청 Matchers() 이해

lovejava 2023. 6. 23. 21:08

스프링 보안에 대한 요청 Matchers() 이해

저는 스프링 보안 코드를 공부하고 있습니다.인터넷 1에서 발견한 이 예를 이해하고 싶습니다.

http.requestMatchers()
        .antMatchers("/management/**") // (1)
        .and()
        .authorizeRequests() // (2)
        .antMatchers("/management/health")
        .permitAll()
        .antMatchers("/management/info")
        .permitAll()
        .antMatchers("/management/**")
        .hasRole("ACTUATOR")
        .anyRequest().permitAll()
        .and()
        .httpBasic(); (3)

}

이 구성을 이해할 수 없습니다. 왜 이 코드가 다음과 같습니다.

http.requestMatchers()
        .antMatchers("/management/**")
        .and() 

.authorizeRequests() 앞에 있습니까? (1)

그게 무슨 의미죠?

이 예를 설명해 주시겠습니까?

2: 두 번째 경우, 차이점은 무엇입니까?

http.requestMatchers().antMatchers("/rest2/**")
.and()
.authorizeRequests()
.antMatchers("/rest/v1/test/hello").permitAll()
.antMatchers("/rest/v1/test/**").denyAll()
.and()
.requestMatchers().antMatchers("/rest/**")
.and()
.authorizeRequests()
.antMatchers("/rest/v1/test/hello").permitAll();

requestMatchers()를 사용하면 어떤 영향이 있습니까?

요청을 "/rest/v1/test/hello2"로 보내면 401 요청을 거부하는 규칙이 antMatchers("/rest2/**")와 일치하지 않는 이유는 무엇입니까?

의 목적requestMatchers()스프링 보안 구성을 적용할 요청을 지정합니다.

예를 들어 끝점이 2개인 경우"/public"그리고."/private"보안(특히 csrf 보호)만 적용하기를 원하는 경우"/private"그런 다음 다음 구성을 추가할 수 있습니다.

http
    .requestMatchers()
        .antMatchers("/private/**")
        .and()
    .csrf();

그러면, 만약 당신이 게시한다면."/private"당신은 403 응답을 받을 것입니다.
하지만 당신이 투고를 한다면,"/public"당신은 200점을 받을 것입니다, 왜냐하면 보안이 적용되지 않았기 때문입니다.

이는 다음과 별개입니다.authorizeRequests보안이 적용되는지 여부와 반대로 해당 엔드포인트에 필요한 액세스 유형을 나타냅니다.

당신이 언급한 예 1에서

http
    .requestMatchers()
        .antMatchers("/management/**")
        .and() 
        ...

보안 구성은 다음에만 적용됩니다."/management/**"그래서 만약 당신이 요청을 한다면."/foo"보안이 되지 않을 것입니다.

당신이 언급한 예 2에서,

http
    .requestMatchers()
        .antMatchers("/rest2/**")
        .and()
    .authorizeRequests()
        .antMatchers("/rest/v1/test/hello").permitAll()
        .antMatchers("/rest/v1/test/**").denyAll()
        .and()
    .requestMatchers()
        .antMatchers("/rest/**")
        .and()
    .authorizeRequests()
        .antMatchers("/rest/v1/test/hello").permitAll();

이유"/rest/v1/test/hello2"401로 응답하는 이유는"/rest/**"요청 일치자에 있으므로 보안 규칙.antMatchers("/rest/v1/test/hello").permitAll()적용됩니다.
만약 당신이 요청을 한다면,"/rest3/v1/test/hello2"그러면 그것은 200으로 응답할 것이다 왜냐하면"/rest3/**"요청 일치자의 일부가 아닙니다.

스프링 부트 3 업데이트

WebSecurityConfigurerAdapterSpring Security 5.5는 제거되었으며 Spring Security 5.5를 사용하여 보안을 구성하는 새로운 접근 방식을 도입했습니다.SecurityFilterChain인터페이스 - HttpSecurity Builder API는 더 이상 사용되지 않습니다.

예: 요청 허용/public/개미핥기식 주석을 유지하면서 다음을 사용합니다.

  @Bean
  public SecurityFilterChain configure(HttpSecurity http) throws Exception {
    http
        .authorizeHttpRequests((requests) -> requests
            .requestMatchers(new AntPathRequestMatcher("/public/**")).permitAll()
            .anyRequest().authenticated()) //other URLs are only allowed authenticated users.
        .httpBasic();
    return http.build();
  }

인증된 사용자의 정의는 다음을 사용하여 지정할 수 있습니다.JwtTokenFilter확장OncePerRequestFilter(예를 들어).

Spring 보안 API: 공용 최종 클래스 HttpSecurity입니다.RequestMatcherConfigurer가 AbstractRequestMatcherRegistry를 확장합니다.

이 HttpSecurity가 사용될 HTTP 요청을 매핑할 수 있습니다.

언급URL : https://stackoverflow.com/questions/52029258/understanding-requestmatchers-on-spring-security