Spring Security 中的HttpSecurity 與 WebSecurity

Spring Security
Remote
1
09:43 AM · Nov 30 ,2025

1. 概述

Spring Security 框架提供 WebSecurityHttpSecurity 類,以提供全局和資源特定機制,限制對 API 和資產的訪問。 WebSecurity 類有助於在全局級別配置安全,而 HttpSecurity 提供了用於為特定資源配置安全性的方法。

在本教程中,我們將詳細研究 HttpSecurityWebSecurity 的關鍵用法。 此外,我們還將看到這兩種類的區別。

2. HttpSecurity

The HttpSecurity 類有助於配置特定 HTTP 請求的安全性。

此外,它允許使用 requestMatcher() 方法來限制安全配置到特定的 HTTP 端點。

更重要的是,它提供了配置特定 HTTP 請求的授權靈活性。我們可以使用 hasRole() 方法創建基於角色的身份驗證。

以下是一個使用 HttpSecurity 類來限制對“/admin/**”的訪問的示例代碼:

@Bean
SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
    http.authorizeHttpRequests((authorize) -> authorize.requestMatchers("/admin/**")
      .authenticated()
      .anyRequest()
      .permitAll())
      .formLogin(withDefaults());
    return http.build();
}

在上面的代碼中,我們使用 HttpSecurity 類來限制對“/admin/**”端點的訪問。任何到該端點的請求都需要在訪問之前進行身份驗證。

此外,HttpSecurity 提供了用於配置受限端點授權的方法。讓我們修改我們的示例代碼,以僅允許具有管理員角色的用户訪問“/admin/**”端點:

// ...
http.authorizeHttpRequests((authorize) -> authorize.requestMatchers("/admin/**").hasRole("ADMIN")
// ...

在這裏,我們為請求添加了更多的安全層,允許用户僅通過具有“ADMIN”角色訪問該端點。

此外,HttpSecurity 類還用於配置 Spring Security 中的 CORS 和 CSRF 保護。

3. WebSecurity

The WebSecurity

The WebSecurity

The WebSecurity

3.1. The ignoring()

Additionally, the WebSecurity

Here’s an example that uses the ignoring()

@Bean
WebSecurityCustomizer ignoringCustomizer() {
    return (web) -> web.ignoring().requestMatchers("/resources/**", "/static/**");
}

Here, we use the ignoring()

Notably, Spring advises that the ignoring()

However, dynamic requests need to pass through authentication and authorization to provide different access rules because they carry sensitive data. Also, if we ignore dynamic endpoints completely, we lose total security control. This could open an application for different attacks like CSRF attacks or SQL injection.

3.2. The debug()

Additionally, the debug()

Let’s see an example code that uses the debug()

@Bean
WebSecurityCustomizer debugSecurity() {
    return (web) -> web.debug(true);
}

Here, we invoke debug()

We create a StrictHttpFirewall

Finally, we expose a WebSecurityCustomizer

4. 關鍵差異

與其相互衝突,HttpSecurityWebSecurity 配置可以協同工作,提供全局和資源特定安全規則。

但是,如果同時配置了類似的安全性規則,WebSecurity 配置具有更高的優先級:

@Bean
WebSecurityCustomizer ignoringCustomizer() {
    return (web) -> web.ignoring().antMatchers("/admin/**");
}

// ...
 http.authorizeHttpRequests((authorize) -> authorize.antMatchers("/admin/**").hasRole("ADMIN")
// ...

在這裏,我們全局忽略 “/admin/**” 路徑在 WebSecurity 配置中,但我們也配置了 “/admin/**” 路徑上的訪問規則在 HttpSecurity 中。

在這種情況下,WebSecurity 的忽略配置將覆蓋 HttpSecurity 的授權規則對於 “/admin/**” 路徑。

此外,在 SecurityFilterChain 中,WebSecurity 配置在構建過濾器鏈時首先執行。HttpSecurity 規則隨後進行評估。

以下表格顯示了 HttpSecurityWebSecurity 類之間的關鍵差異:

特徵 WebSecurity HttpSecurity
範圍 全局默認安全規則 資源特定安全規則
示例 防火牆配置、路徑忽略、調試模式 URL 規則、授權、CORS、CSRF
配置方法 按資源條件配置 全局可重用安全配置

 

5. 結論

在本文中,我們學習了 HttpSecurityWebSecurity 的關鍵用法,並提供了示例代碼。 此外,我們還看到了 HttpSecurity 允許為特定資源配置安全規則,而 WebSecurity 則設置全局默認規則。

將它們結合使用,可以在全局和資源級別靈活地安全 Spring 應用程序。

user avatar
0 位用戶收藏了這個故事!
收藏

發佈 評論

Some HTML is okay.