首页 文章

Spring会话 - 重定向到目标URL后,在会话中找不到安全上下文

提问于
浏览
0

我正在使用带有淡褐色和 spring 安全性的 Spring 季 Session . 我面临的问题是Spring安全无法从httpsession加载安全上下文 . 在身份验证期间,我可以在以下代码中看到安全上下文设置为session:

if (contextChanged(context)||  httpSession.getAttribute(springSecurityContextKey) == null) {
  httpSession.setAttribute(springSecurityContextKey, context);
  if (logger.isDebugEnabled()) {
    logger.debug("SecurityContext '" + context
                + "' stored to HttpSession: '" + httpSession);
    }
}

但是在Spring重定向到目标url之后进行身份验证后,它无法从以下代码中的会话中获取安全上下文:

Object contextFromSession = httpSession.getAttribute(springSecurityContextKey);

if (contextFromSession == null) {

  if (debug) {
    logger.debug("HttpSession returned null object for SPRING_SECURITY_CONTEXT");
  }
  return null;
}

知道这里有什么问题吗?安全调试日志 -

wasSessionFixationProtectionStrategy:已启动新会话:2192be54-aee1-4249-98ba-01a65a401830 ciiwsLoggingSecurityEventListener:event = SessionFixationProtectionEvent username = cgrant remoteAddress = 0:0:0:0:0:0:0:1 sessionId = c7363d39-28ff-44e3- 83a9-d463f2f371e5 waUsernamePasswordAuthenticationFilter:身份验证成功 . 更新SecurityContextHolder以包含:org.springframework.security.authentication.UsernamePasswordAuthenticationToken@5e87c6c5:Principal:org.springframework.security.core.userdetails . ciiwsLoggingSecurityEventListener:event = InteractiveAuthenticationSuccessEvent username = cgrant remoteAddress = 0:0:0:0:0:0:0:1 sessionId = c7363d39-28ff-44e3-83a9-d463f2f371e5 RequestAwareAuthenticationSuccessHandler:使用默认Url:/ ossweb.DefaultRedirectStrategy:重定向to'/ myapp /'wcHttpSessionSecurityContextRepository:SecurityContext'org.springframework.security.core.context.SecurityContextImpl@5e87c6c5:身份验证:org.springframework.security.authentication.UsernamePasswordAuthenticationToken@5e87c6c5:Principal:org.springframework.security.core.userdetails .User @ fb03e089:用户名:cgrant; ..........存储到HttpSession:'org.springframework.session.web.http.SessionRepositoryFilter$SessionRepositoryRequestWrapper$HttpSessionWrapper@5d2baa59 sswcSecurityContextPersistenceFilter:SecurityContextHolder现已清除,因为请求处理已完成osswsHttpSessionEventPublisher:发布事件:org .springframework.security.web.session.HttpSessionCreatedEvent [source =org.springframework.session.web.http.ExpiringSessionHttpSession@287a471f] ..... ..... ossecurity.web.FilterChainProxy:/ at position 1 of 11在额外的过滤链中;触发过滤器:'WebAsyncManagerIntegrationFilter'o.s.security.web.FilterChainProxy:/位于附加过滤器链中的位置2的11;触发过滤器:'SecurityContextPersistenceFilter'w.c.HttpSessionSecurityContextRepository:HttpSession返回了SPRING_SECURITY_CONTEXT的空对象w.c.HttpSessionSecurityContextRepository:HttpSession中没有可用的SecurityContext:org.springframework.session.web.http.SessionRepositoryFilter $ SessionConpositoryRequestWrapper $ HttpSessionWrapper @ 8d3c315 . 将创建一个新的 . o.s.security.web.FilterChainProxy:/在附加过滤器链中的位置3 of 11;触发过滤器:'HeaderWriterFilter'osswheader.writers.HstsHeaderWriter:不注入HSTS头,因为它与requestMatcher org.springframework.security.web.header.writers.HstsHeaderWriter$SecureRequestMatcher@57f4e063 ossecurity.web.FilterChainProxy:/ at不匹配位置4的11位于额外的过滤链中;触发过滤器:'LogoutFilter'o.s.s.w.u.matcher.AntPathRequestMatcher:检查请求匹配:'/';对于'/ logout'o.s.security.web.FilterChainProxy:/在第5位的11位于额外的过滤链中;触发过滤器:'UsernamePasswordAuthenticationFilter'os.s.s.w.u.matcher.AntPathRequestMatcher:请求'GET /'与'POST / login / auth o.s.security.web.FilterChainProxy:/在第6位的第11位上的其他过滤器链不匹配;触发过滤器:'RequestCacheAwareFilter'o.s.security.web.FilterChainProxy:/在第7位,共11个附加过滤器链;触发过滤器:'SecurityContextHolderAwareRequestFilter'o.s.security.web.FilterChainProxy:/在其他过滤器链中的位置8 of 11;使用匿名标记:'org.springframework.security.authentication.AnonymousAuthenticationToken@6fa843a8:Principal:anonymousUser;'anonymousAuthenticationFilter'on.As.证书:[受保护];认证:真实;详细信息:org.springframework.security.web.authentication.WebAuthenticationDetails@fffd3270:RemoteIpAddress:0:0:0:0:0:0:0:1; SessionId:2192be54-aee1-4249-98ba-01a65a401830;授权机构:ROLE_ANONYMOUS'o.s.security.web.FilterChainProxy:/在第9位,共11个额外的过滤链;触发过滤器:'SessionManagementFilter'o.s.security.web.FilterChainProxy:/在第10位的11位于附加过滤器链中;触发过滤器:'ExceptionTranslationFilter'o.s.security.web.FilterChainProxy:/在11位11的附加过滤器链中; filter Filter:'FilterSecurityInterceptor'..... ..... o.s.s.w.a.i.FilterSecurityInterceptor:Secure object:FilterInvocation:URL:/;属性:[authenticated] o.s.s.w.a.i.FilterSecurityInterceptor:以前经过身份验证:org.springframework.security.authentication.AnonymousAuthenticationToken@6fa843a8:Principal:anonymousUser;证书:[受保护];认证:真实;详细信息:org.springframework.security.web.authentication.WebAuthenticationDetails@fffd3270:RemoteIpAddress:0:0:0:0:0:0:0:1; SessionId:2192be54-aee1-4249-98ba-01a65a401830;授权机构:ROLE_ANONYMOUS o.s.s.access.vote.AffirmativeBased:Voter:org.springframework.security.web.access.expression.WebExpressionVoter@1023c8f1,返回:-1 o.s.s.w.a.ExceptionTranslationFilter:访问被拒绝(用户是匿名的);重定向到身份验证入口点

2 回答

  • 2

    身份验证成功后,Spring Security会破坏上一个会话并创建一个认证设置为true的新会话 .

    因此,您在身份验证之前放入Session的Context Object在身份验证后将无法使用,因为之前的会话将被销毁 .

    该战略被称为 Session Fixation . 你可以Read Here更多关于这一点 .

    因此,如果要在身份验证后获取Context,请使用:

    Object contextObject = org.springframework.security.core.context.SecurityContextHolder.getContext();
    

    Spring Security Chain Filter in web.xml:

    <filter-mapping>
          <filter-name>springSecurityFilterChain</filter-name>
          <url-pattern>/*</url-pattern>
          <dispatcher>REQUEST</dispatcher>
          <dispatcher>FORWARD</dispatcher>
        </filter-mapping>
    
  • 1

    我找到了这个问题的实际原因 . 我使用的是Spring-session-1.2.2和hazelcast-3.5.4 . 迁移后,Spring会话无法保存会话 . 我不知道它是否是淡褐色问题或 Spring 季 Session 问题 . 出于测试目的,我使用了MapSessionRepository和@EnableSpringHttpSession,一切正常 . 在调试spring-session库时遇到了痛苦的经历,根据我的经验,我建议在使用任何存储进行会话复制之前首先使用MapSessionRepository进行测试,这将确保您的应用程序配置与spring-session一起工作,之后转移到使用第三方会话存储库 .

相关问题