JSD-7449 开源任务材料
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.

90 lines
3.1 KiB

package com.fr.plugin.sso.filter;
import com.fr.decision.fun.impl.AbstractEmbedRequestFilterProvider;
import com.fr.json.JSONObject;
import com.fr.plugin.sso.config.simple.account.PluginSimpleConfig;
import com.fr.plugin.sso.utils.FRUtils;
import com.fr.plugin.sso.utils.HttpUtils;
import com.fr.plugin.sso.utils.ResponseUtils;
import com.fr.plugin.sso.utils.Utils;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;
public class SSOFilter extends AbstractEmbedRequestFilterProvider {
@Override
public void filter(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) {
String url = FRUtils.getAllUrl(httpServletRequest);
FRUtils.FRLogInfo("url:"+url);
FRUtils.FRLogInfo("url2"+httpServletRequest.getRequestURL().toString());
String token = httpServletRequest.getParameter("c_token");
String env = httpServletRequest.getParameter("application_env");
FRUtils.FRLogInfo("token:"+token+";env:"+env);
boolean isLogin = FRUtils.isLogin(httpServletRequest);
if(isLogin){
return;
}
if(Utils.isNotNullStr(token) && Utils.isNotNullStr(env)){
Map<String,String> envConfig = this.getEnvConfig();
String postUrl = envConfig.get(env);
FRUtils.logout(httpServletRequest,httpServletResponse);
Map<String,String> header=new HashMap<String,String>();
header.put("Conetent-type","application/json");
header.put("h-app-id","100");
header.put("h-token",token);
String result = HttpUtils.HttpPostJson(postUrl,"",header);
if(Utils.isNullStr(result)){
ResponseUtils.failedResponse(httpServletResponse,"获取用户信息失败!");
return;
}
JSONObject json = new JSONObject(result);
String desc = json.getString("desc");
if(!desc.equals("OK")){
ResponseUtils.failedResponse(httpServletResponse,"获取用户信息失败!");
return;
}
JSONObject data = json.getJSONObject("data");
String username = data.getString("name");
String redirectURL = "";
if(url.contains("?c_token")){
redirectURL = url.substring(0,url.indexOf("?c_token"));
}else{
redirectURL = url.substring(0,url.indexOf("&c_token"));
}
FRUtils.FRLogInfo("redirectUrl:"+redirectURL);
FRUtils.login(httpServletRequest,httpServletResponse,username,"");
}
}
private Map<String, String> getEnvConfig() {
PluginSimpleConfig psc = PluginSimpleConfig.getInstance();
String url = psc.getUrl();
FRUtils.FRLogInfo("config:"+url);
String[] urls = url.split(";");
Map<String,String> result = new HashMap<String,String>();
for(String config : urls){
String[] configs = config.split(",");
result.put(configs[0],configs[1]);
}
return result;
}
}