spring-security - Spring 依赖项未嵌入 HTTPSessionListener
问题描述
我想通过下面的链接在我的自定义 HTTPSessionListener 实例中注入 spring 依赖项。如果我从 web.xml 中删除条目 sessionDestroyed 方法不会被调用,但是当 web.xml 方法中的条目被调用但 XUSerMgr 的依赖项是无效的。
如何使用 Spring 将依赖项注入 HttpSessionListener?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>
解决方案
推荐阅读
- python - How to print one row based on the date with python pandas?
- powershell - Powershell: Disable-ADAccount : 服务器不愿意处理请求
- apache-spark - Spark-nlp:无法从 pyspark 中的磁盘加载预训练的识别实体模型
- scala - How to make it so that dependent types in two different traits are recognized as the same type
- installation - UI Style Capabilities and Alternatives for InstallShield
- php - 转储或 dd laravel 在结果前添加字符时出错
- c - 仅使用整数寄存器将浮点值从 C 程序传递到汇编程序级程序?
- .net-core - csproj
CopyToOutputDirectory="PreserveNewest" 无效 - regex - 需要帮助以了解 1 有效而 1 无效的类似 RegExp 用法之间的区别
- python - 使用 Python 的 aiohttp 发出 Web 请求,出现浏览器过时错误?