将Spring Security移至Java Config,authentication-success-handler-ref在哪里?

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

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

 ...   ... 

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

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

此外,我们找不到将default-target-url放在哪里,但这绝对不那么重要。

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

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

 @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() } 

您必须创建扩展SimpleUrlAuthenticationSuccessHandlerSavedRequestAwareAuthenticationSuccessHandler bean。 例如:

 @Bean public SavedRequestAwareAuthenticationSuccessHandler successHandler() { SavedRequestAwareAuthenticationSuccessHandler successHandler = new SavedRequestAwareAuthenticationSuccessHandler(); successHandler.setTargetUrlParameter("/secure/"); return successHandler; } 

然后你必须在bean上设置它,扩展AbstractAuthenticationProcessingFilter

 UsernamePasswordAuthenticationFilter authenticationFilter = new UsernamePasswordAuthenticationFilter(); authenticationFilter.setAuthenticationSuccessHandler(successHandler());