Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring mvc Spring安全中的会话管理_Spring Mvc_Spring Security_Session Management - Fatal编程技术网

Spring mvc Spring安全中的会话管理

Spring mvc Spring安全中的会话管理,spring-mvc,spring-security,session-management,Spring Mvc,Spring Security,Session Management,我是新来的春天安全。我需要进行基于URL的身份验证,其中用户需要基于唯一引用进行身份验证,该引用将作为URL中的参数每次发送。我将把这个引用传递给一个Web服务,获取所需的数据,然后对用户进行身份验证(并设置角色)。身份验证和授权部分工作正常 但是,当我再次尝试访问应用程序时(现在URL中有一个不同的引用),它会说“SecurityContextHolder没有使用匿名令牌填充,因为它已经包含…”,并且它显示了以前请求的详细信息。我已尝试使用清除安全上下文 SecurityContextHold

我是新来的春天安全。我需要进行基于URL的身份验证,其中用户需要基于唯一引用进行身份验证,该引用将作为URL中的参数每次发送。我将把这个引用传递给一个Web服务,获取所需的数据,然后对用户进行身份验证(并设置角色)。身份验证和授权部分工作正常

但是,当我再次尝试访问应用程序时(现在URL中有一个不同的引用),它会说“SecurityContextHolder没有使用匿名令牌填充,因为它已经包含…”,并且它显示了以前请求的详细信息。我已尝试使用清除安全上下文 SecurityContextHolder.getContext().setAuthentication(null)和SecurityContextHolder.clearContext()

在此之后,我可以多次访问该应用程序。然而,如果我试图同时从我同事的机器上访问应用程序,我会得到一个空白页。检查日志后,我看到一条消息“SecurityContextHolder未使用匿名令牌填充…”。我也尝试过设置课程,但我不知道我在哪里迷失了方向

下面是my web.xml(仅spring安全部分):


springSecurityFilterChain
org.springframework.web.filter.DelegatingFilterProxy
springSecurityFilterChain
/*
org.springframework.web.context.ContextLoaderListener
org.springframework.security.web.session.HttpSessionEventPublisher
30
spring-security.xml:

<http use-expressions="true" auto-config="false" entry-point-                   
                            ref="http403ForbiddenEntryPoint">
<intercept-url pattern="/paymentPortalInternal.*" access="hasRole('Internal')"/>
<intercept-url pattern="/paymentPortalExternal.*" access="hasRole('External')"/>

<custom-filter position="PRE_AUTH_FILTER" ref="customAuthenticationFilter"/>
<custom-filter position="CONCURRENT_SESSION_FILTER" ref="concurrencyFilter" />
<session-management session-authentication-strategy-ref="sas"/>
</http>

<beans:bean id="concurrencyFilter"
class="org.springframework.security.web.session.ConcurrentSessionFilter">
<beans:property name="sessionRegistry" ref="sessionRegistry" />
<beans:property name="expiredUrl" value="/session-expired.htm" />
</beans:bean>

<beans:bean id="http403ForbiddenEntryPoint"
  class="org.springframework.security.web.authentication.Http403ForbiddenEntryPoint"/>
<beans:bean id="customAuthenticationFilter"
  class="com.xxx.xxx.xxxxx.security.CustomAuthenticationFilter">
      <beans:property name="sessionAuthenticationStrategy" ref="sas" />
  <beans:property name="authenticationManager" ref="authenticationManager" />
</beans:bean>

<beans:bean id="sas" 
              class="org.springframework.security.web.authentication.session.
                                               ConcurrentSessionControlStrategy">
<beans:constructor-arg name="sessionRegistry" ref="sessionRegistry" />
<beans:property name="maximumSessions" value="1" />
<beans:property name="exceptionIfMaximumExceeded" value="true" />
</beans:bean>

<beans:bean id="sessionRegistry"    
class="org.springframework.security.core.session.SessionRegistryImpl" />

<authentication-manager alias="authenticationManager">
    <authentication-provider ref="preauthAuthProvider" />
</authentication-manager>

<beans:bean id="preauthAuthProvider" 
                class="org.springframework.security.web.authentication.preauth.
                                       PreAuthenticatedAuthenticationProvider">
<beans:property name="preAuthenticatedUserDetailsService">
    <beans:bean class="com.XXX.XXXX.XXX.UserServiceImpl" />
    </beans:property>
</beans:bean>

如果我需要提供更多信息,请告诉我

编辑:添加日志

对于第一个请求:

2013-02-07 17:27:38834调试[http-8081-2][org.springframework.security.web.context.HttpSessionSecurityContextRepository.readSecurityContextFromSession(127)]-目前不存在HttpSession 2013-02-07 17:27:38834调试[http-8081-2][org.springframework.security.web.context.HttpSessionSecurityContextRepository.loadContext(85)]-HttpSession中没有可用的SecurityContext:null。将创建一个新的

对于第二个请求(请注意,安全上下文中的详细信息是第一个请求的详细信息):

2013-02-07 17:27:54272调试[http-8081-2][org.springframework.security.web.context.HttpSessionSecurityContextRepository.readSecurityContextFromSession(158)]-从SPRING\u SECURITY\u上下文中获取了有效的SecurityContext:'org.springframework.SECURITY.core.CONTEXT。SecurityContextImpl@1017293c:Authentication:org.springframework.security.web.Authentication.preauth。PreAuthenticatedAuthenticationToken@1017293c:Principal:org.springframework.security.core.userdetails。User@35cd3811:用户名:Internal@5581e6e1-7e61-41bb-9257-b3c1acb96519;密码:[受保护];启用:真;AccountNoExpired:正确;无需证明的凭证:真实;AccountNonLocked:true;授予的权限:内部;凭据:[受保护];认证:正确;详细信息:org.springframework.security.web.authentication。WebAuthenticationDetails@ffffc434:RemoteIP地址:10.188.182.107;SessionId:null;授予的权限:内部'


我的理解是,安全上下文持有者存储所有用户的详细信息。但是在这种情况下,我无法从其他选项卡/浏览器启动应用程序。

如果CustomAuthenticationFilter正在扩展
AbstractPreAuthenticationdProcessingFilter
,以下两个属性可能会让您有所了解。 1. <代码>检查主要更改
2.
InvalidateSessionPrincipalChange

我可以通过覆盖默认过滤器链(使用的过滤器链代理)并仅调用所需的过滤器来解决此问题。感谢LukeTaylor和Ketan的建议。如果有人有同样的问题,请告诉我。我可以发布我的XML和其他内容。

听起来您的自定义身份验证筛选器忽略了URL参数中的更改,可能是因为用户已经通过了身份验证。此外,您可能应该删除所有并发会话的内容,直到基本功能正常工作。感谢您的回复。这里的问题是,当第二个请求被激发时,我的customAuthenticationFilter根本不会被触发。请查看我添加到问题中的日志跟踪。@LukeTaylor,在这里再添加一点。。我们没有适当的登录机制。我们只有一个来自URL的唯一引用。因此,理想情况下,当第二个请求出现时,它与新用户尝试获得身份验证一样好。然而,安全上下文已经填充了第一个用户的令牌,我现在不知道。我现在正在试用SecurityContextPersistenceFilter过滤器。我的方向正确吗?如果您在同一会话中连接,那么安全上下文已经包含以前的身份验证也就不足为奇了。您真的应该发布
CustomAuthenticationFilter
的概要。你说它不是“触发的”。你调试过它了吗?它是一个过滤器,因此每个请求都会调用它的
doFilter
方法。在那里放一个断点。您还没有阐明为什么要使用所有并发会话基础结构。此外,出于安全原因,通常不赞成在URL中传递身份验证参数。@LukeTaylor,我找到了第二个请求未触发customAuthFilter的原因。看来我引用它的位置是错误的。我在前面使用了position=“PRE\u AUTH\u FILTER”。我尝试过其他位置(如FIRST、BASIC_AUTH_FILTER等),该过滤器能够对多个请求进行身份验证。但是,在my customAuthFilter之后将触发匿名筛选器,它将使用匿名令牌覆盖安全上下文。有没有办法避免这种情况?我现在尝试使用FilterProxy链来仅使用requir
<http use-expressions="true" auto-config="false" entry-point-                   
                            ref="http403ForbiddenEntryPoint">
<intercept-url pattern="/paymentPortalInternal.*" access="hasRole('Internal')"/>
<intercept-url pattern="/paymentPortalExternal.*" access="hasRole('External')"/>

<custom-filter position="PRE_AUTH_FILTER" ref="customAuthenticationFilter"/>
<custom-filter position="CONCURRENT_SESSION_FILTER" ref="concurrencyFilter" />
<session-management session-authentication-strategy-ref="sas"/>
</http>

<beans:bean id="concurrencyFilter"
class="org.springframework.security.web.session.ConcurrentSessionFilter">
<beans:property name="sessionRegistry" ref="sessionRegistry" />
<beans:property name="expiredUrl" value="/session-expired.htm" />
</beans:bean>

<beans:bean id="http403ForbiddenEntryPoint"
  class="org.springframework.security.web.authentication.Http403ForbiddenEntryPoint"/>
<beans:bean id="customAuthenticationFilter"
  class="com.xxx.xxx.xxxxx.security.CustomAuthenticationFilter">
      <beans:property name="sessionAuthenticationStrategy" ref="sas" />
  <beans:property name="authenticationManager" ref="authenticationManager" />
</beans:bean>

<beans:bean id="sas" 
              class="org.springframework.security.web.authentication.session.
                                               ConcurrentSessionControlStrategy">
<beans:constructor-arg name="sessionRegistry" ref="sessionRegistry" />
<beans:property name="maximumSessions" value="1" />
<beans:property name="exceptionIfMaximumExceeded" value="true" />
</beans:bean>

<beans:bean id="sessionRegistry"    
class="org.springframework.security.core.session.SessionRegistryImpl" />

<authentication-manager alias="authenticationManager">
    <authentication-provider ref="preauthAuthProvider" />
</authentication-manager>

<beans:bean id="preauthAuthProvider" 
                class="org.springframework.security.web.authentication.preauth.
                                       PreAuthenticatedAuthenticationProvider">
<beans:property name="preAuthenticatedUserDetailsService">
    <beans:bean class="com.XXX.XXXX.XXX.UserServiceImpl" />
    </beans:property>
</beans:bean>