簡體   English   中英

Spring Boot 2.2.2 中的 AuthenticationManager 注入失敗

[英]AuthenticationManager injection failed in spring boot 2.2.2

我正在嘗試在我的 Web 應用程序中使用 Spring boot AuthenticationManager 類,同時我收到一個錯誤

com.tabish.flightreservation.services.SecurityServiceImpl 中的字段 authMang 需要一個無法找到的“org.springframework.security.authentication.AuthenticationManager”類型的 bean。

注入點有以下注釋: - @org.springframework.beans.factory.annotation.Autowired(required=true)

它要求我這樣做

行動:考慮在你的配置中定義一個“org.springframework.security.authentication.AuthenticationManager”類型的bean。

我的代碼是:

import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.stereotype.Service;

@Service
public class SecurityServiceImpl implements SecurityService {

    @Autowired
    UserDetailsService userDetailService;

    @Autowired
    AuthenticationManager authMang;

    @Override
    public boolean login(String username, String password) {
        // TODO Auto-generated method stub
        UserDetails userDetails = userDetailService.loadUserByUsername(username);

        UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(userDetails, password,
                userDetails.getAuthorities());

        authMang.authenticate(token);

        boolean result = token.isAuthenticated();

        //If result is successful then spring will not ask for auth again and again and will not display login page again
        if(result)
            SecurityContextHolder.getContext().setAuthentication(token);

        return result;
    }

}


正如@Ritesh 上面提到的。 這就是解決我的問題的原因。

@Bean 
@Override   
public AuthenticationManager authenticationManagerBean() throws Exception {          
    return super.authenticationManagerBean();   
} 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM