# 11.5.1 配置响应式 Web 安全

重提一下，配置 `Spring Security` 以确保 `Spring MVC` 的 web 应用程序的安全，通常涉及创建一个扩展 `WebSecurityConfigurerAdapter` 的新配置类，并添加 `@EnableWebSecurity` 注解。这样的配置类通过覆写 `configuration()` 方法，以指定 web 安全性规范，例如：哪些请求路径需要什么样的授权。以下简单的配置类，让您重温一下非响应式 `Spring MVC` 应用程序，是如何配置 `Spring Security` 的：

```java
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
         .authorizeRequests()
          .antMatchers("/design", "/orders").hasAuthority("USER")
           .antMatchers("/**").permitAll();
    }
}
```

现在，让我们看看对于响应式 `Spring WebFlux` 应用，同样的配置是什么样子。下面的清单显示了一个响应式安全配置类，这大致相当于上面的简单配置类。

{% code title="程序清单 11.2 为 Spring WebFlux 应用配置 Spring Security" %}

```java
@Configuration
@EnableWebFluxSecurity
public class SecurityConfig {
    @Bean
    public SecurityWebFilterChain securityWebFilterChain(
                                             ServerHttpSecurity http) {
        return http
            .authorizeExchange()
              .pathMatchers("/design", "/orders").hasAuthority("USER")
              .anyExchange().permitAll()
          .and()
            .build();
    }
}
```

{% endcode %}

正如你所见，有很多是熟悉的，但同时又有些不同。比如这个新的配置类没有使用 `@EnableWebSecurity` ，而是使用了 `@EnableWebFluxSecurity` 。此外，也没有继承像 `WebSecurityConfigurerAdapter` 的任何其他基类。因此，它也不用覆写任何 `configure()` 方法。

代替 `configure()` 方法，您可以使用 `securityWebFilterChain()` 方法声明 `SecurityWebFilterChain` 类型的 bean。而 `securityWebFilterChain()` 的方法体与前面配置的 `configure()` 方法没有太大区别，只有一些微小的变化。

首先，使用给定的 `ServerHttpSecurity` 对象进行配置，而不是 `HttpSecurity` 对象。使用 `ServerHttpSecurity` 声明请求级别安全性，可以调用 `authorizeExchange()`，这大致相当于 `authorizeRequests()`。

> 注意：`ServerHttpSecurity` 是 `Spring Security 5` 的中新加的功能，是 `HttpSecurity` 的一种响应式编程的模拟。

在匹配路径时，仍然可以使用 Ant 样式的通配符路径，但要使用 `pathMatchers()` 方法而不是 `antMatchers()`。为了方便起见，您不再需要指定 `/**` 来匹配所有路径，因为 `anyExchange()` 会捕获所有路径。

最后，因为您将 `SecurityWebFilterChain` 声明为 bean ，且没有覆写任何框架方法，所以必须调用 `build()` 方法，来组装所有安全规则以返回给 `SecurityWebFilterChain`。

除了这些小的区别，对于 `Spring WebFlux` 和 `Spring MVC` 的 web 安全配置基本一致。但如何指定有关用户的安全配置呢？


---

# 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-v5/di-11-zhang-kai-fa-xiang-ying-shi-api/11.5-bao-hu-xiang-ying-shi-web-api/11.5.1-pei-zhi-xiang-ying-shi-web-an-quan.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.
