package com.gta.sso.demo.controller;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import com.alibaba.fastjson.JSONObject;
import com.gta.platform.cache.service.ICacheService;
import com.gta.platform.cas.client.sso.controller.PathUtils;
import com.gta.platform.cas.client.util.CasConfigurationConstants;
import com.gta.platform.configuration.util.ClientIsOutside;
import com.gta.platform.configuration.util.ConfigurationConstants;
import com.gta.platform.configuration.util.ConfigurationUtils;
import com.gta.platform.util.HttpClientUtils;
@RestController
public class HelloContorller {
private ICacheService cacheService;
// 1
@RequestMapping(value = "/index")
public String ok() {
//进入主页
return "index3";
}
@RequestMapping(value = "/nologin")
public String nologin(HttpServletRequest request, HttpServletResponse response) throws Exception {
return "nologin";
}
@RequestMapping(value = "/logout")
public @ResponseBody void logout(HttpServletRequest request, HttpServletResponse response) throws Exception {
// online user number-1
WebApplicationContext context = WebApplicationContextUtils
.getRequiredWebApplicationContext(request.getServletContext());
cacheService = (ICacheService) context.getBean(ICacheService.class);
if (cacheService.get("casUserNumbers") != null) {
Long un = Long.parseLong(cacheService.get("casUserNumbers")) - 1;
cacheService.setCastime("casUserNumbers", un + "");
}
String userId = "";
if (request.getSession().getAttribute("userId") != null) {
userId = request.getSession().getAttribute("userId").toString();
}
if (!StringUtils.isEmpty(userId)) {
cacheService.delExceed(userId + ClientIsOutside.get() +"expireTime");
cacheService.delExceed(userId + ClientIsOutside.get() + "logouturi");
}
String uri = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
+ request.getContextPath() + "/index";
StringBuilder str = new StringBuilder();
str.append(ConfigurationUtils.getMustExistsConfig(ConfigurationConstants.PLATFORM_CAS_SERVER_URL_PREFIX))
.append("/logout?service=").append(uri);
String logoutc = str.toString();
request.getSession().removeAttribute("userId");
request.getSession().invalidate();
response.sendRedirect(logoutc);
}
}