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.
120 lines
4.4 KiB
120 lines
4.4 KiB
3 years ago
|
package com.fr.plugin.xx.sso;
|
||
|
|
||
|
import com.fr.decision.fun.impl.AbstractGlobalRequestFilterProvider;
|
||
|
import com.fr.decision.webservice.utils.WebServiceUtils;
|
||
|
import com.fr.decision.webservice.v10.config.ConfigService;
|
||
|
import com.fr.locale.InterProviderFactory;
|
||
|
import com.fr.log.FineLoggerFactory;
|
||
|
import com.fr.plugin.context.PluginContexts;
|
||
|
import com.fr.plugin.xx.sso.conf.PortalSsoConfig;
|
||
|
import com.fr.plugin.xx.sso.util.CallUtil;
|
||
|
import com.fr.plugin.xx.sso.util.CommonUtils;
|
||
|
import com.fr.plugin.xx.sso.util.LogUtils;
|
||
|
import com.fr.plugin.transform.FunctionRecorder;
|
||
|
import com.fr.stable.StringUtils;
|
||
|
import com.fr.stable.fun.Authorize;
|
||
|
import com.fr.web.utils.WebUtils;
|
||
|
|
||
|
import javax.servlet.FilterChain;
|
||
|
import javax.servlet.http.HttpServletRequest;
|
||
|
import javax.servlet.http.HttpServletResponse;
|
||
|
import java.io.IOException;
|
||
|
import java.io.PrintWriter;
|
||
|
import java.util.HashMap;
|
||
|
import java.util.Map;
|
||
|
|
||
|
|
||
|
/**
|
||
|
* @author xx
|
||
|
* @since 2021/12/04
|
||
|
*/
|
||
|
@FunctionRecorder
|
||
|
@Authorize(callSignKey = PluginConstants.PLUGIN_ID)
|
||
|
public class LoginFilter extends AbstractGlobalRequestFilterProvider {
|
||
|
|
||
|
|
||
|
@Override
|
||
|
public void doFilter(HttpServletRequest request, HttpServletResponse response, FilterChain chain) {
|
||
|
PortalSsoConfig config = PortalSsoConfig.getInstance();
|
||
|
if (CommonUtils.isLogin(request)) {
|
||
|
CommonUtils.next(request, response, chain);
|
||
|
}
|
||
|
String code = request.getParameter("code");
|
||
|
if (StringUtils.isNotBlank(code)) {
|
||
|
LogUtils.debug4plugin("get code is {}",code);
|
||
|
if (!config.isAccept()) {
|
||
|
setError(response, String.format("单点登录配置未完成"));
|
||
|
return;
|
||
|
}
|
||
|
String account = CallUtil.call(code);
|
||
|
LogUtils.debug4plugin("get account is {}",account);
|
||
|
if (!CommonUtils.checkUser(account)) {
|
||
|
setError(response, String.format("【%s】用户不存在", account));
|
||
|
return;
|
||
|
}
|
||
|
CommonUtils.login(account, request, response);
|
||
|
CommonUtils.next(request, response, chain);
|
||
|
return;
|
||
|
}
|
||
|
try {
|
||
|
if(!request.getRequestURI().endsWith("/portalhndx")){
|
||
|
CommonUtils.next(request, response, chain);
|
||
|
return;
|
||
|
}
|
||
|
String redirect = request.getRequestURL() + (StringUtils.isNotBlank(request.getQueryString()) ? request.getQueryString() : StringUtils.EMPTY);
|
||
|
String url = String.format("%s?client_id=%s&client_secret=%s&redirect_uri=%s", config.getMethodGetCode(), config.getClientId(), config.getClientSecret(), redirect);
|
||
|
LogUtils.debug4plugin("current url {} is not log redirect to {}", redirect, url);
|
||
|
response.sendRedirect(url);
|
||
|
} catch (IOException e) {
|
||
|
LogUtils.error(e.getMessage(), e);
|
||
|
}
|
||
|
return;
|
||
|
|
||
|
|
||
|
}
|
||
|
|
||
|
|
||
|
@Override
|
||
|
public String filterName() {
|
||
|
return "9769";
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public String[] urlPatterns() {
|
||
|
if (!PluginContexts.currentContext().isAvailable()) {
|
||
|
LogUtils.error("未注册或禁用");
|
||
|
return new String[]{"/neverbeused"};
|
||
|
}
|
||
|
String servletPathName = "decision";
|
||
|
try {
|
||
|
servletPathName = ConfigService.getInstance().getBasicParam().getServletPathName();
|
||
|
} catch (Exception e) {
|
||
|
LogUtils.error(e.getMessage(), e);
|
||
|
}
|
||
|
return new String[]{
|
||
|
"/" + servletPathName,
|
||
|
"/" + servletPathName+"/portalhndx",
|
||
|
//"/" + servletPathName + "/view/report",
|
||
|
//"/" + servletPathName + "/view/form",
|
||
|
|
||
|
};
|
||
|
}
|
||
|
|
||
|
private void setError(HttpServletResponse res, String reason) {
|
||
|
try {
|
||
|
PrintWriter printWriter = WebUtils.createPrintWriter(res);
|
||
|
Map<String, Object> map = new HashMap<>();
|
||
|
map.put("result", InterProviderFactory.getProvider().getLocText("Fine-Engine_Error_Page_Result"));
|
||
|
map.put("reason", reason);
|
||
|
map.put("solution", InterProviderFactory.getProvider().getLocText("Fine-Engine_Please_Contact_Platform_Admin"));
|
||
|
String page = WebServiceUtils.parseWebPageResourceSafe("com/fr/web/controller/decision/entrance/resources/unavailable.html", map);
|
||
|
printWriter.write(page);
|
||
|
printWriter.flush();
|
||
|
printWriter.close();
|
||
|
} catch (Exception e) {
|
||
|
FineLoggerFactory.getLogger().error(e.getMessage(), e);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
}
|