簡體   English   中英

將Spring Security遷移到Java Config,authentication-success-handler-ref在哪里?

[英]Moving Spring Security To Java Config, where does authentication-success-handler-ref go?

我們的應用程序具有成功登錄的自定義成功處理程序。 它基本上將它們重定向到會話到期時它們所在的頁面。

我們正在轉向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()
  }

此外,我們無法找到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()
  }

您必須創建bean擴展SimpleUrlAuthenticationSuccessHandlerSavedRequestAwareAuthenticationSuccessHandler 例如:

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

然后你必須在bean上設置它,擴展AbstractAuthenticationProcessingFilter

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM