springboot设置session监听器

                             springboot设置session监听器

 

 

 1.添加session管理器

package com.sinosoft.session.server;

import java.util.HashMap;
import javax.servlet.http.HttpSession;

/**
 * Created by  lijunming
 * on  date 2018-09-20
 * session管理器
 * time 20:01
 */
public class MySessionContext {
    private static HashMap mymap = new HashMap();

    public static synchronized void AddSession(HttpSession session) {
        if (session != null) {
            mymap.put(session.getId(), session);
        }
    }

    public static synchronized void DelSession(HttpSession session) {
        if (session != null) {
            mymap.remove(session.getId());
        }
    }

    public static synchronized HttpSession getSession(String session_id) {
        if (session_id == null)
            return null;
        return (HttpSession) mymap.get(session_id);
    }

}

 

2.添加session监听器

package com.sinosoft.session.server;

import org.springframework.stereotype.Component;

import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

/**
 * Created by  lijunming
 * on  date 2018-09-20
 * session监听器
 * time 20:02
 */
@Component
public class MySessionListener implements HttpSessionListener {
    @Override
    public void sessionCreated(HttpSessionEvent httpSessionEvent) {
        System.out.println("session正在創建");
        MySessionContext.AddSession(httpSessionEvent.getSession());
    }

    @Override
    public void sessionDestroyed(HttpSessionEvent httpSessionEvent) {
        HttpSession session = httpSessionEvent.getSession();
        System.out.println("session注銷中");
        MySessionContext.DelSession(session);
    }
}

 

3.登录的时候设置session超时时间

package com.ocrweb.controller;

import com.etop.common.pojo.ResponseResult;
import com.etop.common.util.IpAddrUtil;
import com.ocrweb.entity.SysModel;
import com.ocrweb.entity.SysUser;
import com.ocrweb.pojo.SessionKey;
import com.ocrweb.service.SysModelService;
import com.ocrweb.service.SysUserService;
import com.ocrweb.sso.AgentThreadLocal;
import com.ocrweb.sso.CurrentUser;
import com.ocrweb.vo.UserVo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * 不需要权限既能访问的接口
 */
@Controller
public class ExportController {

 
    /**
     * 登录页
     */
    @GetMapping("/login")
    public String toUserLogin() {
        return "user/login";
    }



    /**
     * 登录
     */
    @ResponseBody
    @RequestMapping(value = "toLogin.do", produces = "application/json")
    public ResponseResult login(String loginName, String password,HttpSession session) {
        if(StringUtils.isEmpty(loginName) || StringUtils.isEmpty(password)){
            return new ResponseResult(false,"参数丢失!");
        }
        ResponseResult requestResult = sysUserService.findByLoginNameAndPassword(loginName, password);
        if (requestResult.isSuccess()) {
            //session的实效时间,单位为秒
            session.setMaxInactiveInterval(1800);
        }
        return requestResult;
    }



}

 

你可能感兴趣的:(springboot,springBoot,java)