简体   繁体   English

Spring安全上下文和@Repository bean

[英]Spring security context and @Repository bean

Is it safe to access to the Spring Security context from @Repository bean? @Repository bean访问Spring Security上下文是否安全?

Let us say that we have some @Repository : 让我们说我们有一些@Repository

public interface FooRep {
    Foo getFoo();
}

@Repository
public class FooRepImpl {    
    public Foo getFoo() {
       Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
       return (Foo)authentication.getDetails();
    }
}

It is wrapped into service layer: 它包装在服务层中:

public interface FooService {
     Foo getFoo();
}

@Service    
public class FooServiceImpl {
    @Autowired FooRep fooRep;

    public Foo getFoo() {
        return fooRep.getFoo();
    }
}

And let us say that this method is accessed from secured controller, something like that: 让我们说这个方法是从安全控制器访问的,就像这样:

@RestController
@Secured
public void FooController {
     @Autowired FooService fooSer;

     @RequestMapping("/foo");
     public Foo getFoo() {
         return fooSer.getFoo();
     }
}

This is very simplified example, but essential part of logic is here. 这是一个非常简化的示例,但此处的逻辑必不可少。

Please, do not ask me why do I need it and do not give me advices how to restructure this architecture. 拜托,不要问我为什么需要它,也不要给我建议如何重组该体系结构。

I only need to know, can it cause any issues related to multithread usage? 我只需要知道,它会引起与多线程使用相关的任何问题吗?

The question is arisen, because we have experienced cases when authentication.getDetails() contained Foo instance different from that which was placed there in the authentication interceptor. 之所以出现这个问题,是因为我们遇到过一些情况,当authentication.getDetails()包含的Foo实例与身份验证拦截器中放置的实例不同。 This is very weird and looks impossible. 这很奇怪,看起来不可能。

There is a case when you start eg a Job which has no access to Request so don't have access to auth info but the Job still uses the repository. 在某些情况下,当您启动某个作业时,例如该作业无法访问请求,因此无法访问身份验证信息,但该作业仍在使用存储库。

If you create a new thread and access the repository from the thread again could be an issue. 如果创建新线程并再次从该线程访问存储库,则可能会出现问题。

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

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