【发布时间】:2011-07-22 00:15:04
【问题描述】:
我正在尝试实现以下内容,但我的 authenticationManager 实例引发了以下异常并且没有自动装配。如何手动从 Spring 获取它的实例?我没有使用弹簧控制器,我使用的是 JSF 请求范围 bean。当容器尝试自动装配 authenticationManager 时,我在运行时收到以下异常。 requestCache 很好。我不明白为什么我有两个实例...
配置:
<authentication-manager>
<authentication-provider user-service-ref="userManager">
<password-encoder ref="passwordEncoder" />
</authentication-provider>
</authentication-manager>
自动装配依赖注入失败;嵌套异常是 org.springframework.beans.factory.BeanCreationException:无法自动装配字段:受保护的 org.springframework.security.authentication.AuthenticationManager com.dc.web.actions.SignUpDetail.authenticationManager;嵌套异常是 org.springframework.beans.factory.NoSuchBeanDefinitionException:没有定义类型 [org.springframework.security.authentication.AuthenticationManager] 的唯一 bean:预期单个匹配 bean 但找到 2:[org.springframework.security.authentication.ProviderManager #0,org.springframework.security.authenticationManager] javax.faces.webapp.FacesServlet.service(FacesServlet.java:325)
@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字段为空?requestCache有线正常吗? -
@skaffman 感谢您的回复,我用更多信息更新了我的问题。我之前发布的内容不正确,它不为空。容器说我有两个 authenticationManager 实例,但我不知道如何。我也在上面粘贴了我的配置,有什么想法吗?我只是想在成功注册后进行自动登录。我需要使用我的 userManager 来自动登录吗? userManager 没有采用 UsernamePasswordAuthenticationToken 的方法