2016-11-19 138 views
-1

我在IntelliJ中使用Spring啓動來創建Spring MVC項目。 我使用Spring安全性來驗證用戶身份。我遵循指南發現hereSpring not autowiring AuthenticationManager

這裏是我SecurityService類:

@Service 
public class SecurityService { 

    @Autowired 
    private AuthenticationManager authenticationManager; 

    private UserDetailsService userDetailsService = new UserDetailsServiceImpl(); 

    public String findLoggedInUsername() { 
     Object userDetails = SecurityContextHolder.getContext(); 
     if (userDetails instanceof UserDetails) { 
      return ((UserDetails)userDetails).getUsername(); 
     } 
     return null; 
    } 

    public void autologin(String username, String password) { 
     UserDetails userDetails = userDetailsService.loadUserByUsername(username); 
     if(userDetails.getUsername().equals("NotFound")) { 
      return; 
     } 
     UsernamePasswordAuthenticationToken usernamePasswordAuthenticationToken = new UsernamePasswordAuthenticationToken(userDetails, password, userDetails.getAuthorities()); 

     authenticationManager.authenticate(usernamePasswordAuthenticationToken); 

     if (usernamePasswordAuthenticationToken.isAuthenticated()) { 
      SecurityContextHolder.getContext().setAuthentication(usernamePasswordAuthenticationToken); 
     } 
    } 
} 

然後,我有這些線路中securityConfig.xml定義:

<authentication-manager alias="authenticationManager"> 
    <authentication-provider user-service-ref="userDetailsServiceImpl"> 
     <password-encoder ref="encoder"></password-encoder> 
    </authentication-provider> 
</authentication-manager> 

在的IntelliJ,旁邊的AuthenticationManager有一個bean圖標當我點擊它時,它會指引我到AuthenticationManager的Bean定義。

然而,當我嘗試編譯,春天給了我這個錯誤:

​​

爲什麼不能春天找到豆,當能的IntelliJ?我該如何解決?

順便說一句,我的大部分配置都是用Spring Boot Autoconfiguration來完成的。我已經嘗試添加@Qualifier(「authenticationManager」),這沒有奏效。

回答

0

我認爲component-scan存在問題。你可以做的是在securityConfig.xml創建如下的豆SecurityService

<authentication-manager alias="authenticationManager"> 
    <authentication-provider user-service-ref="userDetailsServiceImpl"> 
     <password-encoder ref="encoder"></password-encoder> 
    </authentication-provider> 
</authentication-manager> 

<bean id="securityService" class="SecurityService" /> 
+0

嗨,對不起,我應該包括整個XML文件。我已經在我的配置文件中有這一行: Svava

相關問題