package com.jeeplus.modules.API.sys;

import com.jeeplus.common.json.AjaxJson;
import com.jeeplus.common.security.shiro.session.JedisSessionDAO;
import com.jeeplus.common.security.shiro.session.SessionDAO;
import com.jeeplus.common.utils.CacheUtils;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.common.web.BaseController;
import com.jeeplus.modules.sys.security.FormAuthenticationFilter;
import com.jeeplus.modules.sys.security.SystemAuthorizingRealm.Principal;
import com.jeeplus.modules.sys.utils.UserUtils;
import com.jeeplus.modules.utils.ErrorCode;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.UnavailableSecurityManagerException;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.session.InvalidSessionException;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.web.util.WebUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Collection;

/**
 * 登录Controller
 * 
 * @author mengxy
 * @version 2017-5-17
 */
@Controller

public class SessionController extends BaseController {

	@Autowired
	private SessionDAO sessionDAO;

	private JedisSessionDAO jedisSessionDAO;


	/**
	 * 管理session
	 * 
	 * @throws IOException
	 */
	@RequestMapping(value = "${frontPath}/session")
	@ResponseBody
	public AjaxJson sessionUpdate(HttpServletRequest request, HttpServletResponse response, Model model) throws IOException {
		AjaxJson j = new AjaxJson();
		Session session = null;
		String sessionId = "";
		try{
			Subject subject = SecurityUtils.getSubject();
			session = subject.getSession(false);
			Session session1 = UserUtils.getSession();
			session.touch();
			//更新会话：SecurityUtils.getSubject().getSession().touch()；
			//停止会话：SecurityUtils.getSubject().getSession().stop()；
			sessionId = session.getId().toString();
		}catch (InvalidSessionException e){
			logger.error("Invalid session error", e);
		}catch (UnavailableSecurityManagerException e2){
			logger.error("Unavailable SecurityManager error", e2);
		}
		logger.info("登录成功返回的sessionId+++++++++++++" + sessionId);
		j.setSuccess(true);
		j.setMsg("session更新成功");
		j.setErrorCode(ErrorCode.code_1004);
		return j;

	}



}
