平台默认提供的有在线用户的功能,要判断用户是否已登录,可以通过在线用户的数据判断
如果登录过获取到对用的bsessionid,调用注销的API强制注销
.j的具体代码如下:
import java.io.IOException;
import java.sql.Date;
import java.util.ArrayList;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.dom4j.Document;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.justep.biz.client.ActionEngine;
import com.justep.biz.client.ActionResult;
import com.justep.biz.client.ActionUtils;
import com.justep.ui.JustepConfig;
import com.justep.ui.util.NetUtils;
public class Login extends com.justep.ui.impl.JProcessorImpl {
public void execute(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String username = request.getParameter("username");
//判断用户是否登录,如果登录强制注销
ActionResult onLineResult = ActionEngine.invokeActions(JustepConfig.getBusinessServer() + "/onlineUsers", null, null, null, "application/json", "application/json", null, null, "get", null);
JSONObject onLineContent = (JSONObject) onLineResult.getContent();
JSONObject data = (JSONObject) ((JSONObject)onLineContent.get("data")).get("value");
JSONArray ar = (JSONArray) data.get("rows");
for (int i = 0; i < ar.size(); i++) {//解析在线用户数据的JSONArray
JSONObject jsonObject = ar.getJSONObject(i);
String name = (String) ((JSONObject)jsonObject.get("name")).get("value");
String bsessionID = (String) ((JSONObject)jsonObject.get("sessionid")).get("value");
if(name.equals(username)){
ActionEngine.logout(bsessionID, ActionUtils.JSON_CONTENT_TYPE); //注销已登录的当前用户
}
}
//用户登录
JSONObject content = null;
String bsessionid = "";
//从cookie中获取已有的bsessionid
Cookie[] cookies = request.getCookies();
if (cookies != null){
for (int i = 0; i < cookies.length; i++) {
String name = cookies[i].getName();
if(name.equals("bsessionid")){
bsessionid = cookies[i].getValue();
}
}
}
//判断bsessionid是否超时
ActionResult checkResult = ActionEngine.checkSession(bsessionid, ActionUtils.JSON_CONTENT_TYPE);
if (checkResult.isSessionTimeOut()) {
String password = request.getParameter("password");
String ip = request.getRemoteAddr();
String language = "zh_CN";
Date loginDate = new Date(System.currentTimeMillis());
ActionResult actionResult = ActionEngine.login2(username, password, ip, language, loginDate, null, ActionUtils.JSON_CONTENT_TYPE, null);
content = (JSONObject) actionResult.getContent();
bsessionid = actionResult.getBSessionID();
//把bsessionid放到Cookie中
Cookie cookie = new Cookie("bsessionid",bsessionid);
cookie.setMaxAge(-1);
cookie.setPath("/");
response.addCookie(cookie);
} else {
content = (JSONObject) checkResult.getContent();
}
content.put("bsessionid", bsessionid);
response.setCharacterEncoding("UTF-8");
response.addHeader("Access-Control-Allow-Origin", "*");
response.setContentType(ActionUtils.JSON_CONTENT_TYPE);
response.sendRedirect("/x5/UI2/portal/pc3/index.w");
response.flushBuffer();
}
}
评一波