2016-11-19 2 views
-1

私はSpring MVCプロジェクトを作成するためにIntelliJでSpringブートを使用しています。 私は、ユーザーの認証にSpringセキュリティを使用しています。私はガイドhereを見つけた。ここで Springが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で定義されている中で、これらの線があります。次のauthenticationManagerに豆のアイコンがあり、IntelliJので

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

をこれをクリックすると、AuthenticationManagerのBean定義に移動します。

私がコンパイルしようとしたときしかし、春は私に、このエラーを与える:ときのIntelliJができ

​​

はなぜ、Beanを見つけることができません春?これをどうやって解決するのですか?

私の設定のほとんどは、Springブート自動設定で行われています。私は@Qualifier( "authenticationManager")を追加しようとしましたが、これはうまくいきませんでした。

答えて

0

component-scanに問題があると思います。あなたができることはsecurityConfig.xmlで、以下のようにbean 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

関連する問題