spring – 手动获取AuthenticationManager的实例

我正在尝试实现以下,但我的authenticationManager实例抛出以下异常并且没有自动装配.如何从 Spring手动获取它的实例?我没有使用弹簧控制器,我正在使用JSF请求范围的bean.当容器尝试自动装配authenticationManager时,我在运行时获得以下异常. requestCache很好.我不明白为什么我有两个实例……

配置:

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

Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: protected org.springframework.security.authentication.AuthenticationManager com.dc.web.actions.SignUpDetail.authenticationManager; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [org.springframework.security.authentication.AuthenticationManager] is defined: expected single matching bean but found 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提供一个显式bean名称

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

二,自动布线时使用限定符:

@Autowired
@Qualifier("authenticationManager")
protected AuthenticationManager authenticationManager;
相关文章
相关标签/搜索