【问题标题】:Match request method with Kotlin DSL for Spring Security将请求方法与 Kotlin DSL 匹配以实现 Spring Security
【发布时间】:2020-08-25 09:37:42
【问题描述】:

Spring Security 提供了 Kotlin DSL 以便于配置。以下是来自Spring Blog 的示例:

override fun configure(http: HttpSecurity?) {
    http {
        httpBasic {}
        authorizeRequests {
            authorize("/greetings/**", hasAuthority("ROLE_ADMIN"))
            authorize("/**", permitAll)
        }
    }
}

我只想允许对特定路径的 POST 请求。在 Java 中,您可以这样做:

http
  .httpBasic().and()
  .authorizeRequests()
    .antMatchers(HttpMethod.POST, "/greetings").hasRole("ADMIN");

有使用 Kotlin DSL 的示例吗?

【问题讨论】:

    标签: spring kotlin spring-security kotlin-dsl


    【解决方案1】:

    问得太早了。看来我在查看源代码后找到了解决方案。

    添加以供将来参考:

    override fun configure(http: HttpSecurity) {
        http {
            csrf { disable() }
            httpBasic {}
            authorizeRequests {
                authorize(AntPathRequestMatcher("/greetings", HttpMethod.POST.name),  hasRole("ADMIN"))
                authorize("/**", denyAll)
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2020-12-23
      • 2021-05-07
      • 1970-01-01
      • 1970-01-01
      • 2020-07-10
      • 2020-08-12
      • 2013-06-25
      • 2017-07-01
      相关资源
      最近更新 更多