首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >并行流与SpringSecurity

并行流与SpringSecurity
EN

Stack Overflow用户
提问于 2022-06-24 13:06:06
回答 1查看 146关注 0票数 0

我有以下代码块:

代码语言:javascript
复制
myList.parallelStream().forEach(item -> {
  //this external api call will use the current
  //spring security to populate the headers with values
  //that is extracted from jwt token

  var response = externalApi.getFoo(item.getAttribute());

  ..do something..

});

问题是,SecurityContext不会从一个线程传递到另一个线程。当获得身份验证主体时,我得到空指针。有正确的方法吗?

它不涉及将SecurityContextHolder的策略设置为MODE_INHERITABLETHREADLOCAL?如果有多个用户访问该服务,我相信这会导致安全问题。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-06-24 13:27:53

我只需将身份验证信息设置为每个线程。你觉得这种方法有什么问题吗?

代码语言:javascript
复制
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
myList.parallelStream().forEach(item -> {
  SecurityContextHolder.getContext().setAuthentication(authentication);
  var response = externalApi.getFoo(item.getAttribute());
  SecurityContextHolder.getContext().setAuthentication(null);

  ..do something..

});
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72744563

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档