简体   繁体   中英

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

Field authMang in com.tabish.flightreservation.services.SecurityServiceImpl required a bean of type 'org.springframework.security.authentication.AuthenticationManager' that could not be found.

and

The injection point has the following annotations: - @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.

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. That's what solved my problem.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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