Get AuthenticationManager instance manually

I am trying to implement below, but my authenticationManager instance throws an exception below and is not auto-sensing. How can I get its instance from Spring manually? I do not use a Spring controller, I use a JSF request with a bean binding. At runtime, I get the following exception when the container tries autwire authenticationManager. RequestCache is delivered in order. I don’t understand why I have two instances ...

configurations:

<authentication-manager> <authentication-provider user-service-ref="userManager"> <password-encoder ref="passwordEncoder" /> </authentication-provider> </authentication-manager> 

Failed to inject auto-notified dependencies; The nested exception is org.springframework.beans.factory.BeanCreationException: Autofield failed: protected org.springframework.security.authentication.AuthenticationManager com.dc.web.actions.SignUpDetail.authenticationManager; The nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: a unique bean of type [org.springframework.security.authentication.AuthenticationManager] is not defined: expected one-time bean match, but found 2: [org.springframework.security.authentication. 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); } } 
+7
source share
1 answer

First specify an explicit bean for the AuthenticationManager

 <authentication-manager alias="authenticationManager"> ... </authentication-manager> 

Secondly, use the qualifier for automatic posting:

 @Autowired @Qualifier("authenticationManager") protected AuthenticationManager authenticationManager; 
+33
source

All Articles