简体   繁体   English

使用Spring Security自动连接依赖注入

[英]Autowired Dependency Injection with Spring Security

I have a spring webapp with annotation driven configuration. 我有一个带有注释驱动配置的spring webapp。

All Controllers, Repositories are autowired. 所有控制器,存储库都是自动装配的。

When integrating Spring Security I defined a separate security-app.xml. 在集成Spring Security时,我定义了一个单独的security-app.xml。 I created a Service called LoginUserService which implements UserDetailsService. 我创建了一个名为LoginUserService的服务,它实现了UserDetailsS​​ervice。 Now the method loadUserByUsername() method of this class gets invoked for authentication. 现在调用此类的方法loadUserByUsername()方法进行身份验证。

This class has an autowired dependency for UserRepository. 此类具有UserRepository的自动连接依赖项。 Now this autowired dependency turns out to be null. 现在这个自动连接的依赖关系变成了null。 To fix this I enable annotation driven configuration and add the package name for the repository class in component scan configuration. 为了解决这个问题,我启用了注释驱动配置,并在组件扫描配置中添加了存储库类的包名称。

This solution is also discussed here spring security with custom user details 此解决方案还在此处讨论了Spring安全性和自定义用户详细信息

But now the problem is that the UserRepository has an EntityManager field with @PersistenceContext annotation. 但现在的问题是UserRepository有一个带有@PersistenceContext注释的EntityManager字段。 For the spring security configuration it is able to locate the UserRepository but not able to locate the entity manager. 对于spring安全配置,它能够找到UserRepository但无法找到实体管理器。 Should I create a new EntityManagerFactory here? 我应该在这里创建一个新的EntityManagerFactory吗? I guess that will create two persistence units in my application? 我想这将在我的应用程序中创建两个持久性单元?

How can I inject an autowired dependency to UserRepository created with the original servlet xml? 如何为使用原始servlet xml创建的UserRepository注入自动连接的依赖项?

Update 更新

This is briefly discussed here: https://stackoverflow.com/a/7078395/161628 这里简要讨论一下: https//stackoverflow.com/a/7078395/161628

But I guess a canonical detailed answer will be more useful to me. 但我想一个规范的详细答案对我来说会更有用。

Update 更新

How about using ApplicationContext to get the UserRepository at runtime? 如何使用ApplicationContext在运行时获取UserRepository?

if (userRepository == null) {
    userRepository = ApplicationContextProvider.getApplicatonContext().getBean(UserRepository.class);
}

Why is Spring's ApplicationContext.getBean considered bad? 为什么Spring的ApplicationContext.getBean被认为是坏的?

EDIT: Beans that you declare in your config for your DispatcherServlet are not going to be available to any beans you declare or component scan in your contextConfigLocation config files. 编辑:您在DispatcherServlet的配置中声明的Bean将不会对您在contextConfigLocation配置文件中声明或组件扫描的任何bean可用。 So in this case, if you're setting up your JPA config in your config file that you load for your DispatcherServlet there is no way to wire that in to beans your declare in your security config. 因此,在这种情况下,如果您在为DispatcherServlet加载的配置文件中设置JPA配置,则无法将其连接到您在安全配置中声明的bean。 You need to move any "core" bean config like that (datasource config, db connection pool config, JPA/Hibernate config, repository/service component scanning, etc.) into a config file that you load via the contextConfigLocation. 您需要将任何“核心”bean配置(数据源配置,数据库连接池配置,JPA / Hibernate配置,存储库/服务组件扫描等)移动到您通过contextConfigLocation加载的配置文件中。 Then that stuff will be available both to your security beans and your MVC beans. 然后,这些东西将可用于您的安全bean和您的MVC bean。 I think generally the idea is to only load MVC specific beans in your DispatcherServlet config (eg Controllers, views, request handlers, request scoped beans, etc.). 我认为通常的想法是只在DispatcherServlet配置中加载MVC特定的bean(例如控制器,视图,请求处理程序,请求范围的bean等)。 That way you ensure you have a clean separation between MVC code and non-MVC code, with only a one-way dependency from the MVC code to the "core" code, and no dependencies on MVC code in your "core" code. 这样,您可以确保在MVC代码和非MVC代码之间实现清晰的分离,只需要从MVC代码到“核心”代码的单向依赖关系,并且在“核心”代码中不依赖于MVC代码。 This helps make your code more modular, and makes it easier to reuse your "core" code in other ways, specifically in unit tests. 这有助于使您的代码更加模块化,并且可以更轻松地以其他方式重用“核心”代码,特别是在单元测试中。

(Original comment text was asking about how the security config is loaded, if it's in the contextConfigLocation or somewhere else.) (原始评论文本询问如何加载安全配置,如果它在contextConfigLocation或其他地方。)

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

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