繁体   English   中英

Spring安全上下文和@Repository bean

[英]Spring security context and @Repository bean

@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实例与身份验证拦截器中放置的实例不同。 这很奇怪,看起来不可能。

在某些情况下,当您启动某个作业时,例如该作业无法访问请求,因此无法访问身份验证信息,但该作业仍在使用存储库。

如果创建新线程并再次从该线程访问存储库,则可能会出现问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM