首页 > Java > java教程 > 正文

Spring Security 6 中的新 requestMatchers

王林
发布: 2024-07-15 12:10:34
转载
1156人浏览过

spring security 6 中的新 requestmatchers

spring security 6 中,requestmatchers 方法取代了已弃用的 antmatchers、mvcmatchers 和 regexmatchers 方法,用于配置基于路径的访问控制。以下是关于新 requestmatchers 的要点:

在authorizehttprequests中使用requestmatchers

httpsecurity配置中的authorizehttprequests方法允许您配置细粒度的请求匹配以进行访问控制。您可以使用 requestmatchers 方法来指定应允许或验证哪些请求。例如:

@bean
public securityfilterchain securityfilterchain(httpsecurity http) throws exception {
    return http.authorizehttprequests(auth -> auth
        .requestmatchers("/greet").permitall()
        .anyrequest().authenticated())
        .formlogin()
        .build();
}
登录后复制

此配置允许无需身份验证即可访问 /greet 端点,同时需要对所有其他请求进行身份验证。

requestmatchers 与 securitymatchers

类似的方法还有两个:requestmatchers 和 securitymatchers。两者都根据类路径中 spring mvc 的存在来选择最合适的 requestmatcher 实现:

  • 如果 spring mvc 存在,它使用 mvcrequestmatcher
  • 如果 spring mvc 不存在,它将回退到 antpathrequestmatcher

主要区别在于 securitymatchers 用于 websecuritycustomizer 等地方,而 requestmatchers 用于authorizehttprequests。

百度文心百中
百度文心百中

百度大模型语义搜索体验中心

百度文心百中 22
查看详情 百度文心百中

选择正确的匹配器

requestmatchers 方法允许您根据模式或其他条件匹配请求,而无需依赖特定的匹配器(如 antpathrequestmatcher 或 regexrequestmatcher)。这提供了更大的灵活性和更好的默认值。

要使用特定的匹配器,您可以将 requestmatcher 实现传递给 requestmatchers 方法:

@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
    return http.authorizeHttpRequests(auth -> auth
        .requestMatchers(new AntPathRequestMatcher("/greet")).permitAll()
        .anyRequest().authenticated())
        .formLogin()
        .build();
}
登录后复制

总而言之,spring security 6 中新的 requestmatchers 方法提供了一种更灵活、更安全的方式来配置基于路径的访问控制,根据应用程序的依赖关系选择最合适的 requestmatcher 实现。

以上就是Spring Security 6 中的新 requestMatchers的详细内容,更多请关注php中文网其它相关文章!

最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:dev.to网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号