Spring Security预身份验证/登录

时间:2015-11-03 03:48:15

标签: spring spring-security pre-authentication

我使用Spring Security进行了概念验证,以便使用PRE_AUTH_FILTER过滤器执行预身份验证。它工作正常,但我想知道如果此过滤器不起作用我是否可以重定向到登录页面,因为我得到HTTP 403.
我的意思是,如果初始请求中不包含标题中的SM_USER字段,我该如何重定向到登录页面?我需要考虑这两种情况(当它包含字段时 - SM_USER - ,何时不包含)并且我无法使其正常工作。关于它的任何想法?

2 个答案:

答案 0 :(得分:0)

Pra-authentication在Spring Security中使用登录身份验证可以顺利运行。您只需设置一个有效的登录表单配置,并添加PRE_AUTH_FILTER过滤器。

Spring仅重定向到登录页面,如果在通过身份验证过滤器后,它会检测到用户未经过身份验证。因此,如果请求包含标头中的预期字段,则用户将通过PRE_AUTH_FILTER过滤器进行身份验证,并且不会转到登录页面。但如果它不包含一个,Spring安全性将检测到缺少身份验证并重定向到登录页面。

答案 1 :(得分: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> 

<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>