【发布时间】:2020-04-18 00:49:58
【问题描述】:
我有 2 个方法,每个方法都在一个单独的 Spring Bean 中:
汽车服务:
PreAuthorize("someCheck(#carId)")
public List<Color> getCarColors(String carId) {
return this.getCar(carId).getColors().parallelStream()
.map(ColorService::getColor)
.collect(Collectors.toList());
}
色彩服务:
PreAuthorize ("someOtherCeck(#colorId)")
public Color getColor(String colorId) {
return this.colorRepository.findById(colorId);
}
这些方法只是为了举例,只是简单地解释问题的最简单方法。
第一次检查 (someCheck) 顺利通过,但第二次检查 (someOtherCheck) 抛出异常:
在 SecurityContext 中找不到 Authentication 对象
我知道parallelStream 使用多个线程,所以我在application.properties 中添加了以下行:
spring.security.strategy=MODE_INHERITABLETHREADLOCAL
但这并不能解决问题,SecurityContext 没有填充到新线程中,并且第二个 PreAuthorize 抱怨在 SecurityContext 中找不到任何身份验证。
【问题讨论】:
标签: java spring spring-boot spring-security java-stream