# 第 14 章　保护方法应用

本章内容：

* 保护方法调用
* 使用表达式定义安全规则
* 创建安全表达式计算器

在离家或上床睡觉之前，我做的最后一件事就是确保房间的门已经关好。但是在此之前，我会设置好警报。为什么呢？这是因为，尽管门锁是保证安全的一个好办法，但是警报系统提供了第二层防护，窃贼 有可能会越过门锁的保护。

在第 9 章中，我们看到了如何使用 Spring Security 保护应用的 Web 层。Web 安全是非常重要的，它能阻止用户访问没有权限的内容。但是，如果应用的 Web 层出现安全漏洞会怎样呢？如果用户能够请求他们不允许访问的内容会怎样呢？

尽管我们没有理由认为用户能够攻破应用的安全层，但是在 Web 层出现安全漏洞实在是太容易了。例如，假设用户请求了一个允许访问的页面，但是由于开发人员不认真，处理这个请求的控制器方法返回了该用户不允许看到的数据。这是一个无心之失，不过，安全问题很可 能就是无心之失所造成的，因为他们是非常聪明的攻击者。

我们可以同时保护应用的 Web 层以及场景后面的方法，这样就能保证如果用户不具备权限的话，就无法执行相应的逻辑。

在本章中，我们将会看到如何使用 Spring Security 保护 bean 方法。通过这种方式，就能声明安全规则，保证如果用户没有执行方法的权限，就不会执行相应的方法。首先，我们会看一些可以放在方法上的简单注解，它们能够将方法锁定，阻止无权限用户的访问。


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://potoyang.gitbook.io/spring-in-action-v4/untitled-8.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
