如何在自定义Spring安全登录成功处理程序中注入会话bean

时间:2011-06-30 18:41:29

标签: spring session login spring-security

我无法在自定义成功处理程序中注入spring会话bean:

@Component
public class CustomSavedRequestAwareAuthenticationSuccessHandler extends SavedRequestAwareAuthenticationSuccessHandler {

    @Resource
    private SessionController sessionController;

    @Override
    public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws ServletException, IOException {
        super.onAuthenticationSuccess(request, response, authentication);
        sessionController.setUtenteOnline();
    }

在sessionController上返回空指针异常。 提前谢谢

1 个答案:

答案 0 :(得分:1)

你的成功处理程序可能是一个单例,所以你只能可靠地将单例注入其中。您可以使用scoped dependencies解决此问题。基本上,它涉及Spring注入一个单独的动态代理,该代理管理从会话范围获取真实bean并将调用委托给它。