2015-11-03 260 views
0

我做了Spring Security的概念證明,以便使用PRE_AUTH_FILTER過濾器執行預認證。它的工作正常,但我想知道如果我可以重定向到登錄頁面,如果此過濾器不起作用,因爲我得到HTTP 403.
我的意思是,如果初始請求不包含SM_USER字段在標題,我如何重定向到登錄頁面?我需要考慮這兩種情況(當它包含字段 - SM_USER - 時,沒有),我無法得到它的工作。有關它的任何想法?春季安全預認證/登錄

+0

向我們展示你的配置請 – ArunM

回答

0

在Spring Security中,Pra-authentication可以與登錄認證一起順利進行。您只需設置工作登錄表單配置,然後添加PRE_AUTH_FILTER過濾器。

Spring只重定向到登錄頁面,如果在通過認證過濾器後,它檢測到用戶在他應該時沒有被認證。因此,如果請求包含頭中的預期字段,則該用戶將由PRE_AUTH_FILTER過濾器進行身份驗證,並且不會進入登錄頁面。但是,如果它不包含Spring Security,它將檢測到缺少身份驗證並重定向到登錄頁面。

+0

感謝回答。我基本上試圖做到這一點,但它不適合我。 – Luis

0

這是我的設置:

<http auto-config="true" use-expressions="true" entry-point-ref="http403EntryPoint"> 
    <intercept-url pattern="/login" access="permitAll" /> 
    <intercept-url pattern="/logout" access="permitAll" /> 
    <intercept-url pattern="/accessdenied" access="permitAll" /> 
    <intercept-url pattern="/**" access="hasRole('ROLE_USER')" /> 
    <custom-filter before="PRE_AUTH_FILTER" ref="siteminderFilter" /> 
    <form-login login-page="/login" default-target-url="/list" authentication-failure-url="/accessdenied" /> 
    <logout logout-success-url="/logout" /> 
</http> 

<beans:bean id="siteminderFilter" class="org.springframework.security.web.authentication.preauth.RequestHeaderAuthenticationFilter"> 
    <beans:property name="principalRequestHeader" value="SM_USER"/> 
    <beans:property name="authenticationManager" ref="authenticationManager" /> 
    <beans:property name="exceptionIfHeaderMissing" value="false" /> 
</beans:bean> 

<beans:bean id="preauthAuthProvider" class="org.springframework.security.web.authentication.preauth.PreAuthenticatedAuthenticationProvider"> 
    <beans:property name="preAuthenticatedUserDetailsService"> 
     <beans:bean id="userDetailsServiceWrapper" class="org.springframework.security.core.userdetails.UserDetailsByNameServiceWrapper"> 
      <beans:property name="userDetailsService" ref="customUserDetailsService"/> 
     </beans:bean> 
    </beans:property> 
</beans:bean> 

<authentication-manager alias="authenticationManager"> 
    <authentication-provider user-service-ref="employeeDAO" /> 
    <authentication-provider ref="preauthAuthProvider" /> 
</authentication-manager> 

<beans:bean id="customUserDetailsService" class="com.test.security.CustomUserDetailsService"></beans:bean> 
<beans:bean id="http403EntryPoint" class="org.springframework.security.web.authentication.Http403ForbiddenEntryPoint"></beans:bean>