Spring security 从web安全性中排除一个url

Spring security 从web安全性中排除一个url,spring-security,Spring Security,我的SpringWebSecurity中有以下内容 http .headers() .and() .sessionManagement() .and() .authorizeRequests() .antMatchers("/api/info").permitAll() .antMatchers("/api/**").authenticated(); 除了我有一个url,我想允许一个

我的SpringWebSecurity中有以下内容

        http
        .headers()
    .and()
        .sessionManagement()
    .and()
        .authorizeRequests()
        .antMatchers("/api/info").permitAll()
        .antMatchers("/api/**").authenticated();
除了我有一个url,我想允许一个许可证,例如/api/state/

我尝试了一些不起作用的方法(身份验证失败),例如

我正在寻找的是一种向以下内容添加排除的方法

.antMatchers("/api/**").authenticated();

我希望避免在api下列出所有URL并将它们设置为已验证的

看起来问题是按规则排序的。您应该首先声明
permitAll
规则:

    .antMatchers("/api/state/**").permitAll()
    .antMatchers("/api/**").authenticated();

事实上,规则的顺序必须从最具体到较少。如果首先声明
“/api/**”
,则以
/api/state/
开头的URL将匹配
“/api/**”
,因此antMatcher
“/api/state/**”
的规则将不适用于不起作用的URL。鲍里诺的回答很有效
    .antMatchers("/api/state/**").permitAll()
    .antMatchers("/api/**").authenticated();