【问题标题】:AntMatchers are not working in Spring Security [duplicate]AntMatchers 在 Spring Security 中不起作用 [重复]
【发布时间】:2022-01-26 15:22:38
【问题描述】:

我的应用程序中有两个 URL:

  • /sa/abc(角色应该可以访问 - ABC)
  • /sa/practice(角色应该可以访问 - ADMIN)

为此我已配置:

http
    .sessionManagement()
        .sessionCreationPolicy(SessionCreationPolicy.STATELESS).and()
    .authorizeRequests()
        .antMatchers("/sa/**").authenticated()
        .antMatchers("/sa/abc/**").hasAnyAuthority("ABC")
        .antMatchers("/sa/practices/**").hasAnyAuthority("ADMIN")

我希望角色为ABC 的用户无法访问/sa/practices/link1,但他可以。

我还想知道antMatchers 中未提及的链接会发生什么情况。我的猜测是,无论角色如何,都可以毫无问题地访问它们。

我说的对吗?

【问题讨论】:

    标签: java spring spring-boot spring-security


    【解决方案1】:

    订单很重要。第一个匹配的 ant 模式决定访问权限。所以在你的情况下:

    http
        .sessionManagement()
            .sessionCreationPolicy(SessionCreationPolicy.STATELESS)
            .and()
        .authorizeRequests()
            .antMatchers("/sa/abc/**").hasAnyAuthority("ABC")
            .antMatchers("/sa/practices/**").hasAnyAuthority("ADMIN")
            .antMatchers("/sa/**").authenticated()
    

    请注意,最不具体的路径是最后一个。

    【讨论】:

      猜你喜欢
      • 2018-01-31
      • 2019-09-27
      • 2018-03-27
      • 2013-12-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多