繁体   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