我有以下代码块:
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?如果有多个用户访问该服务,我相信这会导致安全问题。
发布于 2022-06-24 13:27:53
我只需将身份验证信息设置为每个线程。你觉得这种方法有什么问题吗?
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..
});https://stackoverflow.com/questions/72744563
复制相似问题