美文网首页
基于SpringRedis,登录和退出监听session

基于SpringRedis,登录和退出监听session

作者: 离别刀 | 来源:发表于2018-07-03 15:33 被阅读0次
153457.png

陋室铭
唐代刘禹锡

山不在高,有仙则名。水不在深,有龙则灵。斯是陋室,惟吾德馨。苔痕上阶绿,草色入帘青。谈笑有鸿儒,往来无白丁。可以调素琴,阅金经。无丝竹之乱耳,无案牍之劳形。南阳诸葛庐,西蜀子云亭。孔子云:何陋之有?

环境配置

1.依赖jar

        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-redis</artifactId>
            <version>1.8.7.RELEASE</version>
        </dependency>

        <!--jedis -->
        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
            <version>2.9.0</version>
        </dependency>

2.redis配置
redis.maxTotal=600
redis.maxIdle=100
redis.maxWaitMillis=2000
redis.testOnBorrow=true
redis.host=localhost
redis.password=
redis.port=6379
redis.timeout=2000

<!-- 配置jedis连接池 -->
    <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
        <property name="maxTotal" value="${redis.maxTotal}" />
        <property name="maxIdle" value="${redis.maxIdle}" />
        <property name="maxWaitMillis" value="${redis.maxWaitMillis}" />
        <property name="testOnBorrow" value="${redis.testOnBorrow}" />
    </bean>

    <!-- 配置spring data redis -->
    <bean id="jedisConnectionFactory"
        class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
        <property name="hostName" value="${redis.host}" />
        <property name="password" value="${redis.password}" />
        <property name="port" value="${redis.port}" />
        <property name="timeout" value="${redis.timeout}" />
        <property name="usePool" value="true" />
        <property name="poolConfig" ref="jedisPoolConfig" />
    </bean>
    <bean id="redisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate">
        <property name="connectionFactory" ref="jedisConnectionFactory" />
        <property name="keySerializer">
            <bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />
        </property>
        <property name="valueSerializer">
            <bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />
        </property>
        <property name="hashKeySerializer">
            <bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" />
        </property>
        <property name="hashValueSerializer">
            <bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" />
        </property>
    </bean>

    <!-- 配置spring session保存在redis中,session超时时间1800秒 -->
    <bean id="redisHttpSessionConfiguration"
        class="org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration">
        <property name="maxInactiveIntervalInSeconds" value="7200" />
    </bean>

  <cache:annotation-driven/>
    
    <bean id="cacheManager" class="org.springframework.data.redis.cache.RedisCacheManager">
        <constructor-arg name="redisOperations" ref="redisTemplate"></constructor-arg>
    </bean>

登录监控

@Component
public class StudentLoginEventListener implements ApplicationListener<LoginEvent>, ApplicationContextAware {
    private Log logger = LogFactory.getLog(StudentLoginEventListener.class);
    @Autowired
    private StringRedisTemplate redisTemplate;

    @Autowired
    private RedisOperationsSessionRepository redisOperationsSessionRepository;

    private ApplicationContext applicationContext;

    @Override
    public void onApplicationEvent(LoginEvent event) {
        HttpSession session = (HttpSession) event.getSource();
        //todo
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }
}

退出监控

@Component
public class UserLogoutEventListener implements ApplicationListener<AbstractSessionEvent> {

    private Log log = LogFactory.getLog(getClass());
    @Autowired
    private StringRedisTemplate redisTemplate;

    @Autowired
    private RedisOperationsSessionRepository redisOperationsSessionRepository;

    @Override
    public void onApplicationEvent(AbstractSessionEvent event) {
        log.info("logout "+event.getClass());
        if (event instanceof SessionDestroyedEvent || event instanceof SessionExpiredEvent || event instanceof SessionDeletedEvent) {
            SessionDestroyedEvent sessionEvent = (SessionDestroyedEvent) event;
            Session session = sessionEvent.getSession();
            Object obj= session.getAttribute("sessionUser");
            if(obj==null){
                log.info("logout user is null");
            }else{
              //todo
            }
        }
    }

}

辅助

public class AfterLoginFilter implements Filter, ApplicationEventPublisherAware {

    private ApplicationEventPublisher applicationEventPublisher;

    @Override
    public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
        this.applicationEventPublisher = applicationEventPublisher;
    }

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
            throws IOException, ServletException {
        chain.doFilter(request, response);
        HttpServletRequest req = WebUtils.toHttp(request);
        if (("POST".equalsIgnoreCase(req.getMethod()) || ("GET".equalsIgnoreCase(req.getMethod()) ) {
            HttpSession session = req.getSession();
            //todo
            LoginEvent event = new LoginEvent(session);
            applicationEventPublisher.publishEvent(event);
        }
    }

    @Override
    public void destroy() {

    }

}

public class LoginEvent extends ApplicationEvent {
    public LoginEvent(Object source) {
        super(source);
    }
}

相关文章

网友评论

      本文标题:基于SpringRedis,登录和退出监听session

      本文链接:https://www.haomeiwen.com/subject/tkaduftx.html