Spring Security 授权 URL

基于 HttpSecurity 配置

package cloud.xuxiaowei.passport.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.web.SecurityFilterChain;

/**
 * @author xuxiaowei
 * @since 0.0.1
 */
@Configuration
public class ResourceServerConfig {

    @Bean
    public SecurityFilterChain defaultSecurityFilterChain(HttpSecurity http) throws Exception {

        http.authorizeRequests(customizer -> {

            // 这里可以根据 下方表格 配置
            // 这里可以根据 下方表格 配置
            // 这里可以根据 下方表格 配置

            // 其他地址:需要授权访问
            // 如果需要此配置,必须要放在最后一行,否则启动项目报错
            customizer.anyRequest().authenticated();

        });

        return http.build();
    }

}
  1. 注意:POSTPUTPATCHDELETE 可能会被 CSRF 拦截而响应 401,
    请参考:CSRF 跨站请求伪造
  2. mvcMatchers 基于 org.springframework.security.web.servlet.util.matcher.MvcRequestMatcher 实现,方法支持正则表达式,可以填写多个路径
  3. regexMatchers 基于 org.springframework.security.web.util.matcher.RegexRequestMatcher 实现,方法支持正则表达式,可以填写多个路径
  4. antMatchers 基于 org.springframework.security.web.util.matcher.AntPathRequestMatcher 实现,方法支持正则表达式,可以填写多个路径
  5. requestMatchers 无论是使用字符串还是接口 RequestMatcher 的实现,都支持填写多个值(参数为数组)
    1. Spring Boot 2 不支持使用字符串,仅支持使用 RequestMatcher 的实现
    2. Spring Boot 3 支持使用 字符串 和 RequestMatcher 的实现
使用方法:仅限 Spring Boot 2 作用
customizer.mvcMatchers("/a1").permitAll(); /a1 路径允许所有人访问,基于 MvcRequestMatcher 实现
customizer.mvcMatchers(HttpMethod.GET, "/a2").permitAll(); /a2 仅允许使用 GET 请求 匿名访问
customizer.mvcMatchers("/a3").hasAuthority("A1"); /a3 拥有 A1 权限的用户才能访问
customizer.mvcMatchers("/a4").hasAnyAuthority("A1", "A2"); /a4 拥有 A1 A2 权限的用户才能访问
customizer.mvcMatchers("/a5").hasRole("R1"); /a5 拥有 R1 角色的用户才能访问
customizer.mvcMatchers("/a6").hasAnyRole("R1", "R2"); /a6 拥有 R1 R2 角色的用户才能访问
customizer.mvcMatchers("/a7").hasIpAddress("192.168.0.0/16"); /a7192.168.0.0/16 发送请求可以匿名访问
customizer.mvcMatchers("/a8").rememberMe(); /a8 使用 记住我 功能登陆的用户可以访问
customizer.mvcMatchers("/a9").fullyAuthenticated(); /a9 必须完整验证身份后才能访问,使用 记住我 功能登陆的用户无法访问
customizer.mvcMatchers("/a10").denyAll(); /a10 禁止访问
customizer.mvcMatchers("/a11").not().hasAuthority("A1"); /a11 取反,拥有 A1 权限才能访问
customizer.mvcMatchers("/a12").access("hasAuthority('A1')"); /a12 与上方 /a3 结果一致
customizer.mvcMatchers("/a13").access("hasAuthority('A1') and hasAuthority('A2')"); /a13 同时拥有 A1 A2 权限才能访问
customizer.mvcMatchers("/a14").access("!hasAuthority('A1')"); /a14 取反,拥有 A1 权限才能访问
customizer.regexMatchers("/a21").permitAll(); /a21mvcMatchers 方法雷同,使用 RegexRequestMatcher 实现
customizer.antMatchers("/a31").permitAll(); /a31mvcMatchers 方法雷同,使用 AntPathRequestMatcher 实现
使用方法:仅限 Spring Boot 3 作用
customizer.requestMatchers("/a1").permitAll(); /a1 路径允许所有人访问,基于 MvcRequestMatcher 实现
customizer.requestMatchers(HttpMethod.GET, "/a2").permitAll(); /a2 仅允许使用 GET 请求 匿名访问
customizer.requestMatchers("/a3").hasAuthority("A1"); /a3 拥有 A1 权限的用户才能访问
customizer.requestMatchers("/a4").hasAnyAuthority("A1", "A2"); /a4 拥有 A1 A2 权限的用户才能访问
customizer.requestMatchers("/a5").hasRole("R1"); /a5 拥有 R1 角色的用户才能访问
customizer.requestMatchers("/a6").hasAnyRole("R1", "R2"); /a6 拥有 R1 R2 角色的用户才能访问
customizer.requestMatchers("/a8").rememberMe(); /a8 使用 记住我 功能登陆的用户可以访问
customizer.requestMatchers("/a9").fullyAuthenticated(); /a9 必须完整验证身份后才能访问,使用 记住我 功能登陆的用户无法访问
customizer.requestMatchers("/a10").denyAll(); /a10 禁止访问
使用方法:同时支持 Spring Boot 2、3 作用
customizer.requestMatchers(request -> { /* 这里可以使用 request 进行判断*/ return true;}).permitAll(); 可以根据 HttpServletRequest 进行自定义判断,最后的 permitAll 也可修改为 hasRole 等等

基于 @PreAuthorize 配置

参见:Spring Security 使用 @PreAuthorize 注解