Java Spring安全性与EJB

Java Spring安全性与EJB,java,spring-security,ejb,cdi,Java,Spring Security,Ejb,Cdi,我正试图在EJB应用程序中通过spring security建立一个角色用户身份验证系统。 我实现了UserDetailsService,如下所示: @Stateless public class UserDetailsServiceImpl implements UserDetailsService { @Inject private UserDAO userDao; ... } 用户道: @Stateless public class UserDAOImpl implements Use

我正试图在EJB应用程序中通过spring security建立一个角色用户身份验证系统。 我实现了UserDetailsService,如下所示:

@Stateless
public class UserDetailsServiceImpl implements UserDetailsService {

@Inject
private UserDAO userDao;
...
}
用户道:

@Stateless
public class UserDAOImpl implements UserDAO{

@PersistenceContext(name="persistence-unit-name")
private EntityManager em;
...
}
安全配置:

@Configuration
@EnableWebSecurity
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {

@Inject
private UserDetailsService userDetailsService;
...
}
SecurityWebApplicationInitializer:

public class SecurityWebApplicationInitializer extends 
AbstractSecurityWebApplicationInitializer {

public SecurityWebApplicationInitializer() {
    super(SecurityConfiguration.class);
  }
}
部署项目时,我有以下例外情况:

GRAVE: Exception lors de l'envoi de l'évènement contexte initialisé (context initialized) à l'instance de classe d'écoute (listener) org.springframework.web.context.ContextLoaderListener
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'securityConfiguration': Unsatisfied dependency expressed through field 'userDetailsService'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.security.core.userdetails.UserDetailsService' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@javax.inject.Inject()}
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:588)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:366)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1225)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:552)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:759)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:866)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:542)
    at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:444)
    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:326)
    at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:107)
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4725)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5189)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1403)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1393)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:748)
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.security.core.userdetails.UserDetailsService' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@javax.inject.Inject()}
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1474)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1102)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1064)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:585)
    ... 24 more
为什么不能将UserDetailsServiceImpl注入SecurityConfiguration?有什么办法解决这个问题吗?
谢谢

因为
UserDetailsServiceImpl
没有声明为Springbean
@Stateless
不适用于Spring,您需要
@Component
或其他组件。我尝试在
UserDetailsServiceImpl
中使用
@Component
并在
SecurityConfiguration
中使用
@autowired
自动连接它,但仍然得到相同的异常。有没有自动连接我的服务的想法?您确定吗?如果在使用Spring注释对其进行注释后出现相同的异常,则可能存在一些组件扫描问题。在创建应用程序上下文时,您是否扫描了Springbean的正确包?顺便说一下,您的
UserDaoImpl
也需要spring注释,因为您正在注入它的接口,因为
UserDetailsServiceImpl
没有声明为Springbean
@Stateless
不适用于Spring,您需要
@Component
或其他组件。我尝试在
UserDetailsServiceImpl
中使用
@Component
并在
SecurityConfiguration
中使用
@autowired
自动连接它,但仍然得到相同的异常。有没有自动连接我的服务的想法?您确定吗?如果在使用Spring注释对其进行注释后出现相同的异常,则可能存在一些组件扫描问题。在创建应用程序上下文时,您是否扫描了Springbean的正确包?哦,顺便说一下,您的
UserDaoImpl
也需要spring注释,因为您正在注入它的接口