【问题标题】:An Authentication object was not found in the SecurityContext在 SecurityContext 中找不到 Authentication 对象
【发布时间】:2013-05-09 16:54:25
【问题描述】:
  1. 我有一个导出 Web 服务的应用程序,配置了 Spring Security SecurityFilterChain(其中包括 SecurityContextPersistenceFilter,其余部分都需要)。
  2. 我的应用程序还使用 Spring Security 来保护方法调用。

触发方法安全时出现以下错误:

org.springframework.security.authentication.AuthenticationCredentialsNotFoundException: An Authentication object was not found in the SecurityContext

第二部分需要SecurityContextHolder 中的Authentication,如org.springframework.security.access.intercept.AbstractSecurityInterceptor(第195 行)所示:

SecurityContextHolder.getContext().getAuthentication();

但是,SecurityContextPersistenceFilter 在触发方法调用之前将其删除,如 org.springframework.security.web.context.SecurityContextPersistenceFilter(第 84 行)

SecurityContextHolder.clearContext();

当方法调用被触发时,我该怎么做才能在SecurityContextHolder 中拥有这个对象?

提前谢谢你。

我正在使用 Spring Security 3.0.8-RELEASE

【问题讨论】:

    标签: spring authentication spring-security authorization security-context


    【解决方案1】:

    好的,我的应用程序放置在Apache CXF DOSGi 1.4 上以生成 REST 端点。 Apache CXF 拦截器会导致意外行为,并在完成请求处理之前调用 SecurityContextHolder.clearContext()

    有关此错误的更多信息可以找到here

    【讨论】:

      【解决方案2】:

      SecurityContextHolder.clearContext() 只会在请求处理完成后调用。所以通常你所有的应用逻辑代码都会在before这一行之前执行,完全没有问题。但是,如果您在代码中执行一些新线程,则可能会出现问题(默认情况下不会传播安全上下文)。如果这是您的情况,那么您可以try to force context propogation to child thread。如果您只使用一个线程,请确保您的所有代码都被 spring 安全过滤器链覆盖(可能是您有一些自定义过滤器执行 围绕 spring 安全过滤器链?)。

      【讨论】:

        猜你喜欢
        • 2013-05-18
        • 2016-03-11
        • 2022-01-22
        • 2013-03-08
        • 2014-04-04
        • 2016-11-12
        • 1970-01-01
        • 2019-10-28
        • 2013-09-20
        相关资源
        最近更新 更多