【问题标题】:Spring Boot Security - Ant Matcher doesn't workSpring Boot 安全性 - Ant Matcher 不起作用
【发布时间】:2019-12-18 20:04:18
【问题描述】:

我正在尝试熟悉 Spring Boot 安全性,我的 API 是一个简单的 GET/POST 到 127.0.0.1:8080/employee/ 像这样简单的自动配置。

@Configuration
public class SecurityConfig implements WebMvcConfigurer {

    @Configuration
    protected static class ApplicationSecurity extends WebSecurityConfigurerAdapter{

        @Override
        protected void configure(AuthenticationManagerBuilder auth) throws Exception {
            auth.inMemoryAuthentication()
                    .withUser("user1").password("{noop}user1").authorities("USER").and()
                    .withUser("user2").password("{noop}user2").authorities("USER").and()
                    .withUser("admin").password("{noop}admin").authorities("ADMIN");
        }

        @Override
        protected void configure(HttpSecurity http) throws Exception {
            http.antMatcher("/employee/**").authorizeRequests().anyRequest().hasRole("ADMIN");
        }
    }
}

这总是给我 403 Forbidden。 试过这个: - antMatcher("/employee*"),适用于任何用户。我能否在理解此模式的工作原理方面获得一些帮助,我只需要将“/employee”或“/employee/”或“/employee/1”限制为 Admin。

【问题讨论】:

    标签: spring-boot spring-security roles


    【解决方案1】:

    您当前的配置只会限制employee 下的任何路径,例如employee/1 但不是/employee。此外,当您返回authorizeRequests 时,您没有对employee 匹配器做任何事情,然后配置anyRequest 具有作用ADMIN

    employee 及其下方的任何路径限制为ADMIN

         http.authorizeRequests().antMatchers("/employee**", "/employee/**").hasRole("ADMIN").httpBasic();
    

    使用** 将捕获路径中的目录。

    https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/util/AntPathMatcher.html

    https://docs.spring.io/spring-security/site/docs/current/api/org/springframework/security/web/util/matcher/AntPathRequestMatcher.html

    使用 /** 或 ** 的模式值被视为通用匹配,它将匹配任何请求。以 /** 结尾(并且没有其他通配符)的模式通过使用子字符串匹配进行优化 - /aaa/** 的模式将匹配 /aaa、/aaa/ 和任何子目录,例如 /aaa/bbb /cc。

    我还建议通过 @WebMvcTest 切片测试来测试您的安全配置

    https://www.baeldung.com/spring-security-integration-tests

    从上面一个简单的例子是,

    @RunWith(SpringRunner.class)
    @WebMvcTest(SecuredController.class)
    public class SecuredControllerWebMvcIntegrationTest {
    
        @Autowired
        private MockMvc mvc;
    
        // ... other methods
    
        @WithMockUser(roles= {"admin"})
        @Test
        public void givenAuthRequestOnPrivateService_shouldSucceedWith200() throws Exception {
            mvc.perform(get("/employee/1").contentType(MediaType.APPLICATION_JSON))
              .andExpect(status().isOk());
        }
        //Repeated for other Paths
    }
    

    【讨论】:

    • 感谢您的解释,但我想我的理解仍然有问题,因为您的解决方案有效,但也允许非管理员.. 我想我必须阅读 spring 的整个文档..
    • @sagar 对配置的额外更改以启用 http basic。我确实制作了一个符合您的规范的基本应用程序,并且似乎可以按照您的意愿工作
    • 一些更正使它工作,禁用 csrf 帮助它正常运行。 csrf().disable。我的愚蠢错误是将 authorities 授予 auth 并检查 httpSecurity 中的 roles。Darren,是的,是 Postman 的 http 基础。非常感谢WebMvcTest 的建议!!
    猜你喜欢
    • 2019-08-12
    • 1970-01-01
    • 2021-12-27
    • 2019-08-25
    • 1970-01-01
    • 1970-01-01
    • 2015-06-20
    • 2011-07-03
    • 1970-01-01
    相关资源
    最近更新 更多