> 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-2.md).

# 14.1　使用注解保护方法

在 Spring Security 中实现方法级安全性的最常见办法是使用特定的注解，将这些注解应用到需要保护的方法上。这样有几个好处，最重要的是当我们在编辑器中查看给定的方法时，能够很清楚地看到它的安全规则。

Spring Security 提供了三种不同的安全注解：

* Spring Security 自带的 @Secured 注解；
* JSR-250 的 @RolesAllowed 注解；
* 表达式驱动的注解，包括 @PreAuthorize、@PostAuthorize、@PreFilter 和 @PostFilter。

@Secured 和 @RolesAllowed 方案非常类似，能够基于用户所授予的权限限制对方法的访问。当我们需要在方法上定义更灵活的安全规则时，Spring Security 提供了 @PreAuthorize 和 @PostAuthorize，而 @PreFilter/@PostFilter 能够过滤方法返回的以及传入方法的集合。

在本章中，你将会看到如何使用这些注解。作为开始，我们首先看一下 @Secured 注解，这是 Spring Security 所提供的方法级安全注解里面最简单的一个。
