sourcecode

개미매처() 대 개미매처()의 스프링 보안 적용

copyscript 2023. 8. 2. 09:19
반응형

개미매처() 대 개미매처()의 스프링 보안 적용

제가 이 질문에 대한 답을 제대로 해석하고 있는지 알고 싶습니다.

다음과 같이 하나의 경로만 보호하면 됩니다.

http.antMatcher("/api/**").authorizeRequests()....

사용할 경우antMatcher().

다음과 같이 여러 URL 경로를 보호해야 하는 경우:

http
.authorizeRequests()
    .antMatchers("/high_level_url_A/sub_level_1").hasRole('USER')
    .antMatchers("/high_level_url_A/sub_level_2").hasRole('USER2')
    ...

사용할 경우antMatchers().

이 질문에는 두 가지 대답이 있지만, 각각의 예에서 제공된 예는 다른 예에서 제공된 예와 모순됩니다.첫 번째 대답은 저자가 필요하지 않다고 말합니다.antMatcher()그리고 두 번째는 항상 'antMatcher() IIUC로 시작하라고 말합니다.

Http 보안.AntMatcher()HttpSecurity 인스턴스에 대한 기본 요청 일치를 AntPathRequestMatcher에서 AntPathRequestMatcher로 변경합니다.ExpressionUrlAuthorizationConfigurer입니다.표현InterceptUrlRegistry.antMatchers()는 현재 HttpSecurity 인스턴스와 연결된 엔드포인트의 하위 집합에 권한 부여 규칙을 적용하는 데 사용됩니다.

코드 예제:

http
    .antMatcher("/api/**")
    .httpBasic()
        .disable()
    .authorizeRequests()
        .antMatchers("/api/user/**", "/api/ticket/**", "/index")
            .hasRole("USER");

위의 예에서는 /api/**와 일치하는 모든 엔드포인트에 대해 기본 권한 부여를 사용할 수 없습니다.또한 /api/user/** 또는 /api/ticket/**와 일치하는 엔드포인트에는 요청의 인증에 ROLE_USER가 포함되어야 합니다.그러나 사용자가 /index에 액세스하려고 하면 기본 인증 프롬프트가 표시됩니다.자격 증명을 입력하면 요청의 인증에 ROLE_USER가 포함되어 있는지 여부에 관계없이 사용자에게 엔드포인트에 대한 액세스 권한이 부여됩니다.이는 .antMatcher("/api/**")가 전체 HttpSecurity 인스턴스의 범위를 특정 AntMatcher로 제한하고 있기 때문입니다.

아래 예제에서는 HttpSecurity의 범위에 이전 AntMatchers 3개가 포함되어 있으며 그 외에는 아무것도 포함되어 있지 않습니다.

http
    .requestMatchers()
        .antMatchers("/api/user/**", "/api/ticket/**", "/index")
        .and()
    .httpBasic()
        .disable()
    .authorizeRequests()
        .any()
            .hasRole("USER");

편집 #hasRole()을 사용하는 경우 역할이 자동으로 삽입되므로 "ROLE_"로 시작하면 안 됩니다.

antMatcher()를 사용하면 제공된 개미 패턴과 일치하는 경우에만 HttpSecurity를 호출하도록 구성할 수 있습니다.

고급 구성이 필요한 경우 requestMatchers() 또는 requestMatcher(RequestMatcher)를 사용하는 것이 좋습니다.

antMatcher()를 호출하면 antMatcher(), mvcMatcher(), requestMatcher(), regexMatcher()requestMatcher()의 이전 호출이 재정의됩니다.

requestMatchers의 사용 예는 아래를 참조하십시오.

   @Configuration
   @EnableWebSecurity
   public class RequestMatchersSecurityConfig extends WebSecurityConfigurerAdapter {
  
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            .requestMatchers((requestMatchers) ->
                requestMatchers
                    .antMatchers("/api/**")
                    .antMatchers("/oauth/**")
            )
            .authorizeRequests((authorizeRequests) ->
                authorizeRequests
                    .antMatchers("/**").hasRole("USER")
            )
            .httpBasic(withDefaults());
    }
   }

아래의 구성도 위의 구성과 동일합니다.

   @Configuration
   @EnableWebSecurity
   public class RequestMatchersSecurityConfig extends WebSecurityConfigurerAdapter {
  
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            .requestMatchers((requestMatchers) ->
                requestMatchers
                    .antMatchers("/api/**")
            )
            .requestMatchers((requestMatchers) ->
            requestMatchers
                .antMatchers("/oauth/**")
            )
            .authorizeRequests((authorizeRequests) ->
                authorizeRequests
                    .antMatchers("/**").hasRole("USER")
            )
            .httpBasic(withDefaults());
    }
   }

언급URL : https://stackoverflow.com/questions/47298079/spring-security-application-of-antmatcher-vs-antmatchers

반응형