Why does HttpSecurity configuration via DSL not seem to work the same as explicit configuration?

一曲冷凌霜 提交于 2019-12-13 15:01:11

问题


I went through the trouble to write a DSL to configure the HttpSecurity for my custom authentication mechanism, but most of the configuration I apply to it doesn't seem to be in effect when the application runs, while everything works perfectly when I configure it all manually in the webapp.

First, the manual configuration, which results in my EntryPoint firing, authenticationProvider being queried, the filter being added to the chain, and my rememberMeServices being added to that filter. Everything correct.

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();
    */
  }

}

However, the code in the DSL looks like this, and when it is used, the authenticationEntryPoint never fires. The rememberMeServices do get configured, and it looks like the filter gets added to the chain correctly, but I just get an error page for a 403 response instead of seeing the entryPoint redirection.

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);

  }
}

Clearly, there's some subtle interaction that I'm missing in the documentation or something, causing my DSL-based configuration of entryPoint to get lost. Any idea why? If I had to guess, it would be that I'm doing something wrong with the way I'm specifying paths, but I can't figure it out.

来源:https://stackoverflow.com/questions/44818399/why-does-httpsecurity-configuration-via-dsl-not-seem-to-work-the-same-as-explici

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!