将Spring Security移至Java Config,authentication-success-handler-ref会去哪儿?


问题内容

我们的应用程序具有用于成功登录的自定义成功处理程序。基本上,它们会将他们重定向到会话过期时所在的页面。

我们将转向Java配置而不是spring xml配置。其余配置非常顺利,但是我们找不到在哪里放置security:form-
login标记的authentication-success-handler-ref属性。

<security:http auto-config='true'>
  ...
  <security:intercept-url pattern="/**" access="IS_AUTHENTICATED_FULLY"/>
  <security:form-login login-page="/login" default-target-url="/sites"
                     authentication-failure-url="/login"
                     authentication-success-handler-ref="authenticationSuccessHandler"/>
 ...

到目前为止,这是我们的配置。

  @Override
  protected void configure(HttpSecurity http) throws Exception {
    http
       .authorizeRequests()
          .anyRequest().authenticated()
          .and()
        .formLogin()
          .loginPage("/login")
          .failureUrl("/login")
          .and()
        .logout()
          .permitAll()
          .and()
  }

另外,我们找不到默认目标网址的放置位置,但这绝对不那么重要。

请注意,我们实际上是在使用Groovy,但是代码基本上与Java配置相同。


问题答案:

所有设置都可以在全局configure方法内完成。添加以下内容:

@Override
protected void configure(HttpSecurity http) throws Exception {
    http
        .authorizeRequests()
          .anyRequest().authenticated()
          .and()
        .formLogin()
          .loginPage("/login")
          .defaultSuccessUrl("/sites")
          .failureUrl("/login")
          .successHandler(yourSuccessHandlerBean) // autowired or defined below
          .and()
        .logout()
          .permitAll()
          .and()
  }