【问题标题】:SpringSecurityContext is not passed when app is working in pods GKE应用程序在 Pod GKE 中工作时未传递 SpringSecurityContext
【发布时间】:2022-01-25 12:00:28
【问题描述】:

我需要在 servlet 过滤器中获取 UserDetails。所以我像这样从 SecurityContextHolder.getContext().getAuthentication() 得到它-

  @Override
  public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws ServletException, IOException {
     UserDetails userDetails = (UserDetails) SecurityContextHolder.getContext().getAuthentication()
        .getPrincipal();
...
}

本地用户是从安全上下文中获取的,但是在将应用程序部署到 GKE 时,SecurityContextHolder.getContext().getAuthentication() 为空。

我尝试像这样将 SecurityContextHolder 配置为 MODE_INHERITABLETHREADLOCAL,但没有帮助

@Configuration
public class SecurityContextHolderConfig {
  public SecurityContextHolderConfig() {
    SecurityContextHolder.setStrategyName(SecurityContextHolder.MODE_INHERITABLETHREADLOCAL);
  }
}

仍然没有将 SecurityContext 传递给 pod 中的过滤器。 (应用打包为WAR) 配置 - 带有 JWT 身份验证的 Spring oauth2-client(在标头中)

【问题讨论】:

  • 我不认为改变策略是要走的路。你的安全配置是什么?您确定无论您的身份验证是什么,它都会到达您的应用程序吗?像 cookie、标题?
  • 它是一个带有 JWT 身份验证的 Spring oauth2-client(在标头中)。它在本地运行良好。这就是它令人困惑的原因。
  • 我试图从标头中获取身份验证 JWT,但标头也被删除(在 GKE 中),本地一切正常。
  • @ArtemEduardov 您使用反向代理还是负载均衡器?也许它会删除标题?

标签: spring-boot spring-security kubernetes-pod


【解决方案1】:

这对我有用:

 @Override
  public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws ServletException, IOException {

    HttpServletRequest httpServletRequest =
    (HttpServletRequest) servletRequest;

    Principal userPrincipal = httpServletRequest.getUserPrincipal();

【讨论】:

    猜你喜欢
    • 2020-11-13
    • 2019-10-05
    • 1970-01-01
    • 2021-11-15
    • 2019-06-25
    • 2020-01-28
    • 1970-01-01
    • 1970-01-01
    • 2019-08-25
    相关资源
    最近更新 更多