[英]Autowire doesn't work for custom UserDetailsService in Spring Boot
Spring啟動應用程序啟動,tomcat運行,然后在最終死亡之前拋出錯誤。
運行我的應用程序給我這個Autowired
錯誤:
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]
我的安全配置:
@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:
@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
嘗試了你的項目,錯誤實際上是不同的:
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 。 解決方案:更換
@Autowired
private MyUserDetailsService userDetailsService;
同
@Autowired
private UserDetailsService userDetailsService;
您的解決方案(刪除@Transactional
)的作品,因為沒有@Transactional
沒有理由春天來包裝MyUserDetailsService
在代理。
從MyUserDetailsService
刪除@Transactional
工作。 不知道為什么。 如果有人有解釋,請發帖,我會將其標記為答案。
仔細檢查,這確實是正確的,如果你想自己檢查,請查看提交840f0753bdca1592d9070c74bc87f30e8e2f87a7
作為上面列出的存儲庫,並進行測試。
並且停止低估這個答案,因為你認為這是不正確的。
將@ComponentScan
添加到WebSecurityConfig。
由於組件掃描 ,它應該在MyUserDetailsService中連接,因為它是用@Service
注釋的。
包名稱可以作為參數傳遞給注釋,以設置組件掃描的范圍,例如:
@ComponentScan("com.company.team")
請在主配置類上添加注釋: @ComponentScan("")
。
如果要為特定目錄創建bean / service,那么可以在@ComponentScan("package.to.scan")
提供包路徑
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.