首页 > 解决方案 > HttpSecurity permitAll 和 WebSecurity 忽略 un-Auth URL 的功能?

问题描述

这个问题可能看起来像重复,但以下答案均未解释何时使用:

`http
   .authorizeRequests()
   .antMatchers("/h2-console/**", "/user/register/**").permitAll()` 

`web
   .ignoring()
   .antMatchers("/h2-console/**", "/user/register/**")`
  1. HttpSecurity、WebSecurity 和 AuthenticationManagerBuilder
  2. Spring Security中Web忽略和Http允许之间的区别?

通过 StackOverflow asnwers 和几篇文章,我了解到:

configure(HttpSecurity)允许在资源级别配置基于 Web 的安全性。

configure(WebSecurity)用于影响全局安全的配置设置。使用此 URL 从 Spring Security Filter Chain 中完全忽略。

当我使用permitAll()它时,它只有在我禁用csrf 时才有效http.csrf().disable()因为 Spring Security 过滤器链仍然处于活动状态。

但与web.ignoring()URL 完全忽略。

还有很多文章http.permitAll()用于/login/register喜欢这个这个

所以我想明白,

为什么我们甚至应该使用和http.permitAll()之类的 Un-Auth URLS ?/login/register

为什么我们不能使用web.ignoring()for/login/register

为什么 web.ignoring()通常仅用于提供静态内容,例如csswebjars等,而不是/login/register

标签: javaspringspring-bootspring-mvcspring-security

解决方案


正如所@M. Deinum指出的,我正在总结答案,并且我已经更新了PR中相同的文档。

configure(WebSecurity web)

此方法中使用的端点忽略了 spring 安全过滤器、标头、CSRF 等。请参阅 HeadersConfigurer、CsrfConfigurer。相反,如果您想保护公共端点免受常见漏洞的影响,请参阅configure(HttpSecurity)配置 HttpSecurity#authorizeRequests方法。

configure(HttpSecurity http)

可以在此处指定需要防御常见漏洞的公共端点。有关详细信息,请参阅授权HttpSecurity#authorizeRequests规则 。permitAll()


推荐阅读