我試圖實現下面,但我的authenticationManager實例拋出下面的異常並且不是自動裝配的。我如何從Spring手動獲取它的實例?我沒有使用彈簧控制器,我使用JSF請求作用域bean。在容器嘗試自動裝載authenticationManager時,我在運行時遇到以下異常。 requestCache正常。我不明白爲什麼我有兩個實例...手動獲取AuthenticationManager的實例
配置:
<authentication-manager>
<authentication-provider user-service-ref="userManager">
<password-encoder ref="passwordEncoder" />
</authentication-provider>
</authentication-manager>
自動裝配Autowired依賴注入失敗;嵌套異常是org.springframework.beans.factory.BeanCreationException:無法自動裝入字段:protected 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 2011-03-24 10:33:40
@skaffman感謝您的回覆,我更新了一些問題,並提供了一些更多信息。之前發佈的內容不正確,不爲空。容器說,我有兩個身份驗證管理器的實例,但我不知道如何。我也粘貼了上面的配置,有什麼想法?我只是想在成功註冊後進行自動登錄。我是否需要使用我的userManager自動登錄? userManager沒有一個方法,需要一個UsernamePasswordAuthenticationToken – c12 2011-03-24 10:53:20