Java 具有xml配置NoSuchBeanDefensionException的Spring安全性没有AuthenticationManager类型的合格bean
我对春天很陌生。我在启动我的spring security时遇到了问题。 以下是我的XML和类: web.xml UserDetailsServiceImplJava 具有xml配置NoSuchBeanDefensionException的Spring安全性没有AuthenticationManager类型的合格bean,java,spring,hibernate,spring-mvc,spring-security,Java,Spring,Hibernate,Spring Mvc,Spring Security,我对春天很陌生。我在启动我的spring security时遇到了问题。 以下是我的XML和类: web.xml UserDetailsServiceImpl @Service public class UsersDetailsServiceImpl implements UserDetailsService { public UsersDetailsServiceImpl() { } @Override @Transactional(readOnly =
@Service
public class UsersDetailsServiceImpl implements UserDetailsService {
public UsersDetailsServiceImpl() {
}
@Override
@Transactional(readOnly = true)
public UserDetails loadUserByUsername(String email) {
UsersDao usersDao = new UsersDaoImpl();
Users user = usersDao.getByEmail(email);
Set<GrantedAuthority> grantedAuthorities = new HashSet<>();
grantedAuthorities.add(new SimpleGrantedAuthority(user.getRoles().getName()));
return new org.springframework.security.core.userdetails.
User(user.getEmail(), user.getPassword(), grantedAuthorities);
}
}
我做错了什么?我在谷歌上搜索,找不到任何适合我的解决方案。请帮忙。查看日志中的这一行: 创建名为“securityServiceImpl”的bean时出错 我想这是因为你实现了构造函数 看看这部分:
private AuthenticationManager authenticationManager;
private UserDetailsService userDetailsServiceImpl;
@Autowired
public SecurityServiceImpl(AuthenticationManager authenticationManager, UserDetailsService userDetailsServiceImpl) {
this.authenticationManager = authenticationManager;
this.userDetailsServiceImpl = userDetailsServiceImpl;
}
将其更改为:
@Autowired
private AuthenticationManager authenticationManager;
@Autowired
private UserDetailsService userDetailsServiceImpl;
public SecurityServiceImpl() {
}
然后再测试一次。我希望这对您有所帮助。不确定,但请您试试。。。在spring-security.xml中。也许我错了。。但这是一个快速检查
<http auto-config="true">
<intercept-url pattern="/account"
access="isAuthenticated()"/>
<intercept-url pattern="/adminPanel/**"
access="hasAnyRole('ROLE_ADMIN')"/>
<form-login login-processing-url="/login"
login-page="/login" default-target-url="/"
authentication-failure-url="/"
username-parameter="email" password-parameter="password"/>
<logout logout-success-url="/user/login?logout"/>
<csrf disabled="true"/>
</http>
<authentication-manager alias="authenticationManager">
<authentication-provider user-service-ref="userDetailsServiceImpl">
<password-encoder ref="encoder"/>
</authentication-provider>
</authentication-manager>
<beans:bean id="userDetailsServiceImpl"
class="com.mobilecompany.services.impl.UsersDetailsServiceImpl"/>
<beans:bean id="encoder"
class="org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder">
<beans:constructor-arg name="strength" value="11"/>
</beans:bean>
</beans:beans>
@Service
public class SecurityServiceImpl implements SecurityService {
private AuthenticationManager authenticationManager;
private UserDetailsService userDetailsServiceImpl;
@Autowired
public SecurityServiceImpl(AuthenticationManager authenticationManager, UserDetailsService userDetailsServiceImpl) {
this.authenticationManager = authenticationManager;
this.userDetailsServiceImpl = userDetailsServiceImpl;
}
@Override
public String findLoggedInEmail() {
Object userDetails = SecurityContextHolder.getContext().getAuthentication()
.getPrincipal();
if(userDetails instanceof UserDetails)
return ((UserDetails) userDetails).getUsername();
return null;
}
@Override
public void autoLogin(String email, String parole) {
UserDetails userDetails = userDetailsServiceImpl.loadUserByUsername(email);
UsernamePasswordAuthenticationToken authenticationToken =
new UsernamePasswordAuthenticationToken(userDetails, parole, userDetails.getAuthorities());
authenticationManager.authenticate(authenticationToken);
if(authenticationToken.isAuthenticated()){
SecurityContextHolder.getContext().setAuthentication(authenticationToken);
}
}
}
@Service
public class UsersDetailsServiceImpl implements UserDetailsService {
public UsersDetailsServiceImpl() {
}
@Override
@Transactional(readOnly = true)
public UserDetails loadUserByUsername(String email) {
UsersDao usersDao = new UsersDaoImpl();
Users user = usersDao.getByEmail(email);
Set<GrantedAuthority> grantedAuthorities = new HashSet<>();
grantedAuthorities.add(new SimpleGrantedAuthority(user.getRoles().getName()));
return new org.springframework.security.core.userdetails.
User(user.getEmail(), user.getPassword(), grantedAuthorities);
}
}
17-Jul-2018 02:44:12.613 SEVERE [RMI TCP Connection(3)-127.0.0.1] org.springframework.web.context.ContextLoader.initWebApplicationContext Context initialization failed
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'securityServiceImpl' defined in file [D:\Java Projects\javaschool\target\javaschool\WEB-INF\classes\com\mobilecompany\services\impl\SecurityServiceImpl.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.security.authentication.AuthenticationManager' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:729)
at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:192)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1270)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1127)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:545)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:502)
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:312)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:310)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:200)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:760)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:868)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:549)
at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:409)
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:291)
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:103)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4900)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5363)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:145)
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:755)
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:731)
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:717)
at org.apache.catalina.startup.HostConfig.manageApp(HostConfig.java:1730)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:564)
at org.apache.tomcat.util.modeler.BaseModelMBean.invoke(BaseModelMBean.java:300)
at java.management/com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:809)
at java.management/com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:801)
at org.apache.catalina.mbeans.MBeanFactory.createStandardContext(MBeanFactory.java:484)
at org.apache.catalina.mbeans.MBeanFactory.createStandardContext(MBeanFactory.java:433)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:564)
at org.apache.tomcat.util.modeler.BaseModelMBean.invoke(BaseModelMBean.java:300)
at java.management/com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:809)
at java.management/com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:801)
at java.management.rmi/javax.management.remote.rmi.RMIConnectionImpl.doOperation(RMIConnectionImpl.java:1466)
at java.management.rmi/javax.management.remote.rmi.RMIConnectionImpl.access$300(RMIConnectionImpl.java:75)
at java.management.rmi/javax.management.remote.rmi.RMIConnectionImpl$PrivilegedOperation.run(RMIConnectionImpl.java:1307)
at java.management.rmi/javax.management.remote.rmi.RMIConnectionImpl.doPrivilegedOperation(RMIConnectionImpl.java:1399)
at java.management.rmi/javax.management.remote.rmi.RMIConnectionImpl.invoke(RMIConnectionImpl.java:827)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:564)
at java.rmi/sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:359)
at java.rmi/sun.rmi.transport.Transport$1.run(Transport.java:200)
at java.rmi/sun.rmi.transport.Transport$1.run(Transport.java:197)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at java.rmi/sun.rmi.transport.Transport.serviceCall(Transport.java:196)
at java.rmi/sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:562)
at java.rmi/sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:796)
at java.rmi/sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.lambda$run$0(TCPTransport.java:677)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at java.rmi/sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:676)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1135)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635)
at java.base/java.lang.Thread.run(Thread.java:844)
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.security.authentication.AuthenticationManager' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1509)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1104)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1065)
at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:815)
at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:721)
... 60 more
private AuthenticationManager authenticationManager;
private UserDetailsService userDetailsServiceImpl;
@Autowired
public SecurityServiceImpl(AuthenticationManager authenticationManager, UserDetailsService userDetailsServiceImpl) {
this.authenticationManager = authenticationManager;
this.userDetailsServiceImpl = userDetailsServiceImpl;
}
@Autowired
private AuthenticationManager authenticationManager;
@Autowired
private UserDetailsService userDetailsServiceImpl;
public SecurityServiceImpl() {
}