自动接线不适用于Spring Boot中的自定义UserDetailsService [英] Autowire doesn't work for custom UserDetailsService in Spring Boot
问题描述
spring boot应用程序启动,tomcat运行,然后在最终死亡之前引发错误.
The spring boot apps starts up, tomcat runs, and then it throws an error before finally dying.
运行我的应用程序会给我这个Autowired
错误:
Running my app gives me this Autowired
error:
2016-07-29 02:18:24.737 ERROR 44715 --- [ restartedMain] o.s.boot.SpringApplication : Application startup failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'webSecurityConfig': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private myapp.MyUserDetailsService myapp.WebSecurityConfig.userDetailsService; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [myapp.MyUserDetailsService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334) ~[spring-beans-4.2.7.RELEASE.jar:4.2.7.RELEASE]
详细信息
我的安全性配置:
Details
My Security configuration:
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/", "/register").permitAll()
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage("/")
.permitAll()
.and()
.logout()
.permitAll();
}
@Autowired
private MyUserDetailsService userDetailsService;
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(userDetailsService);
}
}
MyUserDetails:
MyUserDetails:
@Service
@Transactional
public class MyUserDetailsService implements UserDetailsService {
@Autowired
private UserRepository userRepository;
public MyUserDetailsService() {
super();
}
@Override
public UserDetails loadUserByUsername(String userName)
throws UsernameNotFoundException {
User user = userRepository.findByUserName(userName);
if (user == null) {
return null;
}
List<GrantedAuthority> auth = AuthorityUtils
.commaSeparatedStringToAuthorityList("ROLE_USER");
String password = user.getPassword();
return new org.springframework.security.core.userdetails.User(userName, password,
auth);
}
}
您可以在 Git Repo
推荐答案
我在提交3b9a6a2e
时尝试了您的项目,但错误实际上有所不同:
I've tried your project at commit 3b9a6a2e
, and the error is actually different:
org.springframework.beans.factory.BeanCreationException:创建名称为'webSecurityConfig'的bean时出错:自动连接依赖项的注入失败;嵌套的异常是org.springframework.beans.factory.BeanCreationException:无法自动连线字段:private myapp.MyUserDetailsService myapp.WebSecurityConfig.userDetailsService;嵌套的异常是java.lang.IllegalArgumentException:无法将myapp.MyUserDetailsService字段myapp.WebSecurityConfig.userDetailsService设置为com.sun.proxy.$ Proxy80
哪个让您的问题重复了 Spring无法创建bean .解决方案:替换
Which makes your question a duplicate of Spring can't create beans. Solution: replace
@Autowired
private MyUserDetailsService userDetailsService;
使用
@Autowired
private UserDetailsService userDetailsService;
您的解决方案(删除@Transactional
)之所以有效,是因为没有@Transactional
,Spring没有理由将MyUserDetailsService
包装在代理中.
Your solution (removing @Transactional
) works because without @Transactional
there is no reason for Spring to wrap MyUserDetailsService
in a proxy.
这篇关于自动接线不适用于Spring Boot中的自定义UserDetailsService的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!