【问题标题】:Spring Security Configuration Kotlin DSLSpring Security 配置 Kotlin DSL
【发布时间】:2020-12-23 08:19:42
【问题描述】:

所以,我的配置器适配器中有这个 java 代码:

http.cors().and().csrf().disable()
    .authorizeRequests().antMatchers(HttpMethod.POST, Constants.CREATE_USER_URL).permitAll()
    .and().authorizeRequests().antMatchers(HttpMethod.GET, "/v2/api-docs", "/swagger-resources/**", "/swagger-ui/**", "/swagger-ui.html**", "/webjars/**", "favicon.ico").permitAll().anyRequest().authenticated()
    .and().addFilter(new JwtAuthenticationFilter(authenticationManager())).addFilter(new BasicJwtAuthenticationFilter(authenticationManager()))
    .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);

我尝试使用新的 Kotlin DSL:

http {
  cors { disable() }
  csrf { disable() }
  authorizeRequests {
    authorize(AntPathRequestMatcher(createUserUrl, HttpMethod.POST.name), permitAll)
    authorize(AntPathRequestMatcher("favicon.ico", HttpMethod.GET.name), permitAll)
    authorize(AntPathRequestMatcher("/v2/api-docs", HttpMethod.GET.name), permitAll)
    authorize(AntPathRequestMatcher("/swagger-resources/**", HttpMethod.GET.name), permitAll)
    authorize(AntPathRequestMatcher("/swagger-ui/**", HttpMethod.GET.name), permitAll)
    authorize(AntPathRequestMatcher("/webjars/**", HttpMethod.GET.name), permitAll)
    authorize(anyRequest, authenticated)
  }
  addFilterAt(JwtAuthenticationFilter(authenticationManager()), AuthenticationFilter::class.java)
  addFilterAt(BasicJwtAuthenticationFilter(authenticationManager()), BasicAuthenticationFilter::class.java)
  sessionManagement { SessionCreationPolicy.STATELESS }
}

这个 kotlin dsl 是否具有与 java 代码相同的功能? kotlin dsl没有addFilter吗?

我可以减少冗余的authorize(在 Java 代码上,它使用接受多种模式的 antMatchers)具有相似代码 (permitAll HTTP GET)??

【问题讨论】:

  • 其他选项是使用authorize(EndpointRequest.to("/a", "/b", "/c"), permitAll),虽然我不认为您可以指定允许的确切HTTP方法,因此它允许每种方法。
  • @kdev 我尝试在 kotlin dsl 中使用 for 循环。这种方法有什么问题吗?它编译并运行正常,但没有正确测试。
  • 看不出有什么问题。您可以在 dsl 上创建扩展方法,并将其称为相同的 authorize,它接受:HTTP 方法和可变参数字符串端点作为参数,并在方法内部迭代并注册它们

标签: java spring-boot kotlin spring-security


【解决方案1】:

您的 Kotlin 配置不等同于您共享的 Java 配置。

一、CORS配置

http
    .cors()
    .and()
    // ...

下面是等效的 Kotlin 配置,因为您启用的是 CORS 而不是禁用它。

http {
    cors { }
}

二、会话管理配置

http
    // ...
    .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);

下面是等效的 Kotlin 配置,您要在其中分配 SessionCreationPolicy。

http {
    sessionManagement {
        sessionCreationPolicy = SessionCreationPolicy.STATELESS
    }
}

关于addFilter 方法,它在Javadoc 中声明

添加过滤器,该过滤器必须是安全框架中提供的过滤器的实例或扩展其中之一。

如果您的自定义过滤器 BasicJwtAuthenticationFilterBasicAuthenticationFilter 的一个实例,那么 Kotlin 配置是正确的。

将所有这些加在一起,您将获得以下 Kotlin 配置

http {
    cors { }
    csrf { disable() }
    authorizeRequests {
        authorize(AntPathRequestMatcher(createUserUrl, HttpMethod.POST.name), permitAll)
        authorize(AntPathRequestMatcher("favicon.ico", HttpMethod.GET.name), permitAll)
        authorize(AntPathRequestMatcher("/v2/api-docs", HttpMethod.GET.name), permitAll)
        authorize(AntPathRequestMatcher("/swagger-resources/**", HttpMethod.GET.name), permitAll)
        authorize(AntPathRequestMatcher("/swagger-ui/**", HttpMethod.GET.name), permitAll)
        authorize(AntPathRequestMatcher("/webjars/**", HttpMethod.GET.name), permitAll)
        authorize(anyRequest, authenticated)
    }
    addFilterAt(JwtAuthenticationFilter(authenticationManager()), AuthenticationFilter::class.java)
    addFilterAt(BasicJwtAuthenticationFilter(authenticationManager()), BasicAuthenticationFilter::class.java)
    sessionManagement {
        sessionCreationPolicy = SessionCreationPolicy.STATELESS
    }
}

【讨论】:

    猜你喜欢
    • 2021-05-07
    • 1970-01-01
    • 2020-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-23
    • 2011-01-08
    • 2020-03-04
    相关资源
    最近更新 更多