为什么通过 DSL 的 HttpSecurity 配置似乎与显式配置的工作方式不同?

2024-02-10

我费了很大的劲才写了一个 DSL 来配置HttpSecurity对于我的自定义身份验证机制,但我应用于它的大部分配置在应用程序运行时似乎并未生效,而当我在 web 应用程序中手动配置时,一切都运行良好。

首先,手动配置,这导致我的EntryPoint射击,authenticationProvider被查询,过滤器被添加到链中,我的rememberMeServices被添加到该过滤器中。一切都正确。

public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
  @Override
  protected void configure(HttpSecurity http) throws Exception {
    http
        .authorizeRequests()
          .antMatchers("/auth/callback").permitAll()
          .anyRequest().authenticated()
          .and()
        .authenticationProvider(authProvider)
        .rememberMe()
          .rememberMeServices(rememberMeServices)
          .and()
        .exceptionHandling()
          .authenticationEntryPoint(entryPoint)
          .and()
        .addFilterAfter(filter, UsernamePasswordAuthenticationFilter.class);
    /* The following code is basically what gets run when the DSL is in use
    http
        .apply(new EPIdentityDsl())
          // lots of setters called here, removed for clarity
          .and()
        .authorizeRequests().anyRequest().authenticated();
    */
  }

}

然而DSL中的代码是这样的,使用时,authenticationEntryPoint从不火灾。这rememberMeServices确实已配置,并且看起来过滤器已正确添加到链中,但我只是收到 403 响应的错误页面,而不是看到entryPoint重定向。

public class EPIdentityDsl extends AbstractHttpConfigurer<EPIdentityDsl, HttpSecurity> {
  @Override
  public void init(HttpSecurity http) throws Exception {
    // any method that adds/removes another configurer
    // must be done in the init method
    log.debug("dsl init");
    http
        .exceptionHandling()
          .and()
        .rememberMe();
  }

  @Override
  public void configure(HttpSecurity http) throws Exception {
    http.authorizeRequests()
          .antMatchers(filterProcessesUrl).permitAll()
          .and()
        .authenticationProvider(authProvider)
        .exceptionHandling()
          .authenticationEntryPoint(entryPoint)
          .and()
        .rememberMe()
          .rememberMeServices(rememberMeServices)
          .and()
        .addFilterAfter(filter, UsernamePasswordAuthenticationFilter.class);

  }
}

显然,我在文档或其他内容中遗漏了一些微妙的交互,导致我的基于 DSL 的配置entryPoint迷路。知道为什么吗?如果我不得不猜测,那就是我指定路径的方式出了问题,但我无法弄清楚。


我有类似的问题。我通过将入口点移至 init 解决了这个问题

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

为什么通过 DSL 的 HttpSecurity 配置似乎与显式配置的工作方式不同? 的相关文章

随机推荐