1
从@Repository
bean访问Spring Security上下文安全吗?Spring安全上下文和@Repository bean
让我们说,我们有一些@Repository
:
public interface FooRep {
Foo getFoo();
}
@Repository
public class FooRepImpl {
public Foo getFoo() {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
return (Foo)authentication.getDetails();
}
}
它包装成服务层:
public interface FooService {
Foo getFoo();
}
@Service
public class FooServiceImpl {
@Autowired FooRep fooRep;
public Foo getFoo() {
return fooRep.getFoo();
}
}
让我们说,这种方法是从安全控制器访问,类似的东西:
@RestController
@Secured
public void FooController {
@Autowired FooService fooSer;
@RequestMapping("/foo");
public Foo getFoo() {
return fooSer.getFoo();
}
}
这是一个非常简化的例子,但基本的pa逻辑的rt在这里。
请不要问我为什么需要它,不要给我建议如何重构这个架构。
我只需要知道,它是否会导致与多线程使用有关的任何问题?
问题出现了,因为我们遇到了authentication.getDetails()
包含的Foo
实例与验证拦截器中放置的实例不同的情况。这很奇怪,看起来不可能。
不好意思,你描述的场景如何与我提供的场景(Controller - > Service - > Repository)一致? – Andremoniy
在简单的情况下,它很好,但你问了关于多线程。我描述了潜在的问题。通常情况下,保留图层比较好,以便您可以从控制器获取身份验证,并将其传递到服务器,并作为参数传递给存储库,而不是直接访问。 – StanislavL