Springboot項目監(jiān)聽器失效問題解決
1.使用springboot項目,現(xiàn)在有個需求是在添加或者修改某個菜單后,菜單會影響角色,角色影響用戶。所有受影響的用戶在要退出重新登錄。
自己實現(xiàn)的思路是這樣的:寫一個監(jiān)聽器,在收到某個特定的請求后,監(jiān)聽當前所有的用戶,如果是受影響的用戶,就銷毀session,讓重新登錄。
有了思路后,剛開始上網(wǎng)搜的是怎么在spring boot中添加監(jiān)聽:網(wǎng)上大部分的思路都一樣:使用@ServletComponentScan和一個實現(xiàn)了HttpSessionListener的方法就可以了。但是自己按照這個配置了后,一直不起作用。啟動時候能debug到這個自定義的監(jiān)聽里面,但是登錄后缺不能實現(xiàn)
sessionCreated()
package com.listener;import javax.servlet.annotation.WebListener;import javax.servlet.http.HttpSessionEvent;import javax.servlet.http.HttpSessionListener;/** * session監(jiān)聽器 * @author Administrator */@WebListenerpublic class SessionListener implements HttpSessionListener{ private int onlineCount = 0;//記錄session的數(shù)量 /** * session創(chuàng)建后執(zhí)行 */ @Override public void sessionCreated(HttpSessionEvent se) { onlineCount++; System.out.println('【HttpSessionListener監(jiān)聽器】 sessionCreated, onlineCount:' + onlineCount); se.getSession().getServletContext().setAttribute('onlineCount', onlineCount); } /** * session失效后執(zhí)行 */ @Override public void sessionDestroyed(HttpSessionEvent se) { if (onlineCount > 0) { onlineCount--; } System.out.println('【HttpSessionListener監(jiān)聽器】 sessionDestroyed, onlineCount:' + onlineCount); se.getSession().getServletContext().setAttribute('onlineCount', onlineCount); }}
還問了群里的大神幫忙看了下,也沒問題。剛開始懷疑是 不是登錄時候監(jiān)聽的HttpSession,因為實現(xiàn)的是HttpSessionListener,是需要有個發(fā)起的動作的.但是自己登錄時候也有httpSession。然后在自己的測試類里面進行測試,發(fā)現(xiàn)sesionId是存在的:
package com.sq.transportmanage.gateway.api.auth;import com.alibaba.fastjson.JSONObject;import com.sq.transportmanage.gateway.api.web.interceptor.AjaxResponse;import com.sq.transportmanage.gateway.api.web.interceptor.LoginoutListener;import com.sq.transportmanage.gateway.service.common.shiro.session.RedisSessionDAO;import com.sq.transportmanage.gateway.service.common.web.RestErrorCode;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.util.StringUtils;import org.springframework.web.bind.annotation.RequestBody;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.ResponseBody;import org.springframework.web.bind.annotation.RestController;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpSession;/** * @Author fanht * @Description * @Date 2020/3/5 下午6:46 * @Version 1.0 */@RestController@RequestMapping('/loginoutController')public class LoginoutController extends RedisSessionDAO{ private Logger logger = LoggerFactory.getLogger(this.getClass()); @RequestMapping('/userLoginOut') @ResponseBody public AjaxResponse userLoginOut(String userIds, HttpSession httpSession, HttpServletRequest request){ logger.info('httpSessionId' + httpSession.getId() + ',是否是session會話:' + request.getSession(false)); HttpSession session = request.getSession(); String loginName = (String) session.getAttribute('loginName'); logger.info('loginName:' + loginName); logger.info('調(diào)用退出接口并清除shiro緩存' + userIds); logger.info('獲取監(jiān)聽存取的信息' + JSONObject.toJSONString(LoginoutListener.sessionCount)); try { String userId[] = StringUtils.tokenizeToStringArray(userIds,','); for(int i = 0;i<userId.length;i++){clearRelativeSession(null,null,Integer.valueOf(userId[i])); } return AjaxResponse.success(null); } catch (NumberFormatException e) { e.printStackTrace(); logger.error('shiro退出異常' + e); return AjaxResponse.fail(RestErrorCode.UNKNOWN_ERROR); } } @Override public void clearRelativeSession(Integer permissionId, Integer roleId, Integer userId) { super.clearRelativeSession(null, null, userId); }}
是能夠打印sessionId的,也就是說session是存在不為空的。
然后想到我們項目里面用的是shiro,會不會是shiro重寫了session機制? 想到這個疑問,又上網(wǎng)搜索,最后通過這個發(fā)現(xiàn)是可以的
附上自己的配置:
自定義shiroSessionListener:
package com.sq.transportmanage.gateway.api.web.interceptor;import com.google.common.collect.Maps;import com.sq.transportmanage.gateway.service.common.shiro.session.RedisSessionDAO;import org.apache.shiro.session.Session;import org.apache.shiro.session.SessionListener;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import java.util.Map;import java.util.concurrent.atomic.AtomicInteger;/** * @Author fanht * @Description 監(jiān)聽當前有哪些用戶,當收到特定通知后通知退出登錄 * @Date 2020/3/5 下午1:48 * @Version 1.0 *///@WebListenerpublic class LoginoutListener extends RedisSessionDAO implements SessionListener { private Logger logger = LoggerFactory.getLogger(this.getClass()); public static final Map<Long,String> mapUser = Maps.newHashMap(); public final static AtomicInteger sessionCount = new AtomicInteger(0); @Override public void onStart(Session session) { //會話創(chuàng)建,在線人數(shù)加一 logger.info('======' + sessionCount); sessionCount.incrementAndGet(); } @Override public void onStop(Session session) { //會話退出,在線人數(shù)減一 sessionCount.decrementAndGet(); } @Override public void onExpiration(Session session) { //會話過期,在線人數(shù)減一 sessionCount.decrementAndGet(); } /** * 獲取在線人數(shù)使用 * @return */ public AtomicInteger getSessionCount() { return sessionCount; } /*@Override public void sessionCreated(HttpSessionEvent se) { onlineCount++; logger.info('創(chuàng)建start====== ===' + se.getSession().getId()); mapUser.put(se.getSession().getCreationTime(),se.getSession().getId()); } @Override public void sessionDestroyed(HttpSessionEvent se) { logger.info('銷毀session============='); }*/}
ShiroConfiguration里面添加配置的監(jiān)聽:
@Bean('sessionManager') public DefaultWebSessionManager sessionManager(RedisSessionDAO sessionDAO, SimpleCookie sessionIdCookie) { DefaultWebSessionManager sessionManager = new DefaultWebSessionManager(); //session存活時間60分鐘 sessionManager.setGlobalSessionTimeout(3600000); sessionManager.setDeleteInvalidSessions(true); //自定義監(jiān)聽 fht 不能使用@WebListern的 HttpSessionListerner 因為shiro重寫了session 2020-03-05 Collection<SessionListener> sessionListeners = new ArrayList<>(); sessionListeners.add(sessionListener()); sessionManager.setSessionListeners(sessionListeners); //sessionManager.setSessionValidationSchedulerEnabled(true); //sessionManager.setSessionValidationScheduler(sessionValidationScheduler); sessionManager.setSessionDAO(sessionDAO); sessionManager.setSessionIdCookieEnabled(true); sessionManager.setSessionIdCookie(sessionIdCookie); return sessionManager; }
/** * 自定義shiro監(jiān)聽 * @return */ @Bean('sessionListener') public LoginoutListener sessionListener(){ LoginoutListener loginoutListener = new LoginoutListener(); return loginoutListener; }
然后重新啟動,測試 ,發(fā)現(xiàn)可以進入到shiro自定義的監(jiān)聽里面了。。。。
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. XML入門的常見問題(三)2. XML基本概念XPath、XSLT與XQuery函數(shù)介紹3. WML的簡單例子及編輯、測試方法第1/2頁4. el-input無法輸入的問題和表單驗證失敗問題解決5. 關(guān)于html嵌入xml數(shù)據(jù)島如何穿過樹形結(jié)構(gòu)關(guān)系的問題6. CSS3實例分享之多重背景的實現(xiàn)(Multiple backgrounds)7. 不要在HTML中濫用div8. vue實現(xiàn)復制文字復制圖片實例詳解9. XML入門的常見問題(四)10. 前端html+css實現(xiàn)動態(tài)生日快樂代碼
