问题描述
我正在尝试实现以下内容,但是我的authenticationManager实例抛出以下异常,并且未自动接线.如何从Spring手动获取它的实例?我没有使用spring控制器,而是在使用JSF请求作用域bean.当容器尝试自动连接authenticationManager时,在运行时出现以下异常. requestCache很好.我不明白为什么我有两个实例...
I'm trying to implement the below, but my authenticationManager instance throws the below exception and is not autowired. How do I get an instance of it from Spring manually? I'm not using a spring controller, I'm using a JSF request scoped bean. I get the below exception at runtime when the container tries to autowire the authenticationManager. The requestCache comes in fine. I don't understand why I have two instances...
config:
<authentication-manager>
<authentication-provider user-service-ref="userManager">
<password-encoder ref="passwordEncoder" />
</authentication-provider>
</authentication-manager>
@Controller
public class SignupController
{
@Autowired
RequestCache requestCache;
@Autowired
protected AuthenticationManager authenticationManager;
@RequestMapping(value = "/account/signup/", method = RequestMethod.POST)
public String createNewUser(@ModelAttribute("user") User user, BindingResult result, HttpServletRequest request, HttpServletResponse response)
{
//After successfully Creating user
authenticateUserAndSetSession(user, request);
return "redirect:/home/";
}
private void authenticateUserAndSetSession(User user,
HttpServletRequest request)
{
UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(
user.getUsername(), user.getPassword());
// generate session if one doesn't exist
request.getSession();
token.setDetails(new WebAuthenticationDetails(request));
Authentication authenticatedUser = authenticationManager.authenticate(token);
SecurityContextHolder.getContext().setAuthentication(authenticatedUser);
}
}
推荐答案
首先,为您的AuthenticationManager
<authentication-manager alias="authenticationManager">
...
</authentication-manager>
第二,自动接线时请使用限定符:
Second, use qualifier when auto-wiring:
@Autowired
@Qualifier("authenticationManager")
protected AuthenticationManager authenticationManager;
这篇关于手动获取AuthenticationManager的实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!