2015-11-03 263 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>