【发布时间】:2017-08-10 07:34:31
【问题描述】:
从@Repository bean 访问 Spring Security 上下文是否安全?
假设我们有一些@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();
}
}
这是一个非常简单的例子,但逻辑的基本部分在这里。
请不要问我为什么需要它,也不要给我建议如何重构这个架构。
我只需要知道,它会不会导致与多线程使用相关的任何问题?
问题出现了,因为我们遇到过authentication.getDetails() 包含的Foo 实例与身份验证拦截器中放置的实例不同的情况。这很奇怪,看起来不可能。
【问题讨论】:
标签: java spring spring-mvc spring-security