Java 在spring boot 2.2.2中,AuthenticationManager注入失败
我正在尝试在我的web应用程序中使用Spring boot AuthenticationManager类,但在执行此操作时,我遇到了一个错误: com.tabish.flightreservation.services.securityserviceinpl中的字段authMang需要找不到类型为“org.springframework.security.authentication.AuthenticationManager”的bean。 及 注入点具有以下注释: -@org.springframework.beans.factory.annotation.Autowired(required=true) 它要求我这样做 行动: 考虑在您的配置中定义“Or.Spring Frask.Security,Audio.AudioTimeMeal管理”类型的bean。< /强> 我的代码是:Java 在spring boot 2.2.2中,AuthenticationManager注入失败,java,spring-boot,spring-mvc,spring-security,Java,Spring Boot,Spring Mvc,Spring Security,我正在尝试在我的web应用程序中使用Spring boot AuthenticationManager类,但在执行此操作时,我遇到了一个错误: com.tabish.flightreservation.services.securityserviceinpl中的字段authMang需要找不到类型为“org.springframework.security.authentication.AuthenticationManager”的bean。 及 注入点具有以下注释: -@org.springfr
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;
}
}
您是如何配置AuthenticationManager的?你能一步一步地解释一下吗?因为我是这项技术的新手@ritesh你可以在线查看教程或指南。例如,如果您正在根据数据库对用户进行身份验证,则此选项可能会有所帮助。还要注意,在
websecurityConfigureAdapter
子类中,您可以按如下方式公开身份验证管理器bean:@bean@Override public AuthenticationManager authenticationManagerBean()抛出异常{return super.authenticationManagerBean();}
感谢ritesh的宝贵反馈,我已经在我的WebSecurity配置适配器中添加了这些行,并解决了我的错误