Spring 安全 OAuth 类型安全 SpEL 生成器

Spring Security OAuth type-safe SpEL builder

我们正在使用 spring-security + spring-security-oauth 实现 OAuth2 资源服务器。

用例非常简单:

使用 Java 配置,为了加强安全约束,我们使用了很多像这样的 SpEL :

@Override
public void configure(HttpSecurity http) throws Exception {
    http
        .authorizeRequests()

             // A. Only authorized clients
             .antMatchers("/type-a").access("#oauth2.hasScope('READ_SOMETHING')")

             // B. Both authorized clients and users
             .antMatchers("/type-b").access("#oauth2.hasScope('READ_SOMETHING_ELSE') or (!#oauth2.isOAuth() and hasRole('ROLE_USER'))")

             // ... (different flavors of B.) ...

             // C. Everything else is for authenticated users
            .anyRequest().hasRole("USER")
            .and().httpBasic();
}

虽然这很好用,但我不喜欢:

  1. SpEL 标记本身可能会很棘手,直到运行时才能验证
  2. 在多个 SpEL 字符串中重复常量(作用域、角色)...
  3. 或者为了防止常量重复,150个字符长的连接可读性差

一般来说 spring-security,安全配置库 classes (WebSecurityConfigurerAdapter, ResourceServerConfigurerAdapter) 提供的 HttpSecurity 构建器非常适合简单约束,但 composite/sophisticated 约束最终出现在 SpEL 中。

使用 spring-security-oauth2 除了 SpEL,我不知道任何其他方法。

问题:是否有任何现有实用程序 class 为 OAuth2 SpEL 提供某种类型安全的流畅构建器?

类似于:

TypicalPerfectlyNamedSpringFrameworkDreamClass.builder()
    .startExpressionGroup()
        .hasOAuth2Scope("MY-SCOPE")
    .endExpressionGroup()
    .or()
    .startExpressionGroup()
        .isNotOAuth2Request()
        .and()
        .hasRole("ROLE_USER")
    .endExpressionGroup()
    .toString();

简答:"no, there is no such utility"。