Spring依赖关系未嵌入HTTPSessionListener中

时间:2019-04-18 13:13:42

标签: spring-security

我想通过下面的链接在我的自定义HTTPSessionListener实例中注入spring依赖项。 XUSerMgr的值为null。

How to inject dependencies into HttpSessionListener, using Spring? enter code here戒指

@Component
public class RangerHttpSessionListener implements HttpSessionListener,ApplicationContextAware {

    @Autowired
    XUserMgr xUserMgr;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        if (applicationContext instanceof WebApplicationContext) {
            ((WebApplicationContext) applicationContext).getServletContext().addListener(this);
        } else {
            //Either throw an exception or fail gracefully, up to you
            throw new RuntimeException("Must be inside a web application context");
        }
    }

    private static CopyOnWriteArrayList<HttpSession> listOfSession = new CopyOnWriteArrayList<HttpSession>();

    @Override
    public void sessionCreated(HttpSessionEvent event) {
        listOfSession.add(event.getSession());
    }

    @Override
    public void sessionDestroyed(HttpSessionEvent event) {
        if (!listOfSession.isEmpty()) {
            updateIsActiveStatusForAuthSession(event.getSession());
            listOfSession.remove(event.getSession());
        }
    }

    private void updateIsActiveStatusForAuthSession(HttpSession session) {
        xUserMgr.updateIsActiveStatusOfLoggedInUserWithHTTPSession(session.getId(),1);
    }

    public static CopyOnWriteArrayList<HttpSession> getActiveSessionOnServer() {
        return listOfSession;
    }

}

我的web.xml中有以下条目。 > <listener> <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class> </listener> <listener> <listener-class>org.apache.ranger.security.listener.RangerHttpSessionListener</listener-class> </listener>

0 个答案:

没有答案