由于已提交响应,因此无法创建会话。无法存储SecurityContext

时间:2016-06-07 04:48:15

标签: spring spring-security

摘要

我们已在applicationContext.xml文件中配置了filterchain,如下所述。

<bean id="springSecurityFilterChain" class="org.springframework.security.web.FilterChainProxy">
        <sec:filter-chain-map path-type="ant" >
            <sec:filter-chain pattern="/**" filters="requestContextFilter,securityContextFilter,exceptionTranslationFilter,userRoleProcessingFilter" />
        </sec:filter-chain-map>
    </bean>

    <bean id="requestContextFilter" class="org.springframework.web.filter.RequestContextFilter"/>

    <bean id="securityContextFilter" class="org.springframework.security.web.context.SecurityContextPersistenceFilter">
         <property name="securityContextRepository">
                <bean class="org.springframework.security.web.context.HttpSessionSecurityContextRepository"></bean>
         </property>
         <property name="forceEagerSessionCreation" value="false"/>
    </bean>

    <bean id="exceptionTranslationFilter" class="org.springframework.security.web.access.ExceptionTranslationFilter" >
        <property name="authenticationEntryPoint">
            <bean class="org.springframework.security.web.authentication.Http403ForbiddenEntryPoint" />
        </property>
    </bean>
     <bean id="userRoleProcessingFilter"
                class="org.springframework.security.web.authentication.preauth.RequestHeaderAuthenticationFilter">
                <property name="principalRequestHeader" value="CC_USER" />
                <property name="credentialsRequestHeader" value="CC_CRED" />
                <property name="authenticationManager" ref="authenticationManager" />
                <property name="continueFilterChainOnUnsuccessfulAuthentication" value="false" />
                <property name="exceptionIfHeaderMissing" value="false"></property>
                <property name="checkForPrincipalChanges" value="true"></property>
        </bean>
 <bean id="authenticationManager" class="org.springframework.security.authentication.ProviderManager" >
        <property name="providers" >
            <list>
                <ref local="authenticationProvider" />
            </list>
        </property>
    </bean>
<bean id="authenticationProvider" class="com.powerup.common.authorization.spring.xxx.AuthenticationProvider" >
        <property name="preAuthenticatedUserDetailsService">
            <bean class="com.powerup.common.authorization.spring.xxx.UserDetailsService">
            </bean>
        </property>
    </bean>

和web.xml的配置如下所示。

<web-app>
<!------->
    <filter>
        <filter-name>springSecurityFilterChain</filter-name>
        <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
        <init-param>
            <param-name>targetBeanName</param-name>
            <param-value>springSecurityFilterChain</param-value>
        </init-param>
    </filter>

    <filter-mapping>
        <filter-name>springSecurityFilterChain</filter-name>
        <url-pattern>/remoting/*</url-pattern>
    </filter-mapping>
</web-app>

实际行为

Spring安全性应该在成功的身份验证请求时持久保存会话但在我们的情况下,它无法有时持久化会话(此行为不一致)并导致最终创建新会话导致我的应用程序再次进行实际身份验证。请注意,此行为不一致。

我在spring安全日志文件中看到以下消息。

  

2016-05-10 09:41:22,042 DEBUG [org.springframework.security.web.FilterChainProxy] / remoting / ViewService位于第1位的4位额外过滤器链;触发过滤器:&#39; RequestContextFilter&#39;   2016-05-10 09:41:22,042 DEBUG [org.springframework.security.web.FilterChainProxy] / remoting / ViewService在第2位的4位附加过滤器链;触发过滤器:&#39; SecurityContextPersistenceFilter&#39;   2016-05-10 09:41:22,042 DEBUG [org.springframework.security.web.context.HttpSessionSecurityContextRepository]当前没有HttpSession存在   2016-05-10 09:41:22,042 DEBUG [org.springframework.security.web.context.HttpSessionSecurityContextRepository] ​​HttpSession中没有可用的SecurityContext:null。将创建一个新的。   2016-05-10 09:41:22,042 DEBUG [org.springframework.security.web.FilterChainProxy] / remoting / ViewService在第3位的4位附加过滤器链;触发过滤器:&#39; ExceptionTranslationFilter&#39;   2016-05-10 09:41:22,042 DEBUG [org.springframework.security.web.FilterChainProxy] / remoting / ViewService在4位4的附加过滤链中;触发过滤器:&#39; RequestHeaderAuthenticationFilter&#39;   2016-05-10 09:41:22,042 DEBUG [org.springframework.security.web.authentication.preauth.RequestHeaderAuthenticationFilter]检查安全上下文令牌:null   2016-05-10 09:41:22,042 DEBUG [org.springframework.security.web.authentication.preauth.RequestHeaderAuthenticationFilter] preAuthenticatedPrincipal = snme,试图验证   2016-05-10 09:41:22,064 DEBUG [org.springframework.security.web.authentication.preauth.PreAuthenticatedAuthenticationProvider] PreAuthenticated身份验证请求:org.springframework.security.web.authentication.preauth.PreAuthenticatedAuthenticationToken@6bc667b:Principal:snme;证书:[保护];认证:false;详细信息:org.springframework.security.web.authentication.WebAuthenticationDetails@7798:RemoteIpAddress:144.5.156.254; SessionId:null;没有授予任何权力   2016-05-10 09:41:22,068 DEBUG [org.springframework.security.web.authentication.preauth.RequestHeaderAuthenticationFilter]身份验证成功:org.springframework.security.web.authentication.preauth.PreAuthenticatedAuthenticationToken@e031ed5f:Principal:org.springframework .security.core.userdetails.User @ 35f133:用户名:snme;密码保护];启用:true; AccountNonExpired:true; credentialsNonExpired:true; AccountNonLocked:true;授权机构:UIM用户;证书:[保护];认证:真实;详细信息:org.springframework.security.web.authentication.WebAuthenticationDetails@7798:RemoteIpAddress:144.5.156.254; SessionId:null;授权机构:UIM用户   2016-05-10 09:41:22,068 DEBUG [org.springframework.security.web.FilterChainProxy] / remoting / ViewService到达额外过滤链的末尾;继续与原始链   2016-05-10 09:41:22,342 DEBUG [org.springframework.security.web.context.HttpSessionSecurityContextRepository]作为SecurityContext创建的HttpSession是非默认的   2016-05-10 09:41:22,342 WARN [org.springframework.security.web.context.HttpSessionSecurityContextRepository]无法创建会话,因为已提交响应。无法存储SecurityContext。   2016-05-10 09:41:22,342 DEBUG [org.springframework.security.web.context.HttpSessionSecurityContextRepository]作为SecurityContext创建的HttpSession是非默认的   2016-05-10 09:41:22,342 WARN [org.springframework.security.web.context.HttpSessionSecurityContextRepository]无法创建会话,因为已提交响应。无法存储SecurityContext。   2016-05-10 09:41:22,342 DEBUG [org.springframework.security.web.context.HttpSessionSecurityContextRepository]作为SecurityContext创建的HttpSession是非默认的   2016-05-10 09:41:22,342 WARN [org.springframework.security.web.context.HttpSessionSecurityContextRepository]无法创建会话,因为已提交响应。无法存储SecurityContext。   2016-05-10 09:41:22,343 DEBUG [org.springframework.security.web.access.ExceptionTranslationFilter]正常处理链   2016-05-10 09:41:22,343 DEBUG [org.springframework.security.web.context.SecurityContextPersistenceFilter] SecurityContextHolder现已清除,请求处理完成   2016-05-10 09:41:23,254 DEBUG [org.springframework.security.web.FilterChainProxy] / remoting / LockingService在第1位的4位附加过滤链中;触发过滤器:&#39; RequestContextFilter&#39;   2016-05-10 09:41:23,254 DEBUG [org.springframework.security.web.FilterChainProxy] / remoting / LockingService在第2位的4位附加过滤链中;触发过滤器:&#39; SecurityContextPersistenceFilter&#39;   2016-05-10 09:41:23,254 DEBUG [org.springframework.security.web.context.HttpSessionSecurityContextRepository]当前没有HttpSession存在   2016-05-10 09:41:23,254 DEBUG [org.springframework.security.web.context.HttpSessionSecurityContextRepository]没有来自HttpSession的SecurityContext:null。将创建一个新的。   2016-05-10 09:41:23,255 DEBUG [org.springframework.security.web.FilterChainProxy] / remoting / LockingService在第3位的4位附加过滤链中;触发过滤器:&#39; ExceptionTranslationFilter&#39;   2016-05-10 09:41:23,255 DEBUG [org.springframework.security.web.FilterChainProxy] / remoting / LockingService在4位4的附加过滤链中;触发过滤器:&#39; RequestHeaderAuthenticationFilter&#39;   2016-05-10 09:41:23,255 DEBUG [org.springframework.security.web.authentication.preauth.RequestHeaderAuthenticationFilter]检查安全上下文令牌:null   2016-05-10 09:41:23,255 DEBUG [org.springframework.security.web.authentication.preauth.RequestHeaderAuthenticationFilter] preAuthenticatedPrincipal = snme,试图验证

预期行为

如果上下文已成功存储,则不会有不必要的新身份验证请求。

版本

春季4.0.6 春季安全3.2.4

spring-security-config-3.2.4.RELEASE.jar spring-security-core-3.2.4.RELEASE.jar spring-security-crypto-3.2.4.RELEASE.jar spring-security-web-3.2.4.RELEASE.jar

spring-core-4.0.6.RELEASE.jar

非常感谢任何帮助。

0 个答案:

没有答案