Spring Security OAuth类型安全的SpEL构建器

时间:2015-04-08 14:19:40

标签: java spring spring-security spring-security-oauth2

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

用例非常简单:

  • 甲。某些资源仅适用于授权客户
  • B中。某些资源仅适用于(HTTP基本)经过身份验证的用户
  • ℃。所有其他资源仅适用于(HTTP基本)经过身份验证的用户

使用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个字符长的连接可读性差
  4. 通常使用spring-security,安全配置基类(HttpSecurityWebSecurityConfigurerAdapter)提供的ResourceServerConfigurerAdapter构建器非常适合简单约束,但复合/复杂约束最终会出现在使用SpEL。

    使用spring-security-oauth2我不知道除了SpEL以外的任何其他方式。

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

    类似的东西:

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

1 个答案:

答案 0 :(得分:1)

简短回答:"不,没有这样的效用"。