Springboot sessionリスナーの設定
Springboot sessionリスナーの設定
1.セッションマネージャの追加
2.セッションリスナーの追加
3.ログイン時にセッションタイムアウト時間を設定する
1.セッションマネージャの追加
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.セッションリスナーの追加
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.ログイン時にセッションタイムアウト時間を設定する
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;
}
}