Java Spring Security OAuth类型安全SpEL builder

Java Spring Security OAuth类型安全SpEL builder,java,spring,spring-security,spring-security-oauth2,Java,Spring,Spring Security,Spring Security Oauth2,我们正在使用spring security+spring security oauth实现OAuth2资源服务器 用例非常简单: A.某些资源仅可用于授权客户 B.某些资源仅可用于(HTTP basic)经过身份验证的用户 C.所有其他资源仅可用于(HTTP basic)经过身份验证的用户 在Java配置中,为了实施安全约束,我们使用了很多SpEL,如下所示: @Override public void configure(HttpSecurity http) throws Exceptio

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

用例非常简单:

  • A.某些资源仅可用于授权客户
  • B.某些资源仅可用于(HTTP basic)经过身份验证的用户
  • C.所有其他资源仅可用于(HTTP basic)经过身份验证的用户
在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();
}
虽然这很好,但我不喜欢:

  • SpEL标记本身可能会变得棘手,在运行时之前无法验证
  • 在多个SpEL字符串中重复常量(作用域、角色)
  • 或者,为了防止重复常量,150个字符长的串联具有较差的可读性
  • 一般来说,对于spring安全性,安全配置基类(
    WebSecurityConfigureAdapter
    ResourceServerConfigurerAdapter
    )提供的
    HttpSecurity
    生成器非常适合于简单约束,但复合/复杂约束最终会出现在SpEL中

    使用spring-security-oauth2,除了SpEL,我不知道还有什么其他方法

    问题:是否存在为OAuth2 SpEL提供某种类型安全的fluent builder的实用程序类

    比如:

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

    简短回答:“不,没有这样的实用程序。”

    简短回答:“不,没有这样的实用程序。”

    谢谢,可能在那个问题上绕得太多了谢谢,可能在那个问题上绕得太多了