Issue
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;
}
}
Solution
As mentioned above by @Ritesh. That's what solved my problem.
@Bean
@Override
public AuthenticationManager authenticationManagerBean() throws Exception {
return super.authenticationManagerBean();
}
Answered By - Tabish Hafeez