简体   繁体   English

Spring Boot 2.2.2 中的 AuthenticationManager 注入失败

[英]AuthenticationManager injection failed in spring boot 2.2.2

I am trying to use Spring boot AuthenticationManager class in my web application while doing that i am getting an error that我正在尝试在我的 Web 应用程序中使用 Spring boot AuthenticationManager 类,同时我收到一个错误

Field authMang in com.tabish.flightreservation.services.SecurityServiceImpl required a bean of type 'org.springframework.security.authentication.AuthenticationManager' that could not be found. com.tabish.flightreservation.services.SecurityServiceImpl 中的字段 authMang 需要一个无法找到的“org.springframework.security.authentication.AuthenticationManager”类型的 bean。

and

The injection point has the following annotations: - @org.springframework.beans.factory.annotation.Autowired(required=true)注入点有以下注释: - @org.springframework.beans.factory.annotation.Autowired(required=true)

and it is asking me to do this它要求我这样做

Action: Consider defining a bean of type 'org.springframework.security.authentication.AuthenticationManager' in your configuration.行动:考虑在你的配置中定义一个“org.springframework.security.authentication.AuthenticationManager”类型的bean。

My code is :我的代码是:

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;
    }

}


As mentioned above by @Ritesh.正如@Ritesh 上面提到的。 That's what solved my problem.这就是解决我的问题的原因。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM