> Java > java지도 시간 > 본문

Spring Security 6의 새로운 requestMatchers

WBOY
풀어 주다: 2024-07-19 12:18:31
원래의
703명이 탐색했습니다.

New requestMatchers in Spring Security 6

Spring Security 6에서는 경로 기반 액세스 제어 구성을 위해 더 이상 사용되지 않는 antMatchers, mvcMatchers 및 regexMatchers 메서드를 requestMatchers 메서드로 대체했습니다. 새로운 requestMatchers에 대한 주요 사항은 다음과 같습니다.

AuthorizeHttpRequests에서 requestMatchers 사용

HttpSecurity 구성의 AuthorizeHttpRequests 메소드를 사용하면 액세스 제어를 위한 세분화된 요청 일치를 구성할 수 있습니다. requestMatchers 메소드를 사용하여 허용되거나 인증되어야 하는 요청을 지정할 수 있습니다. 예:

@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
    return http.authorizeHttpRequests(auth -> auth
        .requestMatchers("/greet").permitAll()
        .anyRequest().authenticated())
        .formLogin()
        .build();
}
로그인 후 복사

이 구성은 다른 모든 요청에 ​​대해 인증을 요구하면서 인증 없이 /greet 엔드포인트에 대한 액세스를 허용합니다.

requestMatchers 및 securityMatchers

requestMatchers와 securityMatchers라는 두 가지 유사한 메서드가 있습니다. 둘 다 클래스 경로에 Spring MVC가 있는지에 따라 가장 적절한 RequestMatcher 구현을 선택합니다.

  • Spring MVC가 있는 경우 MvcRequestMatcher를 사용합니다.
  • Spring MVC가 없으면 AntPathRequestMatcher로 대체됩니다.

가장 큰 차이점은 securityMatchers는 WebSecurityCustomizer와 같은 곳에서 사용되는 반면 requestMatchers는 AuthorizeHttpRequests에서 사용된다는 것입니다.

올바른 일치자 선택

requestMatchers 메소드를 사용하면 AntPathRequestMatcher 또는 RegexRequestMatcher와 같은 특정 일치자에 의존하지 않고 패턴이나 기타 기준을 기반으로 요청을 일치시킬 수 있습니다. 이는 더 많은 유연성과 더 나은 기본값을 제공합니다.

특정 매처를 사용하려면 RequestMatcher 구현을 requestMatchers 메소드에 전달할 수 있습니다.

@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
    return http.authorizeHttpRequests(auth -> auth
        .requestMatchers(new AntPathRequestMatcher("/greet")).permitAll()
        .anyRequest().authenticated())
        .formLogin()
        .build();
}
로그인 후 복사

요약하자면, Spring Security 6의 새로운 requestMatchers 메소드는 경로 기반 액세스 제어를 구성하는 보다 유연하고 안전한 방법을 제공하여 애플리케이션의 종속성을 기반으로 가장 적절한 RequestMatcher 구현을 선택합니다.

위 내용은 Spring Security 6의 새로운 requestMatchers의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

원천:dev.to
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!