成功登录后,Spring始终重定向到默认目标URL

时间:2015-08-06 10:46:13

标签: java spring redirect login spring-security

SpringFramework安全性在我已经定义的成功登录后将我重定向到默认目标URL(场景1),但我会指定其他位置,但事实证明需要登录SpringFramework也会重定向到默认位置而不是位置由用户指定(场景2)。

情景1:

情景2:

我总是使用UseDefaultTargetUrl = false。那不够吗? 我使用自定义过滤器链而不是http标签,因为Web服务需要基本身份验证

SpringFramework版本:3.0.5

安全配置:

<bean id="httpSessionContextIntegrationFilterWithASCFalse" class="org.springframework.security.web.context.SecurityContextPersistenceFilter">
    <!--property name="allowSessionCreation" value="false" TODO no property like this exist any more - probably not needed /-->
</bean>

<bean id="httpSessionContextIntegrationFilter" class="org.springframework.security.web.context.SecurityContextPersistenceFilter" />

<bean id="basicAuthenticationEntryPoint"
    class="org.springframework.security.web.authentication.www.BasicAuthenticationEntryPoint">
    <property name="realmName" value="Spring Security Application" />
</bean>

<bean id="basicAuthenticationFilter" class="org.springframework.security.web.authentication.www.BasicAuthenticationFilter">
    <property name="authenticationEntryPoint" ref="basicAuthenticationEntryPoint" />
    <property name="authenticationManager" ref="authenticationManager" />
</bean>

<bean id="accessDeniedHandlerImpl" class="org.springframework.security.web.access.AccessDeniedHandlerImpl" />

<bean id="basicExceptionTranslationFilter" class="org.springframework.security.web.access.ExceptionTranslationFilter">
    <property name="authenticationEntryPoint" ref="basicAuthenticationEntryPoint" />
    <property name="accessDeniedHandler" ref="accessDeniedHandlerImpl" />
</bean>

<bean id="logoutFilter" class="org.springframework.security.web.authentication.logout.LogoutFilter">
    <constructor-arg index="0" value="/" />
    <constructor-arg index="1">
        <bean class="org.springframework.security.web.authentication.logout.SecurityContextLogoutHandler" />
    </constructor-arg>
</bean>

<bean id="formLoginFilter" class="org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter">
    <property name="authenticationManager" ref="authenticationManager" />
    <!--property name="defaultTargetUrl" value="/login!login.action" / to be investigated ! -->
    <property name="authenticationFailureHandler" ref="customAuthenticationFailureHandler" />
    <property name="authenticationSuccessHandler" ref="customAuthenticationSuccessHandler" />
</bean>


<bean id="customAuthenticationFailureHandler" class="org.springframework.security.web.authentication.SimpleUrlAuthenticationFailureHandler">
    <property name="defaultFailureUrl" value="/login.action?authenticationFailed=true" />
</bean>

<bean id="customAuthenticationSuccessHandler" class="org.springframework.security.web.authentication.SimpleUrlAuthenticationSuccessHandler">
    <property name="defaultTargetUrl" value="/search.action" />
    <property name="alwaysUseDefaultTargetUrl" value="false" />
</bean>


<bean id="formLoginEntryPoint" class="org.springframework.security.web.authentication.LoginUrlAuthenticationEntryPoint">
    <property name="loginFormUrl" value="/login.action" />
</bean>

<bean id="securityContextHolderAwareRequestFilter" class="org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter" />

<bean id="exceptionTranslationFilter" class="org.springframework.security.web.access.ExceptionTranslationFilter">
    <property name="authenticationEntryPoint" ref="formLoginEntryPoint" />
    <property name="accessDeniedHandler" ref="accessDeniedHandlerImpl" />
</bean>


<bean id="accessManager" class="org.springframework.security.access.vote.AffirmativeBased">
    <property name="decisionVoters">
        <list>
            <bean class="org.springframework.security.access.vote.AuthenticatedVoter" />
            <bean class="org.springframework.security.access.vote.RoleVoter" />
        </list>
    </property>
</bean>

<bean id="filterSecurityInterceptor" class="org.springframework.security.web.access.intercept.FilterSecurityInterceptor">
    <property name="authenticationManager" ref="authenticationManager" />
    <property name="accessDecisionManager" ref="accessManager" />

    <property name="securityMetadataSource">
        <security:filter-security-metadata-source>
          <security:intercept-url pattern="/bsh*" access="ROLE_Bsh"/>
          <security:intercept-url pattern="/admin/**" access="ROLE_Administrators"/>
          <security:intercept-url pattern="/**" access="ROLE_Users,ROLE_Administrators"/>
        </security:filter-security-metadata-source>
      </property>

</bean>

<bean id="springSecurityFilterChain" class="org.springframework.security.web.FilterChainProxy">
    <security:filter-chain-map path-type="ant">
        <security:filter-chain pattern="/c/**" filters="none" />
        <security:filter-chain pattern="/i/**" filters="none" />
        <security:filter-chain pattern="/j/**" filters="none" />
        <security:filter-chain pattern="/accessdenied.jsp" filters="none" />
        <security:filter-chain pattern="/login.action*" filters="none" />
        <security:filter-chain pattern="/services/**"
            filters="httpSessionContextIntegrationFilterWithASCFalse,logoutFilter,
                   basicAuthenticationFilter,basicExceptionTranslationFilter,
                   filterSecurityInterceptor" />
        <security:filter-chain pattern="/**"
            filters="httpSessionContextIntegrationFilter,logoutFilter,formLoginFilter,
                     securityContextHolderAwareRequestFilter,
                     exceptionTranslationFilter,
                     filterSecurityInterceptor" />
    </security:filter-chain-map>
</bean>

2 个答案:

答案 0 :(得分:2)

知道了!我不得不用SavedRequestAwareAuthenticationSuccessHandler替换SimpleUrlAuthenticationSuccessHandler

答案 1 :(得分:0)

验证成功发生时解决错误的URL:

  • 在你的application-security.xml中,你必须在里面标记:

    认证成功处理程序-REF =&#34; loginSuccessHandler&#34;

相关问题