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

时间:2017-06-29 07:02:35

标签: spring-boot spring-security dsl

我经历了编写DSL来为我的自定义身份验证机制配置HttpSecurity的麻烦,但是我应用的大多数配置在应用程序运行时似乎没有生效,而所有内容都是如此当我在webapp中手动配置它时,它可以很好地工作。

首先,手动配置会导致我的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配置丢失。知道为什么吗?如果我不得不猜测,那就是我在指定路径的方式上做错了,但我无法理解。

1 个答案:

答案 0 :(得分:0)

我遇到了类似的问题。我通过将 entryPoint 移动到 init 来解决它