> For the complete documentation index, see [llms.txt](https://potoyang.gitbook.io/spring-in-action-v4/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://potoyang.gitbook.io/spring-in-action-v4/untitled-8/untitled-1.md).

# 14.2　使用表达式实现方法级别的安全性

尽管 @Secured 和 @RolesAllowed 注解在拒绝未认证用户方面表现不错，但这也是它们所能做到的所有事情了。有时候，安全性约束不仅仅涉及用户是否有权限。

Spring Security 3.0 引入了几个新注解，它们使用 SpEL 能够在方法调用上实现更有意思的安全性约束。这些新的注解在表 14.1 中进行了描述。

这些注解的值参数中都可以接受一个 SpEL 表达式。表达式可以是任意合法的 SpEL 表达式，可能会包含表 9.5 所列的 Spring Security 对 SpEL 的扩展。如果表达式的计算结果为 true，那么安全规则通过，否则就会失败。安全规则通过或失败的结果会因为所使用注解的差异而有所不同。

| 注解             | 描述                                   |
| -------------- | ------------------------------------ |
| @PreAuthorize  | 在方法调用之前，基于表达式的计算结果来限制对方法的访问          |
| @PostAuthorize | 允许方法调用，但是如果表达式计算结果为false，将抛出一个安 全性异常 |
| @PostFilter    | 允许方法调用，但必须按照表达式来过滤方法的结果              |
| @PreFilter     | 允许方法调用，但必须在进入方法之前过滤输入值               |

稍后，我们将会看到每个注解的例子。但首先，我们需要将 @EnableGlobalMethodSecurity 注解的 prePostEnabled 属性设置为 true，从而启用它们：

```java
@Configuration
public class MethodSecurityConfig extends GlobalMethodSecurityConfiguration {
}
```

现在，方法调用前后的注解都已经启用了，我们可以使用它们了。我们首先看一下如何使用 @PreAuthorize 和 @PostAuthorize 注解限制对方法的调用。
