【问题标题】:Why do we need to call http.addFilterBefore() method in spring security configure(HttpSecurity http) method?为什么我们需要在spring security configure(HttpSecurity http)方法中调用http.addFilterBefore()方法?
【发布时间】:2020-03-18 14:55:45
【问题描述】:

我想了解为什么我们通常需要在初始 configure(HttpSecurity http) 方法中调用 http.addFilterBefore(jwtAuthenticationFilter(),UsernamePasswordAuthenticationFilter.class) 方法? 据我了解,它将首先添加 jwtAuthenticationFilter() 的结果,然后是 UsernamePasswordAuthenticationFilter 但我不知道为什么? jwtAuthenticationFilter() 实现:

@Override
protected  void configure(AuthenticationManagerBuilder authenticationManagerBuilder) throws Exception {
        authenticationManagerBuilder.userDetailsService(customerService).passwordEncoder(bCryptPasswordEncoder());
    }

难道扩展WebSecurityConfigurerAdapter的安全类只会在启动时调用一次?

【问题讨论】:

  • 您尝试使用addFilterAfter(jwtAuthenticationFilter(),UsernamePasswordAuthenticationFilter.class) 看看会发生什么吗?
  • 对于第二个问题,到达您应用程序的每个请求都将通过您的安全类和过滤器。
  • 我个人认为,在 UsernameAndPassword 身份验证过滤器之前调用 JWT 身份验证过滤器只是为了确保过滤器尊重 Spring Security 过滤器链顺序,并且它与 UsernameAndPassword 过滤器没有直接关系。重要的一点是在链中进一步调用之前一起调用身份验证处理机制。有关安全过滤器链的更多信息,请查看官方文档docs.spring.io/spring-security/site/docs/3.0.x/reference/…

标签: spring spring-boot spring-security jwt


【解决方案1】:
  1. 为什么我们通常需要调用

我们正在配置。不打电话!

  1. 难道扩展WebSecurityConfigurerAdapter的安全类只会在启动时调用一次?

是的,配置方法将在启动时执行(Run-Time-Polymorphism)以设置 HttpSecurity 或配置 spring 安全过滤器。


简单来说,Spring Security 是一个基于过滤器的框架。我们要么启用现有过滤器并对其进行配置添加我们的自定义过滤器。

  1. configure() 方法用于设置现有过滤器,设置后我们可以修改这些过滤器配置。如果您对配置的修改不能满足您的要求,那么您可以定义自己的自定义文件管理器。

  2. 要定义自定义过滤器,有以下三个*规定
    (实际上是4个addFilterAt(),很少使用)

 --------------------------------------------------------------------------------------
| java-config                      | xml-config                                        |
 --------------------------------------------------------------------------------------
| .addFilter()                     | <custom-filter  position="BASIC_AUTH_FILTER"/>    |
 --------------------------------------------------------------------------------------
| .addFilterBefore()               | <custom-filter  before="LAST" />                  |
 --------------------------------------------------------------------------------------
| .addFilterAfter()                | <custom-filter  after="FIRST" />                  |
 -------------------------------------------------------------------------------------- 
  1. 简单来说。
  • .addFilter() 您可以仅添加弹簧定义过滤器的实例,也可以添加这些弹簧安全定义过滤器的子类。 例如
    .addFilter(customAuthFilter, UsernamePasswordAuthenticationFilter.class)customAuthFilter 应该是UsernamePasswordAuthenticationFilter 子类的实例或UsernamePasswordAuthenticationFilter 的实例。

  • .addFilterAfter().addFilterBefore() 这里的过滤器可以是任何自定义过滤器。但是,自定义过滤器应该是GenericFilterBean 的实现。大多数情况下会使用OncePerRequestFilter的实现。

详细分析可以参考sequence of execution in spring security

【讨论】:

  • 为什么我们应该在UsernamePasswordAuthenticationFilter之前添加jwt过滤器而不是另一个?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-03
  • 1970-01-01
  • 2020-11-29
  • 1970-01-01
  • 2019-03-10
相关资源
最近更新 更多