【发布时间】:2015-06-13 13:40:32
【问题描述】:
我们正在使用 spring-security + spring-security-oauth 实现 OAuth2 资源服务器。
用例非常简单:
- A.部分资源仅供授权客户使用
- 乙。某些资源仅适用于(HTTP 基本)经过身份验证的用户
- C.所有其他资源仅适用于(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();
}
虽然这很好用,但我不喜欢:
- SpEL 标记本身可能很棘手,直到运行时才能验证
- 在多个 SpEL 字符串中重复常量(范围、角色)...
- 或者为了防止重复的常量,150 个字符长的连接可读性很差
通常对于 spring-security,安全配置基类(WebSecurityConfigurerAdapter、ResourceServerConfigurerAdapter)提供的 HttpSecurity 构建器非常适合简单的约束,但复合/复杂的约束最终会出现在 SpEL 中。
使用 spring-security-oauth2,除了 SpEL,我不知道任何其他方式。
问题:是否有任何现有的实用程序类为 OAuth2 SpEL 提供某种类型安全的流利构建器?
类似:
TypicalPerfectlyNamedSpringFrameworkDreamClass.builder()
.startExpressionGroup()
.hasOAuth2Scope("MY-SCOPE")
.endExpressionGroup()
.or()
.startExpressionGroup()
.isNotOAuth2Request()
.and()
.hasRole("ROLE_USER")
.endExpressionGroup()
.toString();
【问题讨论】:
标签: java spring spring-security spring-security-oauth2