springboot項目監聽器不起作用


1.使用springboot項目,現在有個需求是在添加或者修改某個菜單后,菜單會影響角色,角色影響用戶。所有受影響的用戶在要退出重新登錄。

自己實現的思路是這樣的:寫一個監聽器,在收到某個特定的請求后,監聽當前所有的用戶,如果是受影響的用戶,就銷毀session,讓重新登錄。

 

  有了思路后,剛開始上網搜的是怎么在spring boot中添加監聽:網上大部分的思路都一樣:使用@ServletComponentScan和一個實現了HttpSessionListener的方法就可以了。(參考:https://www.cnblogs.com/liuyong1993/p/10012808.html)。但是自己按照這個配置了后,一直不起作用。啟動時候能debug到這個自定義的監聽里面,但是登錄后缺不能實現

sessionCreated()
package com.listener;

import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

/**
 * session監聽器
 * @author Administrator
 */
@WebListener
public class SessionListener implements HttpSessionListener{

    private int onlineCount = 0;//記錄session的數量
    
    /**
     * session創建后執行
     */
    @Override
    public void sessionCreated(HttpSessionEvent se) {
        onlineCount++;
        System.out.println("【HttpSessionListener監聽器】 sessionCreated, onlineCount:" + onlineCount);
        se.getSession().getServletContext().setAttribute("onlineCount", onlineCount);
    }

    /**
     * session失效后執行
     */
    @Override
    public void sessionDestroyed(HttpSessionEvent se) {
        if (onlineCount > 0) {
            onlineCount--;
        }
        System.out.println("【HttpSessionListener監聽器】 sessionDestroyed, onlineCount:" + onlineCount);
        se.getSession().getServletContext().setAttribute("onlineCount", onlineCount);
    }

}
復制代碼

  

還問了群里的大神幫忙看了下,也沒問題。剛開始懷疑是 不是登錄時候監聽的HttpSession,因為實現的是HttpSessionListener,是需要有個發起的動作的.但是自己登錄時候也有httpSession。然后在自己的測試類里面進行測試,發現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("調用退出接口並清除shiro緩存" + userIds);
        logger.info("獲取監聽存取的信息" + 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機制? 想到這個疑問,又上網搜索,最后通過這個發現是可以的(參考:https://blog.csdn.net/qq_34021712/article/details/80418112

附上自己的配置:

 自定義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 監聽當前有哪些用戶,當收到特定通知后通知退出登錄
 * @Date 2020/3/5 下午1:48
 * @Version 1.0
 */
//@WebListener
public 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) {
        //會話創建,在線人數加一
        logger.info("======" + sessionCount);
        sessionCount.incrementAndGet();
    }

    @Override
    public void onStop(Session session) {
        //會話退出,在線人數減一
        sessionCount.decrementAndGet();
    }

    @Override
    public void onExpiration(Session session) {
        //會話過期,在線人數減一
        sessionCount.decrementAndGet();

    }


    /**
     * 獲取在線人數使用
     * @return
     */
    public AtomicInteger getSessionCount() {
        return sessionCount;
    }



    /*@Override
    public void sessionCreated(HttpSessionEvent se) {
        onlineCount++;
        logger.info("創建start====== ===" + se.getSession().getId());
        mapUser.put(se.getSession().getCreationTime(),se.getSession().getId());
    }

    @Override
    public void sessionDestroyed(HttpSessionEvent se) {
        logger.info("銷毀session=============");
    }*/
}

  

ShiroConfiguration里面添加配置的監聽:
 @Bean("sessionManager")
    public DefaultWebSessionManager sessionManager(RedisSessionDAO sessionDAO, SimpleCookie sessionIdCookie) {
        DefaultWebSessionManager sessionManager = new DefaultWebSessionManager();
        //session存活時間60分鍾
        sessionManager.setGlobalSessionTimeout(3600000);
        sessionManager.setDeleteInvalidSessions(true);
        //自定義監聽 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監聽
     * @return
     */
    @Bean("sessionListener")
    public LoginoutListener sessionListener(){
        LoginoutListener loginoutListener = new LoginoutListener();

        return loginoutListener;
    }

  

 

然后重新啟動,測試 ,發現可以進入到shiro自定義的監聽里面了。。。。

 

 

 

 

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM