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.
95 lines
3.5 KiB
95 lines
3.5 KiB
3 years ago
|
package com.fr.plugin.xxxx.fymh.sso;
|
||
|
|
||
|
import com.fr.base.TemplateUtils;
|
||
|
import com.fr.decision.fun.impl.AbstractGlobalRequestFilterProvider;
|
||
|
import com.fr.json.JSONObject;
|
||
|
import com.fr.plugin.context.PluginContexts;
|
||
|
import com.fr.plugin.transform.FunctionRecorder;
|
||
|
import com.fr.stable.StringUtils;
|
||
|
import com.fr.stable.fun.Authorize;
|
||
|
import com.fr.third.org.apache.commons.codec.binary.Base64;
|
||
|
|
||
|
|
||
|
import javax.servlet.FilterChain;
|
||
|
import javax.servlet.http.HttpServletRequest;
|
||
|
import javax.servlet.http.HttpServletResponse;
|
||
|
import java.nio.charset.Charset;
|
||
|
import java.util.HashMap;
|
||
|
import java.util.Map;
|
||
|
|
||
|
/**
|
||
|
* @Author fr.open
|
||
|
* @Date 2022/1/3
|
||
|
* @Description
|
||
|
**/
|
||
|
@FunctionRecorder
|
||
|
@Authorize(callSignKey = PluginConstants.PLUGIN_ID)
|
||
|
public class LoginFilter extends AbstractGlobalRequestFilterProvider {
|
||
|
@Override
|
||
|
public String filterName() {
|
||
|
return "xxxx";
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public String[] urlPatterns() {
|
||
|
if (PluginContexts.currentContext().isAvailable()) {
|
||
|
return new String[]{"/decision/QPaaS"};
|
||
|
} else {
|
||
|
return new String[0];
|
||
|
}
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public void doFilter(HttpServletRequest req, HttpServletResponse res, FilterChain filterChain) {
|
||
|
String code = req.getParameter("code");
|
||
|
if (StringUtils.isBlank(code)) {
|
||
|
CommonUtils.setError(res, "code is not null");
|
||
|
return;
|
||
|
}
|
||
|
String appid = req.getParameter("appid");
|
||
|
if (StringUtils.isBlank(appid)) {
|
||
|
CommonUtils.setError(res, "appid is not null");
|
||
|
return;
|
||
|
}
|
||
|
LogUtils.debug4plugin("get request code is {}", code);
|
||
|
LogUtils.debug4plugin("get request appid is {}", appid);
|
||
|
FeiYuSsoConfig instance = FeiYuSsoConfig.getInstance();
|
||
|
if (StringUtils.isBlank(instance.getAuthUser()) || StringUtils.isBlank(instance.getAuthPass()) || StringUtils.isBlank(instance.getInfoUrl())) {
|
||
|
CommonUtils.setError(res, "config is not null");
|
||
|
return;
|
||
|
}
|
||
|
Map<String, String> header = new HashMap<>();
|
||
|
header.put("Authorization", getHeader(instance.getAuthUser(), instance.getAuthPass()));
|
||
|
String url = String.format("%s?code=%s&appid=%s", instance.getInfoUrl(), code, appid);
|
||
|
LogUtils.debug4plugin("header is {}",header);
|
||
|
String result = HttpUtil.sendGet(url, null, header);
|
||
|
LogUtils.debug4plugin("info url res is {} by url {}", result, url);
|
||
|
JSONObject object = new JSONObject(result);
|
||
|
if (!object.has("data") || !object.getJSONObject("data").has("adUserNo")) {
|
||
|
CommonUtils.setError(res, "the adUserNo not exist form result : " + result);
|
||
|
return;
|
||
|
}
|
||
|
CommonUtils.login(object.getJSONObject("data").getString("adUserNo"), req, res);
|
||
|
try {
|
||
|
String redirect = req.getParameter("redirect");
|
||
|
LogUtils.debug4plugin("redirect url {}", redirect);
|
||
|
if(StringUtils.isBlank(redirect)){
|
||
|
String root = TemplateUtils.render("${fineServletURL}");
|
||
|
res.sendRedirect(root + "/url/mobile");
|
||
|
}else {
|
||
|
res.sendRedirect(redirect);
|
||
|
}
|
||
|
} catch (Exception e) {
|
||
|
LogUtils.error(e.getMessage(), e);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
private String getHeader(String user, String pass) {
|
||
|
String auth = user + ":" + pass;
|
||
|
byte[] encodedAuth = Base64.encodeBase64(auth.getBytes(Charset.forName("US-ASCII")));
|
||
|
String authHeader = "Basic " + new String(encodedAuth);
|
||
|
return authHeader;
|
||
|
}
|
||
|
|
||
|
}
|