복붙노트

[SPRING] 수동으로 AuthenticationManager 인스턴스 가져 오기

SPRING

수동으로 AuthenticationManager 인스턴스 가져 오기

나는 아래 구현하려고하지만 내 authenticationManager 인스턴스가 아래 예외를 throw하고 autowired되지 않습니다. Spring 인스턴스를 수동으로 어떻게 가져 옵니까? 스프링 컨트롤러를 사용하지 않고 JSF 요청 범위 Bean을 사용하고 있습니다. 컨테이너가 authenticationManager를 autowire하려고 시도 할 때 런타임에 예외가 발생합니다. requestCache는 정상적으로 제공됩니다. 내가 왜 두 가지 경우가 있는지 이해가 안돼.

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);
    }

}

해결법

  1. ==============================

    1.먼저, AuthenticationManager에 명시적인 bean 이름을 제공하십시오.

    먼저, AuthenticationManager에 명시적인 bean 이름을 제공하십시오.

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

    둘째, 자동 배선시 한정자를 사용합니다.

    @Autowired
    @Qualifier("authenticationManager")
    protected AuthenticationManager authenticationManager;
    
  2. from https://stackoverflow.com/questions/5417509/get-instance-of-authenticationmanager-manually by cc-by-sa and MIT license