2013-02-25 38 views
1

我有一個使用spring安全性的web應用程序來管理身份驗證。 我的客戶有2個登錄表單,我的春天的安全配置如下:彈簧安全和多個登錄表單

<http auto-config="true" use-expressions="true" create-session="always"> 
     <intercept-url pattern="/**" access="permitAll" /> 

     <form-login login-processing-url="/user/login" login-page="/user/login/unauthorized" 
      default-target-url="/user/firstLogin" authentication-failure-url="/user/login/failure" /> 

     <form-login login-processing-url="/user/relogin" login-page="/user/login/unauthorized" 
      default-target-url="/user/reLoginFromClient" authentication-failure-url="/user/login/failure" /> 

     <logout logout-url="/user/logout/spring" logout-success-url="/user/logout/success" /> 
     <access-denied-handler ref="accessDeniedHandler"/> 
    </http> 

第一種形式-login元素工作正常,即我能夠從/user/login網址登錄。 但是,當我嘗試從第二個URL /user/relogin登錄時,我從服務器得到415:不支持的媒體類型響應。

請注意,如果我切換這兩個元素,最上面的那個工作正常,最下面的那個會導致415響應。

我一樣的選擇答案的建議,我的配置現在看起來是這樣的:

<http auto-config="true" use-expressions="true" create-session="always" authentication-manager-ref="authenticationManager"> 
     <intercept-url pattern="/**" access="permitAll" /> 
     <custom-filter after="SECURITY_CONTEXT_FILTER" ref="reLoginFilter"/> 
     <form-login login-processing-url="/user/login" login-page="/user/login/unauthorized" 
      default-target-url="/user/firstLogin" authentication-failure-url="/user/login/failure" /> 
     <logout logout-url="/user/logout/spring" logout-success-url="/user/logout/success" /> 
     <access-denied-handler ref="accessDeniedHandler"/> 
    </http> 

    <beans:bean id="reLoginFilter" class="org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter"> 
     <beans:property name="authenticationManager" ref="authenticationManager"/> 
     <beans:property name="filterProcessesUrl" value="/user/relogin"/> 
     <beans:property name="authenticationSuccessHandler" ref="authenticationSuccessHandler" /> 
     <beans:property name="authenticationFailureHandler" ref="authenticationFailHandler" /> 
    </beans:bean> 

    <beans:bean id="authenticationSuccessHandler" class="org.springframework.security.web.authentication.SimpleUrlAuthenticationSuccessHandler"> 
     <beans:property name="defaultTargetUrl" value="/user/relogin/success"/> 
    </beans:bean> 

    <beans:bean id="authenticationFailHandler" class="org.springframework.security.web.authentication.SimpleUrlAuthenticationFailureHandler"> 
     <beans:property name="defaultFailureUrl" value="/user/login/failure"/> 
    </beans:bean> 
+0

出於好奇,你總是使用'auto-config =「true」'和'create-session =「的原因是什麼? – 2013-02-26 15:40:06

+0

@Luke,可能沒有。我需要先驗證這一點,然後再刪除它們。 – 2013-02-27 10:13:21

回答

1

你不能在一個<http>元素中使用多個<form-login>元素。

取而代之,您可以使用一個並添加第二個,方法是定義一個UsernamePasswordAuthenticationFilter bean並使用custom-filter元素插入它。您也應該刪除auto-config。爲每個請求創建會話也很少需要,所以我會刪除create-session屬性,除非您確定需要它。

+0

感謝盧克,虐待用戶名PasswordPasswordAuthenticationFilter。 – 2013-02-25 16:58:05