java - 成功登录后,Spring 总是重新编辑到默认目标 URL

标签 java spring redirect authentication spring-security

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

场景 1:

场景 2:

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

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>

最佳答案

明白了!我不得不用 SavedRequestAwareAuthenticationSuccessHandler 替换 SimpleUrlAuthenticationSuccessHandler

关于java - 成功登录后,Spring 总是重新编辑到默认目标 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31853745/

相关文章:

java - Hibernate:如何映射到静态表?

java - Hibernate 添加一列原始类型字段为 Non Nullable

java - 如何用Python、C或Java读取大数据文件的一部分?

javascript - jQuery - 如果移动设备重定向,则如果单击链接,则忽略第一个 if

redirect - 主页重定向会影响搜索引擎抓取吗?

azure - OWIN 和 Azure AD HTTPS 到 HTTP 重定向循环

java - 如何使此方法对所有传递的类类型通用?

java - 数据库连接未关闭

angular - 如何为PostgreSQL支持的Spring,Angular和Django微服务编写Docker容器

java - Spring Boot中分布式任务调度和Job队列的建议