source

pathVariable이 있는 URL에만 Spring Security AntMatchers 패턴을 적용하는 방법

bestscript 2023. 8. 8. 23:44

pathVariable이 있는 URL에만 Spring Security AntMatchers 패턴을 적용하는 방법

boot과 는나 Spring boot을 .WebSecurityConfigurerAdapter보안을 구성합니다.

무시된 보안 antMatch를 구성하는 방법은 다음과 같습니다.

    @Override
    public void configure(final WebSecurity web) {
        web
           .ignoring()
           .antMatchers("/items")
           .antMatchers("/items/{itemId}")

{itemId} 형식 UUID .

는 이 과 같은 구성 끝점의 /items/report,/items/images열리지만 열리면 안 됩니다.

경로 변수가 있는 uri에만 규칙 무시를 적용할 수 있는 방법이 있습니까?

당신은 이것을 시도할 수 있습니다, d는 아이템을 대표합니다.이드

antMatchers("/items/{\\d+}").access("hasAnyAuthority('ROLE')")

모든 것을 허가하고 싶다면.

antMatchers("/items/**").permitAll()

의 설명서에 따라 다음과 같이 정규식을 사용하여 경로 변수를 지정해야 합니다.

{spring:[a-z]+}은(는) regexp [a-z]+와 "spring"이라는 이름의 경로 변수와 일치합니다.

당신의 경우는 다음과 같습니다.

@Override
    public void configure(final WebSecurity web) {
        web
           .ignoring()
           .antMatchers("/items/{itemId:[\\d+]}")

Ant 패턴이 수행할 수 있는 것보다 더 정교한 일치를 위해서는 컨트롤러 또는 (더 나은) 서비스 계층 방법에 기반한 방법 기반 보안을 사용합니다.

컨트롤러의 해당 URL에 대한 처리기 메서드에서 @preAuthorize 주석을 사용하거나 이 링크를 확인할 수 있습니다. https://docs.spring.io/spring-security/site/docs/4.2.x/reference/html/el-access.html

언급URL : https://stackoverflow.com/questions/55863235/how-to-apply-spring-security-antmatchers-pattern-only-to-url-with-pathvariable