对于最新的稳定版本,请使用 Spring Security 6.5.0spring-doc.cadn.net.cn

基于表达式的访问控制

概述

Spring Security 使用 SpEL 来支持表达式,如果您有兴趣更深入地了解该主题,则应了解其工作原理。 表达式使用“根对象”作为计算上下文的一部分进行计算。 Spring Security 使用特定的 Web 类和方法安全性作为根对象,以提供内置表达式和对值(例如当前主体)的访问。spring-doc.cadn.net.cn

常见的内置表达式

表达式根对象的基类是SecurityExpressionRoot. 这提供了一些在 Web 和方法安全性中都可用的常用表达式:spring-doc.cadn.net.cn

表 1.常见的内置表达式
表达 描述

hasRole(String role)spring-doc.cadn.net.cn

返回true如果当前主体具有指定的角色。spring-doc.cadn.net.cn

例:hasRole('admin')spring-doc.cadn.net.cn

默认情况下,如果提供的角色不以ROLE_,则添加它。 您可以通过修改defaultRolePrefixDefaultWebSecurityExpressionHandler.spring-doc.cadn.net.cn

hasAnyRole(String…​ roles)spring-doc.cadn.net.cn

返回true如果当前主体具有提供的任何角色(以逗号分隔的字符串列表形式提供)。spring-doc.cadn.net.cn

例:hasAnyRole('admin', 'user').spring-doc.cadn.net.cn

默认情况下,如果提供的角色不以ROLE_,则添加它。 您可以通过修改defaultRolePrefixDefaultWebSecurityExpressionHandler.spring-doc.cadn.net.cn

hasAuthority(String authority)spring-doc.cadn.net.cn

返回true如果当前主体具有指定的权限。spring-doc.cadn.net.cn

例:hasAuthority('read')spring-doc.cadn.net.cn

hasAnyAuthority(String…​ authorities)spring-doc.cadn.net.cn

返回true如果当前主体具有任何提供的授权(以逗号分隔的字符串列表形式提供)。spring-doc.cadn.net.cn

例:hasAnyAuthority('read', 'write').spring-doc.cadn.net.cn

principalspring-doc.cadn.net.cn

允许直接访问表示当前用户的主体对象。spring-doc.cadn.net.cn

authenticationspring-doc.cadn.net.cn

允许直接访问当前的Authentication对象从SecurityContext.spring-doc.cadn.net.cn

permitAllspring-doc.cadn.net.cn

Always 的计算结果为true.spring-doc.cadn.net.cn

denyAllspring-doc.cadn.net.cn

Always 的计算结果为false.spring-doc.cadn.net.cn

isAnonymous()spring-doc.cadn.net.cn

返回true如果当前主体是匿名用户。spring-doc.cadn.net.cn

isRememberMe()spring-doc.cadn.net.cn

返回true如果当前委托人是 Remember-Me 用户。spring-doc.cadn.net.cn

isAuthenticated()spring-doc.cadn.net.cn

返回true如果用户不是匿名的。spring-doc.cadn.net.cn

isFullyAuthenticated()spring-doc.cadn.net.cn

返回true如果用户不是匿名用户,也不是 Remember-Me 用户。spring-doc.cadn.net.cn

hasPermission(Object target, Object permission)spring-doc.cadn.net.cn

返回true如果用户有权访问为给定权限提供的目标。 例hasPermission(domainObject, 'read').spring-doc.cadn.net.cn

hasPermission(Object targetId, String targetType, Object permission)spring-doc.cadn.net.cn

返回true如果用户有权访问为给定权限提供的目标。 例hasPermission(1, 'com.example.domain.Message', 'read').spring-doc.cadn.net.cn

Web 安全表达式

要使用表达式来保护单个 URL,您首先需要将use-expressions属性中的<http>元素设置为true. 然后,Spring Security 需要access的属性<intercept-url>元素来包含 SPEL 表达式。 每个表达式的计算结果应为布尔值,定义是否应允许访问。 下面的清单显示了一个示例:spring-doc.cadn.net.cn

<http>
	<intercept-url pattern="/admin*"
		access="hasRole('admin') and hasIpAddress('192.168.1.0/24')"/>
	...
</http>

在这里,我们定义了admin区域(由 URL 模式定义)应仅对具有被授予权限 (admin) 及其 IP 地址与本地子网匹配。 我们已经看到了内置的hasRole表达式。 这hasIpAddressexpression 是特定于 Web 安全性的附加内置表达式。 它由WebSecurityExpressionRoot类,在计算 Web 访问表达式时,其实例用作表达式根对象。 这个对象还直接暴露了HttpServletRequest名称下的 objectrequest,以便您可以直接在表达式中调用请求。 如果正在使用表达式,则WebExpressionVoter已添加到AccessDecisionManager,该名称空间使用。 因此,如果您不使用命名空间并希望使用表达式,则必须将其中一个表达式添加到您的配置中。spring-doc.cadn.net.cn

在 Web 安全表达式中引用 Bean

如果你希望扩展可用的表达式,你可以很容易地引用你公开的任何 Spring Bean。 例如,您可以使用以下命令,假设您有一个名称为webSecurity,其中包含以下方法签名:spring-doc.cadn.net.cn

public class WebSecurity {
		public boolean check(Authentication authentication, HttpServletRequest request) {
				...
		}
}
class WebSecurity {
    fun check(authentication: Authentication?, request: HttpServletRequest?): Boolean {
        // ...
    }
}

然后,您可以按如下方式参考该方法:spring-doc.cadn.net.cn

参考方法
http
    .authorizeHttpRequests(authorize -> authorize
        .requestMatchers("/user/**").access(new WebExpressionAuthorizationManager("@webSecurity.check(authentication,request)"))
        ...
    )
<http>
	<intercept-url pattern="/user/**"
		access="@webSecurity.check(authentication,request)"/>
	...
</http>
http {
    authorizeRequests {
        authorize("/user/**", "@webSecurity.check(authentication,request)")
    }
}

Web 安全表达式中的路径变量

有时,能够在 URL 中引用路径变量是件好事。 例如,假设有一个 RESTful 应用程序,它从 URL 路径中按 ID 查找用户,格式为/user/{userId}.spring-doc.cadn.net.cn

您可以通过将 path 变量放置在 pattern 中来轻松引用它。 例如,如果您有一个名称为webSecurity,其中包含以下方法签名:spring-doc.cadn.net.cn

public class WebSecurity {
		public boolean checkUserId(Authentication authentication, int id) {
				...
		}
}
class WebSecurity {
    fun checkUserId(authentication: Authentication?, id: Int): Boolean {
        // ...
    }
}

然后,您可以按如下方式参考该方法:spring-doc.cadn.net.cn

路径变量
http
	.authorizeHttpRequests(authorize -> authorize
		.requestMatchers("/user/{userId}/**").access(new WebExpressionAuthorizationManager("@webSecurity.checkUserId(authentication,#userId)"))
		...
	);
<http>
	<intercept-url pattern="/user/{userId}/**"
		access="@webSecurity.checkUserId(authentication,#userId)"/>
	...
</http>
http {
    authorizeRequests {
        authorize("/user/{userId}/**", "@webSecurity.checkUserId(authentication,#userId)")
    }
}

在此配置中,匹配的 URL 会将 path 变量传入(并将其转换为)到checkUserId方法。 例如,如果 URL 是/user/123/resource,则传入的 ID 将为123.spring-doc.cadn.net.cn

方法安全表达式

方法安全性比简单的允许或拒绝规则要复杂一些。 Spring Security 3.0 引入了一些新的 Comments,以允许全面支持表达式的使用。spring-doc.cadn.net.cn

@Pre 和 @Post 注释

有四个注释支持表达式属性,以允许调用前和调用后授权检查,还支持筛选提交的集合参数或返回值。 他们是@PreAuthorize,@PreFilter,@PostAuthorize@PostFilter. 它们的使用是通过global-method-securitynamespace 元素:spring-doc.cadn.net.cn

<global-method-security pre-post-annotations="enabled"/>

使用 @PreAuthorize 和 @PostAuthorize 进行访问控制

最明显有用的注释是@PreAuthorize,它决定方法是否真的可以调用。 以下示例(来自 “Contacts” 示例应用程序)使用@PreAuthorize注解:spring-doc.cadn.net.cn

@PreAuthorize("hasRole('USER')")
public void create(Contact contact);
@PreAuthorize("hasRole('USER')")
fun create(contact: Contact?)

这意味着仅允许具有ROLE_USER角色。 显然,通过使用 required 角色的传统配置和简单的配置属性,可以很容易地实现相同的目的。 但是,请考虑以下示例:spring-doc.cadn.net.cn

@PreAuthorize("hasPermission(#contact, 'admin')")
public void deletePermission(Contact contact, Sid recipient, Permission permission);
@PreAuthorize("hasPermission(#contact, 'admin')")
fun deletePermission(contact: Contact?, recipient: Sid?, permission: Permission?)

在这里,我们实际上使用方法参数作为表达式的一部分来决定当前用户是否具有admin给定联系人的权限。 内置的hasPermission()expression通过应用程序上下文链接到 Spring Security ACL 模块,正如我们在本节后面看到的那样。 您可以按名称作为表达式变量访问任何方法参数。spring-doc.cadn.net.cn

Spring Security 可以通过多种方式解析方法参数。 Spring Security 使用DefaultSecurityParameterNameDiscoverer以发现参数名称。 默认情况下,对方法尝试以下选项。spring-doc.cadn.net.cn

  • 如果 Spring Security 的@Pannotation 存在于方法的单个参数上,则使用该值。 这对于使用 JDK 8 之前的 JDK 编译的接口(不包含有关参数名称的任何信息)非常有用。 以下示例使用@P注解:spring-doc.cadn.net.cn

    import org.springframework.security.access.method.P;
    
    ...
    
    @PreAuthorize("#c.name == authentication.name")
    public void doSomething(@P("c") Contact contact);
    import org.springframework.security.access.method.P
    
    ...
    
    @PreAuthorize("#c.name == authentication.name")
    fun doSomething(@P("c") contact: Contact?)

    在幕后,这是通过使用AnnotationParameterNameDiscoverer,您可以自定义该属性以支持任何指定注释的 value 属性。spring-doc.cadn.net.cn

  • 如果 Spring Data 的@Paramannotation 的 Comments 存在于该方法的至少一个参数上,则使用该值。 这对于使用 JDK 8 之前的 JDK 编译的接口非常有用,这些接口不包含有关参数名称的任何信息。 以下示例使用@Param注解:spring-doc.cadn.net.cn

    import org.springframework.data.repository.query.Param;
    
    ...
    
    @PreAuthorize("#n == authentication.name")
    Contact findContactByName(@Param("n") String name);
    import org.springframework.data.repository.query.Param
    
    ...
    
    @PreAuthorize("#n == authentication.name")
    fun findContactByName(@Param("n") name: String?): Contact?

    在幕后,这是通过使用AnnotationParameterNameDiscoverer,您可以自定义该属性以支持任何指定注释的 value 属性。spring-doc.cadn.net.cn

  • 如果使用 JDK 8 编译源代码,并且-parameters参数和 Spring 4+ 时,使用标准的 JDK 反射 API 来发现参数名称。 这适用于类和接口。spring-doc.cadn.net.cn

  • 最后,如果代码是使用调试符号编译的,则使用调试符号发现参数名称。 这不适用于接口,因为它们没有有关参数名称的调试信息。 对于接口,必须使用 Comments 或 JDK 8 方法。spring-doc.cadn.net.cn

表达式中提供了任何 SPEL 功能,因此您还可以访问参数上的属性。 例如,如果您希望特定方法仅允许其用户名与联系人的用户名匹配的用户访问,则可以编写spring-doc.cadn.net.cn

@PreAuthorize("#contact.name == authentication.name")
public void doSomething(Contact contact);
@PreAuthorize("#contact.name == authentication.name")
fun doSomething(contact: Contact?)

在这里,我们访问另一个内置表达式authentication,即Authentication存储在安全上下文中。 您还可以访问其principal属性,通过使用principal表达。 该值通常为UserDetails实例,因此您可以使用诸如principal.usernameprincipal.enabled.spring-doc.cadn.net.cn

使用 @PreFilter 和 @PostFilter 进行筛选

Spring Security 支持使用表达式过滤集合、数组、映射和流。 这通常对方法的返回值执行。 以下示例使用@PostFilter:spring-doc.cadn.net.cn

@PreAuthorize("hasRole('USER')")
@PostFilter("hasPermission(filterObject, 'read') or hasPermission(filterObject, 'admin')")
public List<Contact> getAll();
@PreAuthorize("hasRole('USER')")
@PostFilter("hasPermission(filterObject, 'read') or hasPermission(filterObject, 'admin')")
fun getAll(): List<Contact?>

使用@PostFilter注释时,Spring Security 会遍历返回的集合或 map 并删除提供的表达式为 false 的任何元素。 对于数组,将返回包含筛选元素的新数组实例。filterObject引用集合中的当前对象。 当使用 map 时,它引用当前的Map.Entry对象,它允许您使用filterObject.keyfilterObject.value在表达式中。 您还可以在方法调用之前使用@PreFilter,尽管这是一个不太常见的要求。 语法是相同的。但是,如果有多个参数是集合类型,则必须使用filterTarget属性。spring-doc.cadn.net.cn

请注意,筛选显然不能替代优化数据检索查询。 如果要筛选大型集合并删除许多条目,则效率可能较低。spring-doc.cadn.net.cn

内置表达式

有一些特定于方法安全性的内置表达式,我们之前已经看到过这些表达式的使用。 这filterTargetreturnValue值很简单,但是使用hasPermission()表达值得仔细观察。spring-doc.cadn.net.cn

PermissionEvaluator 接口

hasPermission()表达式被委托给PermissionEvaluator. 它旨在桥接表达式系统和 Spring Security 的 ACL 系统,让您根据抽象权限指定对域对象的授权约束。 它对 ACL 模块没有明确的依赖关系,因此如果需要,您可以将其换成替代实现。 该接口有两种方法:spring-doc.cadn.net.cn

boolean hasPermission(Authentication authentication, Object targetDomainObject,
							Object permission);

boolean hasPermission(Authentication authentication, Serializable targetId,
							String targetType, Object permission);

这些方法直接映射到表达式的可用版本,但第一个参数(Authenticationobject) 的 第一种用于已加载 domain 对象(其访问权限被控制)的情况。 然后表达式返回true当前用户是否具有该对象的给定权限。 第二个版本用于对象未加载但其标识符已知的情况。 还需要域对象的抽象 “type” 说明符,以便加载正确的 ACL 权限。 传统上,这是对象的 Java 类,但只要它与权限的加载方式一致,就不必如此。spring-doc.cadn.net.cn

要使用hasPermission()表达式中,您必须显式配置PermissionEvaluator在您的应用程序上下文中。 以下示例显示了如何执行此作:spring-doc.cadn.net.cn

<security:global-method-security pre-post-annotations="enabled">
<security:expression-handler ref="expressionHandler"/>
</security:global-method-security>

<bean id="expressionHandler" class=
"org.springframework.security.access.expression.method.DefaultMethodSecurityExpressionHandler">
	<property name="permissionEvaluator" ref="myPermissionEvaluator"/>
</bean>

哪里myPermissionEvaluator是实现PermissionEvaluator. 通常,这是 ACL 模块的实现,称为AclPermissionEvaluator. 请参阅Contacts示例应用程序配置,了解更多详细信息。spring-doc.cadn.net.cn

方法安全元注释

您可以利用元注释来实现方法安全性,以使您的代码更具可读性。 如果您发现在整个代码库中重复相同的复杂表达式,这将特别方便。 例如,请考虑以下情况:spring-doc.cadn.net.cn

@PreAuthorize("#contact.name == authentication.name")

你可以创建一个 meta 注解,而不是到处重复此作:spring-doc.cadn.net.cn

@Retention(RetentionPolicy.RUNTIME)
@PreAuthorize("#contact.name == authentication.name")
public @interface ContactPermission {}
@Retention(AnnotationRetention.RUNTIME)
@PreAuthorize("#contact.name == authentication.name")
annotation class ContactPermission

你可以将元 Comments 用于任何 Spring Security 方法安全 Comments。 为了保持符合规范,JSR-250 注释不支持元注释。spring-doc.cadn.net.cn