HttpSecurity详解
HttpSecurity
是 Spring Security 中用于配置 HTTP 安全性的核心类。它允许你定义各种安全规则和过滤器,以保护 Web 应用程序中的不同 URL 和请求。下面是对 HttpSecurity
中常见配置的详细解析,以及每个配置的意义。
1. csrf
配置:
http.csrf(customizers -> customizers.disable());
意义:
- CSRF(Cross-Site Request Forgery):跨站请求伪造攻击。
.disable()
:禁用 CSRF 保护。在前后端分离的应用中,通常使用 JWT 或其他机制进行身份验证,因此可以禁用 CSRF 保护。
2. authorizeHttpRequests
配置:
http.authorizeHttpRequests((authorize) -> authorize.requestMatchers("/**.js","/**.css","/**.ico","/**.jpg","/**.png","/**.gif"