如何仅将Spring Security AntMatchers模式应用于具有pathVariable的url

Ali*_*nik 5 java spring spring-security spring-boot

我正在使用Spring Boot并WebSecurityConfigurerAdapter配置安全性。

配置被忽略的安全性antMatches的方法如下所示:

    @Override
    public void configure(final WebSecurity web) {
        web
           .ignoring()
           .antMatchers("/items")
           .antMatchers("/items/{itemId}")
Run Code Online (Sandbox Code Playgroud)

{itemId}UUID格式在哪里

问题是,这种配置端点喜欢/items/report/items/images也被打开,但他们不应该。

有没有办法将忽略规则仅应用于具有路径变量的uri?

小智 4

你可以试试这个,d代表itemId

antMatchers("/items/{\\d+}").access("hasAnyAuthority('ROLE')")
Run Code Online (Sandbox Code Playgroud)

如果你想允许所有

antMatchers("/items/**").permitAll()
Run Code Online (Sandbox Code Playgroud)