You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
147 lines
4.5 KiB
147 lines
4.5 KiB
4 years ago
|
package com.fr.plugin.bsSSO.handler;
|
||
|
|
||
|
import com.fr.base.TemplateUtils;
|
||
|
import com.fr.decision.fun.impl.BaseHttpHandler;
|
||
|
import com.fr.json.JSONObject;
|
||
|
import com.fr.plugin.bsSSO.bean.simple.account.PluginSimpleConfig;
|
||
|
import com.fr.plugin.bsSSO.utils.FRUtils;
|
||
|
import com.fr.plugin.bsSSO.utils.HttpUtils;
|
||
|
import com.fr.plugin.bsSSO.utils.ResponseUtils;
|
||
|
import com.fr.plugin.bsSSO.utils.Utils;
|
||
|
import com.fr.plugin.transform.FunctionRecorder;
|
||
|
import com.fr.third.org.apache.http.HttpEntity;
|
||
|
import com.fr.third.org.apache.http.HttpResponse;
|
||
|
import com.fr.third.org.apache.http.client.HttpClient;
|
||
|
import com.fr.third.org.apache.http.client.methods.HttpGet;
|
||
|
import com.fr.third.org.apache.http.impl.client.BasicCookieStore;
|
||
|
import com.fr.third.org.apache.http.impl.client.HttpClientBuilder;
|
||
|
import com.fr.third.org.apache.http.impl.cookie.BasicClientCookie;
|
||
|
import com.fr.third.org.apache.http.util.EntityUtils;
|
||
|
import com.fr.third.springframework.web.bind.annotation.RequestMethod;
|
||
|
import com.fr.web.utils.WebUtils;
|
||
|
|
||
|
import javax.servlet.http.Cookie;
|
||
|
import javax.servlet.http.HttpServletRequest;
|
||
|
import javax.servlet.http.HttpServletResponse;
|
||
|
import java.io.IOException;
|
||
|
import java.util.HashMap;
|
||
|
import java.util.Map;
|
||
|
|
||
|
public class MobileLogin extends BaseHttpHandler {
|
||
|
|
||
|
|
||
|
public MobileLogin() {
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public RequestMethod getMethod() {
|
||
|
return RequestMethod.GET;
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public String getPath() {
|
||
|
return "/mobilelogin";
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public boolean isPublic() {
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public void handle(HttpServletRequest req, HttpServletResponse res) throws Exception {
|
||
|
PluginSimpleConfig psc = PluginSimpleConfig.getInstance();
|
||
|
|
||
|
//获取请求参数
|
||
|
String token = req.getParameter("token");
|
||
|
//生成cookie
|
||
|
BasicClientCookie cookie = new BasicClientCookie("access_token", token);
|
||
|
cookie.setDomain(psc.getMdomain());
|
||
|
cookie.setPath("/");
|
||
|
//获取用户id
|
||
|
String userid = getUserid(cookie,psc);
|
||
|
//获取用户名
|
||
|
String username = getUsername(cookie,userid,psc);
|
||
|
|
||
|
if(!FRUtils.isUserExist(username)){
|
||
|
String tip ="数据平台不存在该账号:"+username;
|
||
|
Map<String, String> parameterMap = new HashMap<String, String>();
|
||
|
String path = "/com/fr/plugin/bsSSO/html/error.html";
|
||
|
parameterMap.put("tip", tip);
|
||
|
String macPage = TemplateUtils.renderTemplate(path, parameterMap);
|
||
|
WebUtils.printAsString(res, macPage);
|
||
|
return ;
|
||
|
}
|
||
|
|
||
|
FRUtils.Login(req,res,username,PluginSimpleConfig.getInstance().getMindex());
|
||
|
|
||
|
|
||
|
// try {
|
||
|
// FRUtils.Login2(req,res,username,PluginSimpleConfig.getInstance().getMindex());
|
||
|
// } catch (Exception e) {
|
||
|
//// res.sendRedirect(PluginSimpleConfig.getInstance().getMindex());
|
||
|
// }
|
||
|
}
|
||
|
|
||
|
private String getUsername(BasicClientCookie cookies, String userid, PluginSimpleConfig psc) {
|
||
|
String url = psc.getMuser();
|
||
|
url += userid;
|
||
|
|
||
|
String result = get(cookies,url);
|
||
|
|
||
|
if(Utils.isNullStr(result)){
|
||
|
return "";
|
||
|
}
|
||
|
|
||
|
JSONObject resultJson = new JSONObject(result);
|
||
|
int status = resultJson.getInt("status");
|
||
|
|
||
|
if(status != 200){
|
||
|
return "";
|
||
|
}
|
||
|
|
||
|
return resultJson.getJSONObject("content").getString("login");
|
||
|
}
|
||
|
|
||
|
//获取用户id
|
||
|
private String getUserid(BasicClientCookie cookies,PluginSimpleConfig psc) {
|
||
|
String url = psc.getMtoken();
|
||
|
String result = get(cookies,url);
|
||
|
|
||
|
if(Utils.isNullStr(result)){
|
||
|
return "";
|
||
|
}
|
||
|
|
||
|
JSONObject resultJson = new JSONObject(result);
|
||
|
int status = resultJson.getInt("status");
|
||
|
|
||
|
if(status != 200){
|
||
|
return "";
|
||
|
}
|
||
|
|
||
|
return resultJson.getString("content");
|
||
|
}
|
||
|
|
||
|
private String get(BasicClientCookie cookie,String url) {
|
||
|
BasicCookieStore cookieStore = new BasicCookieStore();
|
||
|
cookieStore.addCookie(cookie);
|
||
|
|
||
|
HttpClient client = HttpClientBuilder.create().setDefaultCookieStore(cookieStore).build();
|
||
|
final HttpGet request = new HttpGet(url);
|
||
|
|
||
|
String returnResult = "";
|
||
|
|
||
|
try{
|
||
|
HttpResponse response = client.execute(request);
|
||
|
HttpEntity entity = response.getEntity();
|
||
|
returnResult = EntityUtils.toString(entity, "utf-8");
|
||
|
}catch(Exception e){
|
||
|
FRUtils.FRLogInfo("get exception " + e.getMessage());
|
||
|
}
|
||
|
|
||
|
|
||
|
return returnResult;
|
||
|
}
|
||
|
}
|
||
|
|