_朝汐 2022-07-09 21:49 采纳率: 100%
浏览 38
已结题

Spring Security中 authenticationToken 会复用吗

Spring Security + 线程池, 如果两个请求都调用了某个线程,那么后一个请求会复用 保存的授权信息 authenticationToken 吗?
源码中使用到了TreadLocal

比如:请求1用到了线程1,保存了authenticationToken信息,请求之行结束后,线程空闲了一段时间又被请求二使用,请求二用的authenticationToken是否是线程1的?Spring Security 有没有提供请求请求结束后清除线程内保存的的authenticationToken的功能呢

@Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain)
            throws ServletException, IOException
    {
        LoginUser loginUser = tokenService.getLoginUser(request);
        if (StringUtils.isNotNull(loginUser) && StringUtils.isNull(SecurityUtils.getAuthentication()))
        {
            tokenService.verifyToken(loginUser);
            UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(loginUser, null, loginUser.getAuthorities());
            authenticationToken.setDetails(new WebAuthenticationDetailsSource().buildDetails(request));
            SecurityContextHolder.getContext().setAuthentication(authenticationToken);
        }
        chain.doFilter(request, response);
    }

  • 写回答

2条回答 默认 最新

  • a1767028198 2022-07-09 22:26
    关注

    threadLocal肯定是每个线程存自己的,但是认证信息可能是同一个,如果你没有禁用session的话,认证信息应该是会复用的

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 7月21日
  • 已采纳回答 7月13日
  • 修改了问题 7月10日
  • 修改了问题 7月10日
  • 展开全部

悬赏问题

  • ¥30 VMware 云桌面水印如何添加
  • ¥15 用ns3仿真出5G核心网网元
  • ¥15 matlab答疑 关于海上风电的爬坡事件检测
  • ¥88 python部署量化回测异常问题
  • ¥30 酬劳2w元求合作写文章
  • ¥15 在现有系统基础上增加功能
  • ¥15 远程桌面文档内容复制粘贴,格式会变化
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”